今日份的课题:
1.判断当前主机的CPU生产商,其信息在/proc/cpuinfo文件中vendor_id一行中
2.根据用户输入成绩,判断优良中差(A,B,C,D, 注意边界问题)
3.判断 sshd 进程是否运行,如果服务启动打印启动,未启动则打印未启动(使用查看进程和端口两种方式)
4.检查主机是否存活,并输出结果(使用for循环实现:主机数>=2)
5.编写脚本,判断当前系统剩余内存大小,如果低于100M,邮件报警管理员,使用计划任务,每10分钟检查一次。
如何判断当前主机的CPU生产商
首先通过命令
cat /proc/cpuinfo |grep vendor_id|cut -d":" -f2
获得CPU厂商
再将获得的结果通过正则表达式进行匹配
$vendor =~ [[:space:]]*GenuineIntel$
$vendor =~ [[:space:]]*GenuineIntel$
最终脚本如图:
#!/bin/bash
#########################
#File name:decide_cpu_vendor.sh
#Version:v1.0
#Email:admin@test.com
#Created time:2022-08-19 01:15:47
#Description:
#########################
vendor=`cat /proc/cpuinfo |grep vendor_id|cut -d":" -f2`
if [[ $vendor =~ [[:space:]]*GenuineIntel$ ]]
then echo "intel"
elif [[ $vendor =~ [[:space:]]*AuthenticAMD$ ]]
then echo 'AMD'
else
echo 'other'
fi
根据用户输入成绩,判断优良中差(A,B,C,D, 注意边界问题)
首先判断输入是否为空
再判断其类型
最后将其划分
#!/bin/bash
#########################
#File name:decide_cpu_vendor.sh
#Version:v1.0
#Email:admin@test.com
#Created time:2022-08-19 01:15:47
#Description:
#########################
read -p "please input your grade:" score
expr $score + 1 &> /dev/null
return_grade=$?
if [[ -z $score ]]
then echo "no space!!!";exit 1
elif ! [[ $return_grade == 0 ]]
then echo "just number!!!";exit 2
elif [[ $score>100 || $score<0 ]]
then echo "please input your grade ps:0-100"
else
echo "this is your garde: $score"
fi
case $score in
8[5-9]|9[0-9]|100)
echo "this is your level A"
;;
7[5-9]|8[0-4])
echo "this is your level B"
;;
6[0-9]|7[0-4])
echo "this is your level C"
;;
*)
echo "this is your level D"
esac
当输入为空时,如图:
当输入为其他时,如图:
当输入正确时,如图:
判断 sshd 进程是否运行,如果服务启动打印启动,未启动则打印未启动(使用查看进程和端口两种方式)
sshd_pro=`ps -ef|grep sshd|grep -v grep|wc -l`查看进程
sshd_port=`netstat -lntup | grep -w 22 | wc -l` 查看端口
倘若数量大于0则表示sshd正在运行
#!/bin/bash
#########################
#File name:sshd_running.sh
#Version:v1.0
#Email:admin@test.com
#Created time:2022-08-19 02:03:26
#Description:
#########################
sshd_pro=`ps -ef|grep sshd|grep -v grep|wc -l`
sshd_port=`netstat -lntup | grep -w 22 | wc -l`
if [[ $sshd_pro > 0 || $sshd_port > 0 ]]
then echo "sshd is running"
else
echo 'sshd not runnung'
fi
编写脚本,判断当前系统剩余内存大小,如果低于100M,邮件报警管理员,使用计划任务,每10分钟检查一次。
通过free -m|grep Mem|tr -s " "|cut -d" " -f4获取剩余内存大小
通过free memory $size less then 200 " | mail -s "warning" 121xxx@qq.com将邮件发送到邮箱
#!/bin/bash
#########################
#File name:save_size.sh
#Version:v1.0
#Email:admin@test.com
#Created time:2022-08-19 02:25:48
#Description:
#########################
size=`free -m|grep Mem|tr -s " "|cut -d" " -f4`
echo $size
if [ $size -lt 200 ]
then echo " free memory $size less then 200 " | mail -s "warning" 121xxx@qq.com
fi