1. ping主机测试
ping -w 1 $1 &>/dev/null
if [ $? -eq 0 ];then
echo "$1 ok"
else
echo "$1 no"
fi
2. 判断一个用户是否存在
id $1 &>/dev/null
if [ $? -eq 0 ];then
echo ""$1"存在"
else
echo ""$1"不存在"
fi
判断当前内核主版本是否为3,且次版本是否大于10
a1=`uname -r|awk -F'.' '{print $1}'`
a2=`uname -r|awk -F'.' '{print $2}'`
if [ $a1 -eq 3 ];then
echo "当前内核主版本为3"
else
echo "当前内核主版本不为3,为$a1"
fi
if [ $a2 -ge 10 ];then
echo "当前次版本大于10,为$a2"
else
echo "当前次版本小于10,为$a2"
fi
4. 判断vsftpd软件包是否安装,如果没有则自动安装#! /bin/bash
rpm -qc vsftpd &>/dev/null
if [ $? -ne 0 ];then
echo "本机没有安装vsftpd"
echo "等待2秒,开始安装"
sleep 2
while :
do
yum -y install vsftpd
if [ $? -eq 0 ];then
echo "vsftpd安装成功"
break
else
yum -y install vsftpd
fi
done
else
echo "本机有vsftpd"
fi
5. 判断httpd是否运行
ps -axu| grep [h]ttpd &>/dev/null
if [ $? -eq 0 ];then
echo "http正在运行"
else
echo "httpd没有运行"
read -p "请问需要运行吗Y|N:" num
case $num in
Y|y)
systemctl start httpd
if [ $? -eq 0 ];then
echo "启动完毕"
else
echo "请手动检查是否安装httpd"
fi
;;
N|n)
echo "正在退出"
exit 0
;;
esac
fi
6. 判断: 如果vsftpd启动,输出以下信息:#! /bin/bash
dk=`ss -tnlp|grep vsftp|awk '{print $4}'|awk -F':' '{print $4}'`
pid=`ps -axu | grep [v]sftpd|awk '{print $2}'`
ps -axu | grep [v]sftpd &>/dev/null
if [ $? -eq 0 ];then
echo "ftp已启动"
echo "监听的地址:"
echo "ftp监听的端口:$dk"
echo "ftp进程PID:$pid"
else
echo "ftp未启动"
fi
7. 报警脚本
sy=`df -Th|grep root|awk '{print $(NF-1)}'|awk -F'%' '{print $1}'`
sy1=`df -Th|grep root|awk '{print $(NF-1)}'`
shengyu=`expr 100 - $sy`
if [ $shengyu -le '20' ];then
echo "根分区剩余不足,已经使用$sy1"
else
echo "根分区ok"
fi
mem_used=`free -m | grep Mem | awk '{print $3}'`
mem_total=`free -m | grep Mem | awk '{print $2}'`
mem_percent=$((mem_used*100/mem_total))
if [ $mem_percent -gt 80 ]
then
echo "报警: 内存使用了$mem_percent %"
else
echo "内存正常"
fi
8. 判断用户输入的是否是数字
read -p "请输入数字:" num
if [[ $num =~ [[:digit:]] ]];then
echo "ok"
else
echo "no"
fi