1
、
ping
主机测试
,
查看主机是否存活;
#!/bin/bash
read -p "enter your id:" ip
ping -c 2 -W 1 $ip &> /dev/null
if [ $? -eq 0 ];then
echo "192.168.120.128 is runing"
else
echo "192.168.120.128 is not runing"
fi
2
、判断一个用户是否存在;
#!/bin/bash
read -p "Please enter a id name:" username
id -u $username &> /dev/null
if [ $? -eq 0 ];then
echo "the user exists"
else
echo "the user don't exists"
fi
3
、判断当前内核主版本是否为
3
,且次版本是否大于
10
;
#!/bin/bash
Version=`uname -r | cut -d "." -f1`
version=`uname -r | cut -d "." -f2`
if [ $Version -ne 3 ];then
echo "Your version not is version3"
fi
if [ $Version -eq 3 -a $version -le 10 ];then
echo "Your version is $Version.$version"
fi
if [ $Version -eq 3 -a $version -gt 10 ];then
echo "Your version exceeds $Version.$version"
fi
~
4
、判断
vsftpd
软件包是否安装,如果没有则自动安装;
rpm -qa | grep vsftpd &> /dev/null
if [ $? -eq 0 ];then
echo "this pakege is exist"
else
yum install vsftpd -y
fi
5
、判断
httpd
是否运行;
esult=`systemctl is-active httpd`
if [ "$result" = "inactive" ]
then
echo "httpd is runing"
else
echo "httpd is not runing"
fi
6
、判断指定的主机是否能
ping
通,必须使用
$1
变量;
ping -c 2 $1 &> /dev/null
if [ $? -eq 0 ]
then
echo "the ip is runing"
else
echo "the ip is not runing"
fi
7
、报警脚本,要求如下:
根分区剩余空间小于
20%
内存已用空间大于
80%
向用户
alice
发送告警邮件
配合
crond
每
5
分钟检查一次
[root@locaklhost
~
]
# echo "
邮件正文
" | mail -s "
邮件主题
" alice
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
8
、判断用户输入的是否是数字,如果是数字判断该数字是否大于
10
;
ead -p "Please enter:" i
expr $i + 1 &> /dev/null
if [ $? -eq 0 ]
then
echo "this is a number"
else
echo "this is not a number"
fi