目录
1.判断当前主机的CPU生产商,其信息在/proc/cpuinfo文件中vendor_id一行中
3.判断 sshd 进程是否运行,如果服务启动打印启动,未启动则打印未启动
5.编写脚本,判断当前系统剩余内存大小,如果低于100M,邮件报警管理员,使用计划任务,每10分钟检查一次。
1.判断当前主机的CPU生产商,其信息在/proc/cpuinfo文件中vendor_id一行中
[root@localhost ~]# cat /proc/cpuinfo | grep vendor_id | cut -d ":" -f 2
GenuineIntel
输出/proc/cpuinfo信息扔到管道,grep匹配vendor_id并且扔到管道,cut用:隔开,取第二个区域
2.根据用户输入成绩,判断优良中差(A,B,C,D, 注意边界问题)
[root@localhost test1]# cat grade.sh
#!/bin/bash
#########################
#File name:grade.sh
#Version:v1.0
#Email:admin@test.com
#Created time:2022-08-18 17:28:47
#Description:
#########################
read -p "Please input a score: " score
#判断输入是否为空
[ -z "$score" ] && {
echo "Please input a score"
exit 1
}
#判断输入是否为数字
expr "$score" + 1 &>/dev/null
result=$?
[ "$result" -ne 0 ] && {
echo "Please input a score(type)"
exit 2
}
#判断数字范围
if ! [ "$score" -ge 0 -a "$score" -le 100 ];then
echo "Please input a score(1-100)"
elif [ "$score" -lt 60 ];then
echo "D"
elif [ "$score" -lt 70 ];then
echo "C"
elif [ "$score" -lt 85 ];then
echo "B"
else
echo "A"
fi
[root@localhost test1]# bash grade.sh
Please input a score:
Please input a score
[root@localhost test1]# bash grade.sh
Please input a score: a
Please input a score(type)
[root@localhost test1]# bash grade.sh
Please input a score: 23
D
[root@localhost test1]# bash grade.sh
Please input a score: 65
C
[root@localhost test1]# bash grade.sh
Please input a score: 100
A
[root@localhost test1]# bash grade.sh
Please input a score: 78
B
3.判断 sshd 进程是否运行,如果服务启动打印启动,未启动则打印未启动
法一:端口
[root@localhost test1]# cat if_ssh1.sh
#!/bin/bash
#########################
#File name:if_ssh1.sh
#Version:v1.0
#Email:admin@test.com
#Created time:2022-08-18 17:41:40
#Description:
#########################
#列出所有端口到管道里,只显示符合22的行并扔到管道,再用wc统计行数,赋值给var_a
var_a=`ss -lntup | grep -w 22 | wc -l `
#wc统计的行数大于0,代表有sshd服务
if [ "$var_a" -gt 0 ];then
echo "SSHD is activing"
else
echo "SSHD is not activing"
fi
[root@localhost test1]# bash if_ssh1.sh
SSHD is activing
法二:进程
[root@localhost test1]# cat if_ssh2.sh
#!/bin/bash
#########################
#File name:if_ssh2.sh
#Version:v1.0
#Email:admin@test.com
#Created time:2022-08-18 17:59:00
#Description:
#########################
#列出所有进程到管道,grep匹配sshd,再反转查找grep,最后统计行数
var_b=`ps -ef | grep sshd | grep -v grep | wc -l`
if [ "$var_b" -gt 0 ];then
echo "SSHD is activing"
else
echo "SSHD is not activing"
fi
[root@localhost test1]# bash if_ssh2.sh
SSHD is activing
4.检查主机是否存活,并输出结果
[kiosk@foundation0 test]$ cat computer.sh
#!/bin/bash
#ip取每一个IP地址做循环
for ip in 172.25.250.{9..15}
do
ping -w 2 $ip &>/dev/null
result=$?
#如果结果执行成功就输出ip地址
if [ "$result" -eq 0 ];then
echo $ip
fi
done
[kiosk@foundation0 test]$ bash computer.sh
172.25.250.9
172.25.250.10
172.25.250.11
172.25.250.12
172.25.250.13
5.编写脚本,判断当前系统剩余内存大小,如果低于100M,邮件报警管理员,使用计划任务,每10分钟检查一次。
[root@localhost test]# cat size.sh
#!/bin/bash
#########################
#File name:size.sh
#Version:v1.0
#Email:admin@test.com
#Created time:2022-08-18 19:56:15
#Description:
#########################
result=`free -m | grep Mem | tr -s " " | cut -d " " -f 4`
if [ "$result" -lt 100 ];then
echo "The size is ${result} < 100M" | mail -s "size" root@localhost
fi
[root@localhost test]# chmod a+rx size.sh
[root@localhost test]# crontab -e
*/10 * * * * .size.sh &>/dev/null