一、脚本
1、编写脚本/root/bin/systeminfo.sh,显示当前主机系统信息,包括主机名,IPv4地址,操作系统版本,内核版本,CPU型号,内存大小,硬盘大小。
#!/bin/bash
#
echo "hostname: `hostname`"
echo "IPV4address: `ifconfig |grep ‘inet\>‘ |sed -nr ‘s@.*addr:(.*)Bca.*@\1@gp‘`"
echo "system relases: `cat /etc/centos-release`"
echo "kernel release: `uname -r`"
echo "memory size: `cat meminfo|grep "MemTotal"`"
echo "Disk size: `fdisk -l | grep "^Disk /dev/s[a-z]"`"
4、编写脚本/root/bin/links.sh,显示正连接本主机的每个远程主机的IPv4地址和连接数,并按连接数从大到小排序
netstat -tn | awk ‘/^tcp/{print $4,$1}‘ | sort |uniq -c| sort -n
9、写一个脚本/root/bin/hostping.sh,接受一个主机的IPv4地址做为参数,测试是否可连通。如果能ping通,则提示用户“该IP地址可访问”;如果不可ping通,则提示用户“该IP地址不可访问”
#!/bin/bash
#
read -p "please input a IPv4 addr: " addr
ping -W 2 -c 2 $addr &>/dev/null && echo "this IPv4addr can be access"|| echo "this ipv4addr can not be access"
10、判断硬盘的每个分区空间和inode的利用率是否大于80,如果是,发邮件通知root磁盘满
#!/bin/bash
#
for I in `df |grep "^/dev/sd" | cut -c 45-46`;do
echo $I
if [ $I -ge 80 ] ;then
cat /etc/issue|mail -s "warning,please notice you sick" test
echo "successful"
fi
done
for I in `df -i|grep "^/dev/sd" | cut -c 41-42`;do
echo $I
if [ $I -ge 5 ];then
cat /etc/fstab|mail -s "warning,please notice you sick" test
echo "congratulation"
fi
done
11、指定文件做为参数,判断文件是否为.sh后缀,如果是,添加x权限
#!/bin/bash
#
read -p "please input a filename: " filename
if [[ $filename =~ .*\.sh ]];then
chmod a+x $filename
echo "funished"
else
exit
fi
12、判断输入的IP是否为合法IP
#!/bin/bash
#
ipaddr=‘^(\<([0-9]|[1-9][0-9]|1[0-9]{2}|2([0-4][0-9]|5[0-5]))\>\.){3}\<([0-9]|[1-9][0-9]|1[0-5][1-9]|2([0-4][0-9]|25[0-5]))\>$‘
read -p "please input a IPV4 addr: " ipv4
if [[ $ipv4 =~ $ipaddr ]];then
echo "a legal IP."
else
echo "unlegal IP"
exit
fi
14、输入起始值A和最后值B,计算从A+(A+1)...+(B-1)+B的总和
#!/bin/bash
#
sum=0
for I in seq $1 $2;do
let sum+=$I
done
echo $sum本文出自 “mylinux” 博客,请务必保留此出处http://luxiangyu.blog.51cto.com/9976123/1836780
原文:http://luxiangyu.blog.51cto.com/9976123/1836780