1、ping主机测试
2、判断一个用户是否存在
3、判断当前内核主版本是否为3,且次版本是否大于10
4、判断vsftpd软件包是否安装,如果没有则自动安装
5、判断httpd是否运行
6、判断指定的主机是否能ping通,必须使用$1变量
7、报警脚本,要求如下:
根分区剩余空间小于20%
内存已用空间大于80%
向用户alice发送告警邮件
配合crond每5分钟检查一次
[root@locaklhost ~]# echo “邮件正文” | mail -s “邮件主题” alice
8、判断用户输入的是否是数字
1、ping主机测试
# vim h1.sh
read -p "请输入IP地址:" ipadd
ping -c 2 $ipadd &>/dev/null
if [ $? -eq 0 ]
then
echo "ip地址 is ok.."
else
echo " ip地址 is fail.."
fi
2、判断一个用户是否存在
# vim h2.sh
read -p "请输入您需要查询的用户" user
id $user &>/dev/null
if [ $? -eq 0 ]
then
echo "存在"
else
echo "不存在"
fi
3、判断当前内核主版本是否为3,且次版本是否大于10
[root@centos7 ~]# vim homework.sh
#!/bin/sh
a=`uname -r | cut -d. -f1`
b=`uname -r | cut -d. -f2`
if [ $a -eq 3 ]
then
echo "main version is 3"
else
echo "main version is not 3"
fi
if [ $b -gt 10 ]
then
echo "minor version is more than 10"
else
echo "minor version is not more than 10"
fi
[root@centos7 ~]# sh homework.sh
main version is 3
minor version is not more than 10
4、判断vsftpd软件包是否安装,如果没有则自动安装
[root@centos7 ~]# vim homework2.sh
#!/bin/sh
a=vsftpd
rpm -qa | grep $a &>/dev/null
if [ $? -eq 0 ]
then
echo "installation package already exists"
else
echo "yum install -y vsftpd"
fi
[root@centos7 ~]# sh homework2.sh
installation package already exists
5、判断httpd是否运行
[root@centos7 ~]# vim homework3.sh
#!/bin/sh
a=`systemctl is-active httpd`
if [ "$a" = "active" ]
then
echo "activing"
else
echo "not runing"
fi
[root@centos7 ~]# sh homework3.sh
activing
[root@centos7 ~]# systemctl stop httpd
[root@centos7 ~]# sh homework3.sh
not runing
6、判断指定的主机是否能ping通,必须使用$1变量
[root@centos7 ~]# vim h1.sh
#!/bin/sh
ping -c 2 $1 &>/dev/null
if [ $? -eq 0 ]
then
echo "$1 is ok.."
else
echo " $1 is fail.."
fi
[root@centos7 ~]# sh h1.sh
is fail..
[root@centos7 ~]# sh h1.sh 192.168.126.134
192.168.126.134 is ok..
[root@centos7 ~]# sh h1.sh 192.168.126.133
192.168.126.133 is fail..
[root@centos7 ~]#
7、报警脚本,要求如下:
根分区剩余空间小于20%
内存已用空间大于80%
向用户alice发送告警邮件
配合crond每5分钟检查一次
[root@locaklhost ~]# echo “邮件正文” | mail -s “邮件主题” alice
[root@centos7 ~]# vim homework4.sh
#!/bin/sh
root_total=`free -m | tr -s " " | cut -d" " -f2 | head -2 | tail -1`
root_used=`free -m | tr -s " " | cut -d" " -f3 | head -2 | tail -1`
mem_used=`df -hP | tr -s " " | cut -d" " -f5 | head -2 | tail -1 | cut -d"%" -f1`
root=$[$root_used / $root_total]
if [ $root -gt 80 ]
then
echo "根分区剩余空间小于20%" | mail -s "报警信息" alice
elif [ $mem_used -gt 80 ]
then
echo "内存已用空间大于80%" | mail -s "报警信息" alice
else
echo "内存使用正常"
fi
[root@centos7 ~]# sh homework4.sh
内存使用正常
8、判断用户输入的是否是数字
[root@centos7 ~]# vim homework5.sh
#!/bin/sh
read -p "please enter:" input
expr $input + 1 >&/dev/null
if [ $? -eq 0 ]
then
echo "number"
else
echo "not number"
fi
[root@centos7 ~]# sh homework5.sh
please enter:67
number
[root@centos7 ~]# sh homework5.sh
please enter:&
not number