1.判断当前主机的CPU生产商,其信息在/proc/cpuinfo文件中vendor_id一行中如果其生产商为GenuineIntel,就显示其为Intel公司;如果其生产商AuthenticAMD,就显示其为AMD公司;否则,就显示无法识别;
[root@server ~]# vim 3.sh
#!/bin/bash
#########################
#File name:3.sh
#Version:v1.0
#Email:admin@test.com
#Created time:2022-08-20 20:06:32
#Description:
#########################
# 定义vendor,提取到本机的生厂商
vendor=$(cat /proc/cpuinfo | grep "vendor_id" | uniq |awk -F: '{print $2}' )
echo "$vendor"
#进行条件判断,使用了通配符
if [[ "$vendor" =~ [[:space:]]*GenuineIntel$ ]];then
echo "intel"
elif [[ "$vendor" =~ [[:space:]]*AuthenticAMD$ ]];then
echo "AMD"
else
echo "unknow"
fi
#然后运行脚本
[root@server ~]# bash 3.sh
GenuineIntel
intel
2.根据用户输入成绩,判断优良中差(A,B,C,D, 注意边界问题)
85-100 优秀–A
70-84 良好–B
60-69 合格–C
60分以下不合格–D
read -p "please input your score : " score
if [ -z "$score" ];then
echo "you must input your score"
exit 1
fi
expr $score + 10 &>/dev/null
exit_code=$?
if test $exit_code -ne 0;then
echo "please input a number(type)"
exit 2
fi
if [ "$score" -lt 0 -o "$score" -gt 100 ];then
echo "score invalid"
exit 3
fi
if [ "$score" -ge 85 ];then
echo "your score is A"
elif [ "$score" -ge 70 ];then
echo "your score is B"
elif [ "$score" -ge 60 ];then
echo "your score is C"
else
echo "your sorce is D"
fi
#测试代码,可以看出无论是输入空格还是字母还是不在范围内的数字都可以执行
[root@server ~]# bash 5.sh
please input your score : asd
please input a number(type)
[root@server ~]# bash 5.sh
please input your score :
you must input your score
[root@server ~]# bash 5.sh
please input your score : 5154
score invalid
[root@server ~]# bash 5.sh
please input your score : 11
your sorce is D
[root@server ~]# bash 5.sh
please input your score : 60
your score is C
[root@server ~]# bash 5.sh
please input your score : 85
your score is A
[root@server ~]# bash 5.sh
please input your score : 99
your score is A
[root@server ~]# bash 5.sh
3.判断 sshd 进程是否运行,如果服务启动打印启动,未启动则打印未启动(使用查看进程和端口两种方式)
方法1:查看进程
[root@server ~]# ps -ef | grep sshd | grep -v grep | wc -l
3
方法2:查看端口
[root@server ~]# ss -lntup | grep -w 22| wc -l
2
[root@server ~]# netstat -lntup | grep -w 22 | wc -l
2
```[root@server ~]# bash if_script2.sh
SSH is Running
[root@server ~]# cat if_script2.sh
#!/bin/bash
#########################
#File name:if_script2.sh
#Version:v1.0
#Email:admin@test.com
#Created time:2022-08-18 10:58:15
#Description:
#########################
result=`ps -ef | grep -w 22 | wc -l`
if test $result -ge 1;then
echo "SSH is Running"
else
echo "sshd is not running"
fi
4.检查主机是否存活,并输出结果(使用for循环实现:主机数>2)
[root@server ~]# vim 6.sh
for ip in 192.168.140.{129..131}
do
ping -c 2 -W 1 $ip &> /dev/null
if [ $? -eq 0 ];then
echo host $ip is running
else
echo host $ip is not running
fi
done
#测试
[root@server ~]# bash 6.sh
host 192.168.140.129 is running
host 192.168.140.130 is running
host 192.168.140.131 is not running
#可以看出在这个网段中有两台主机存活
5.编写脚本,判断当前系统剩余内存大小,如果低于100M,邮件报警管理员,使用计划任务,每10分钟检查一次。
#先定义一个shell脚本
[root@server ~]# vim 2.sh
#内容如下
free_mem=$(free -m | grep "Mem:" | tr -s " " | cut -d " " -f4)
if [ "$free_mem" -le 1000 ]; then
echo "剩余内存:${free_mem},低于1000M" | mail -s "内存警报" zhangsan
fi
#然后在crontab -e编写计划任务
*/2 * * * * /root/2.sh &>/dev/null
#我这位为了方便测试定义的每隔两分钟检查一次
测试:
在我的zhangsan用户下使用mail命令查看邮件可以看出每个两分钟会受到一个”内存警报“邮件(第一封邮件是我自己使用bash 2.sh测试脚本能否正常运行时发送的邮件)