实验1:判断当前主机的CPU生产商,其信息在/proc/cpuinfo文件中vendor_id一行中。 如果其生产商为GenuineIntel,就显示其为Intel公司; 如果其生产商为AuthenticAMD,就显示其为AMD公司; 否则,就显示无法识别;
[root@localhost ~]# vim CPU_zhuji.sh
endor=$(cat /proc/cpuinfo | grep vendor_id | cut -d ":" -f2 | uniq)
if [[ "$vendor" =~ [[:space:]]*AuthenticAMD ]];then
echo "AMD"
elif [[ "$vendor" =~ [[:space:]]*GenuineIntel ]];then
echo "intel"
else
echo "Unknow"
fi
[root@localhost ~]# bash CPU_zhuji.sh
AMD
实验2::根据用户输入成绩,判断优良中差。 85-100 优秀--A 70-84 良好--B 60-69 合格--C 60分以下不合格--D
[root@localhost ~]# vim score_panduan.sh
read -p "please input you score:" score
if [ -z "score" ];then
echo "please input you score(0~100):"
exit 1
fi
expr $score + 10 &>/dev/null
return_score=$?
if ! [ "$return_score" -eq 0 ];then
echo "please input you score(0~100):"
exit 2
fi
if [ "$score" -gt 100 -o "$score" -lt 0 ];then
echo "please input you score(0~100):"
exit 3
fi
if [ "$score" -le 100 -a "$score" -ge 85 ];then
echo "A"
exit 0
elif [ "$score" -lt 85 -a "$score" -ge 70 ];then
echo "B"
exit 0
elif [ "$score" -lt 70 -a "$score" -ge 60 ];then
echo "C"
exit 0
elif [ "$score" -le 69 ];then
echo "D"
exit 0
fi
root@localhost ~]# bash score_panduan.sh
please input you score:2
D
[root@localhost ~]# bash score_panduan.sh
please input you score:50
D
[root@localhost ~]# bash score_panduan.sh
please input you score:60
C
[root@localhost ~]# bash score_panduan.sh
please input you score:99
A
[root@localhost ~]# bash score_panduan.sh
please input you score:120
please input you score(0~100):
[root@localhost ~]#
实验3:判断 sshd 进程是否运行,如果服务未启动则启动相应服务
分析:判断进程是否运行
方法1:查看进程 [root@localhost test]# ps -ef | grep sshd | grep -v grep | wc -l
方法2:查看端口
[root@localhost test]# ss -lntup | grep -w 22| wc -l [root@localhost test3]# netstat -lntup | grep -w 22 | wc -l
[root@localhost ~]# vim sshd_process.sh
ssh_process=$(ps -ef | grep sshd | grep -v grep | wc -l)
if [ $sshd_process -eq 5 ];then
echo "sucess"
else
echo "false"
systemctl start sshd >/dev/null
fi
实验4:检查主机是否存活,并输出结果
[root@localhost ~]# vim ping_base.sh
[root@localhost ~]# chmod +rw ping_base.sh
for IP in 192.168.1.1 192.168.2.1 192.168.3.1
do
echo $IP
done
[root@localhost ~]# bash ping_base.sh
192.168.1.1
192.168.2.1
192.168.3.1
[root@localhost test3]# vim ping.sh
#!/bin/bash
ping -c 2 -W 1 192.168.168.128 &> /dev/null
if [ $? -eq 0 ];then
echo host 192.168.168.128 is running
else
echo host 192.168.168.128 is not running
fi
[root@localhost test3]# chmod a+rx ping.sh
[root@localhost test3]# ./ping.sh
host 192.168.168.128 is running
实验5:编写脚本,判断当前系统剩余内存大小,如果低于100M,邮件报警管理员,使用计划任务,每 10分钟检查一次
[root@localhost ~]# vim neicun_daxiao.sh
$free_mem=$(free -m | grep "Mem:" | tr -s " " | cut -d " " -f4)
if [ "$free_mem" -le 100 ];then
echo "剩余内存:${free_mem},低于100M" | mail -s "内存报警"
fi