1、ping主机测试
[root@localhost day3]# vim ping.sh
[root@localhost day3]# cat ping.sh
#! /bin/bash
read -p "请输入测试主机IP地址:" ip
ping -c2 $ip &> /dev/null
if [ $? -eq 0 ]
then
echo "success"
else
echo "fail"
fi
[root@localhost day3]# bash ping.sh
请输入测试主机IP地址:10.10.123.115
success
[root@localhost day3]# ping 10.10.123.115 -c2
PING 10.10.123.115 (10.10.123.115) 56(84) bytes of data.
64 bytes from 10.10.123.115: icmp_seq=1 ttl=128 time=0.500 ms
64 bytes from 10.10.123.115: icmp_seq=2 ttl=128 time=1.53 ms
[root@localhost day3]# bash ping.sh
请输入测试主机IP地址:10.10.123.110
fail
[root@localhost day3]# ping 10.10.123.110 -c2
--- 10.10.123.110 ping statistics ---
2 packets transmitted, 0 received, 100% packet loss, time 17ms
2、判断一个用户是否存在
[root@localhost day3]# vim user.sh
[root@localhost day3]# cat user.sh
#! /bin/bash
read -p "请输入用户名:" name
id $name &> /dev/null
if [ $? -eq 0 ]
then
echo "用户已存在"
else
[root@localhost day3]# bash user.sh
请输入用户名:xiaoming
用户不存
[root@localhost day3]# bash user.sh
请输入用户名:redhat
用户已存在
3、判断当前内核主版本是否为3,且次版本是否大于10
[root@localhost day3]# vim version.sh
[root@localhost day3]# cat version.sh
#! /bin/bash
version=`uname -r | cut -d"." -f1`
minversion=`uname -r | cut -d"." -f2`
if [ $version -eq 3 ]
then
echo "当前内核主版本为3"
if [ $minversion -gt 10 ]
then
echo "且次版本大于10"
else
echo "但次版本不大于10,次版本为:$minversion"
fi
else
echo "当前内核主版本不为3,主版本为:$version"
fi
[root@localhost day3]# bash version.sh
当前内核主版本不为3,主版本为:4
[root@localhost day3]# uname -r
4.18.0-193.el8.x86_64
4、判断vsftpd软件包是否安装,如果没有则自动安装
[root@localhost day3]# vim yum.sh
[root@localhost day3]# cat yum.sh
#! /bin/bash
rpm -q vsftpd &>/dev/null
if [ $? -eq 0 ]
then
echo "vsftpd软件包已安装"
else
mount /dev/sr0 /mnt &>/dev/null
yum install -y vsftpd &>/dev/null
if [ $? -eq 0 ]
then
echo "安装成功"
else
echo "安装失败"
fi
fi
[root@localhost day3]# bash yum.sh
安装成功
[root@localhost day3]# bash yum.sh
vsftpd软件包已安装
5、判断httpd是否运行
[root@localhost day3]# vim httpd.sh
[root@localhost day3]# cat httpd.sh
#! /bin/bash
rpm -q httpd &>/dev/null
if [ $? -ne 0 ]
then
echo "package httpd is not installed"
else
systemctl status httpd &>/dev/null
if [ $? -eq 0 ]
then
echo "running"
else
echo "down"
fi
fi
[root@localhost day3]# bash httpd.sh
down
[root@localhost day3]# systemctl start httpd
[root@localhost day3]# bash httpd.sh
running
6、判断指定的主机是否能ping通,必须使用$1变量
[root@localhost day3]# vim pingvar.sh
[root@localhost day3]# cat pingvar.sh
#! /bin/bash
[ -z $1 ] && echo "未输入ip" && exit 3
ping -c2 $1 &>/dev/null
if [ $? -eq 0 ]
then
echo "success"
else
echo "fail"
fi
[root@localhost day3]# bash pingvar.sh
未输入ip
[root@localhost day3]# bash pingvar.sh 10.10.123.115
success
[root@localhost day3]# bash pingvar.sh 10.10.123.110
fail
7、报警脚本,要求如下: 根分区剩余空间小于20% 内存已用空间大于80% 向用户alice发送告警邮件 配合crond每5分钟检查一次
[root@locaklhost ~]# echo “邮件正文” | mail -s “邮件主题” alice
[root@localhost day3]# vim mail.sh
[root@localhost day3]# cat mail.sh
#! /bin/bash
rpm -q postfix &>/dev/null
if [ $? -ne 0 ]
then
mount /dev/sr0 /mnt &>/dev/null
yum install -y postfix &>/dev/null
fi
id alice &>/dev/null
if [ $? -ne 0 ]
then
useradd alice &>/dev/null
fi
mem_total=$( free -m | grep "Mem:"|tr -s " "|cut -d" " -f2 )
mem_used=$( free -m | grep "Mem:"|tr -s " "|cut -d" " -f3 )
df_used=$( df -h / |grep /| tr -s " "|cut -d" " -f5 |cut -d"%" -f1)
val=` expr $mem_used / $mem_total `
if [ $df_used -gt 80 ]
then
echo "根分区剩余空间小于20%" | mail -s "warning" alice
fi
if [ $val -gt 80 ]
then
echo "内存已用空间大于80%" | mail -s "warning" alice
fi
[root@localhost day3]# crontab -l
5 * * * * /scripts/day3/mail.sh
8、判断用户输入的是否是数字
[root@localhost day3]# vim num.sh
[root@localhost day3]# cat num.sh
#! /bin/bash
read -p "请输入数字:" num
expr $num "+" 1 &>/dev/null
if [ $? -eq 0 ]
then
echo "$num 是数字"
else
echo "$num 不是数字"
fi
[root@localhost day3]# bash num.sh
请输入数字:22
22 是数字
[root@localhost day3]# bash num.sh
请输入数字:a
a 不是数字
[root@localhost day3]# bash num.sh
请输入数字:@
@ 不是数字