1.判断当前主机的CPU生产商,其信息在/proc/cpuinfo文件中vendor_id一行中
[root@servera homework_818]# vim cpu.sh
cpu=`cat /proc/cpuinfo | grep vendor_id | cut -d ":" -f 2 | uniq`
if [[ $cpu =~ [:space:]*AuthenticAMD$ ]];then
echo "Your cpu is AMD"
elif [[ $cpu =~ [:space:]*GenuineIntel$ ]];then
echo "Your cpu is inter"
else
echo "unknow"
fi
2.根据用户输入成绩,判断优良中差(A,B,C,D, 注意边界问题)
[root@servera homework_818]# vim grade_evaluate.sh
echo "This script is grade evaluate "
read -p "Please input integer number :" score
if [ -z $score ];then
echo "score is null,please input your grade!"
exit 0
fi
expr $score + 6 &> /dev/null
s1=$?
if [ $s1 -ne 0 ];then
echo "type errors ,integer"
exit 1
fi
if [ $score -lt 0 -o $score -gt 100 ];then
echo "out of range!"
exit 2
fi
if [ $score -ge 85 ];then
echo "A"
elif [ $score -ge 75 ];then
echo "B"
elif [ $score -ge 60 ];then
echo "C"
else
echo "D"
fi
3.判断 sshd 进程是否运行,如果服务启动打印启动,未启动则打印未启动(使用查看进程和端口两种方式)
方式一:利用进程
[root@servera homework_818]# vim sshd_status.sh
this_pid=$$
ps -ef | grep sshd | grep -v "grep" | grep -v "$this_pid" > /dev/n
ull
if [ $? -eq 0 ];then
echo "sshd server is running! "
else
echo "sshd server is dead!"
fi
测试:
[root@servera homework_818]# bash sshd_status.sh
sshd server is running!
方式二:利用端口
[root@servera homework_818]# vim sshd_status_netstat.sh
ss -luntp | grep sshd > /dev/null
if [ $? -eq 0 ];then
echo "sshd server is running"
else
echo "sshd server is dead"
fi
测试:
[root@servera homework_818]# bash sshd_status_netstat.sh
sshd server is running
[root@servera homework_818]# systemctl stop sshd
[root@servera homework_818]# bash sshd_status_netstat.sh
sshd server is dead
**Tips:此题很有扩展空间,见文章最后**
4.检查主机是否存活,并输出结果(使用for循环实现:主机数>=2)
[root@servera homework_818]# vim ping_host_status.sh
for i in {0..2}
do
ping -c 3 -W 1 192.168.10.20"$i" &> /dev/null
if [ $? -eq 0 ];then
echo "192.168.10.20$i is running"
else
echo "192.168.10.20$i is dead"
fi
done
测试:
[root@servera homework_818]# bash ping_host_status.sh
192.168.10.200 is running
192.168.10.201 is running
192.168.10.202 is running
把192.168.10.201关闭
[root@servera homework_818]# bash ping_host_status.sh
192.168.10.200 is running
192.168.10.201 is dead
192.168.10.202 is running
5.编写脚本,判断当前系统剩余内存大小,如果低于100M,邮件报警管理员,使用计划任务,每 10分钟检查一次。
为了测试方便,我每一分钟检测一次,内存小于6000是发送邮件给syc用户
[root@servera homework_818]# vim free_mem.sh
mem=$(free -m | awk '/Mem:/ {print $4}')
if [ $mem -lt 6000 ];then
echo "warning: your compter memory is less than 100M" | mail -s "mem warning
" syc
fi
[root@servera homework_818]# crontab -e
*/1 * * * * /home/servera/ansible/homework_818/free_mem.sh &> /dev/null
[root@servera ~]# su - syc
[syc@servera ~]$ mail
Heirloom Mail version 12.5 7/5/10. Type ? for help.
"/var/spool/mail/syc": 3 messages 3 new
>N 3 root Fri Aug 19 01:58 21/757 "mem warning"
& 3
Message 3:
From root@servera Fri Aug 19 01:58:06 2022
Return-Path: <root@servera>
From: root <root@servera>
Date: Fri, 19 Aug 2022 01:57:01 +0800
To: syc@servera
Subject: mem warning
User-Agent: Heirloom mailx 12.5 7/5/10
Content-Type: text/plain; charset=us-ascii
Status: R
warning: your compter memory is less than 6000M
&
**
扩展题:
**
判断vsftpd包是否已经安装,如果已安装,则提示用户是否要启动该服务[针对于某些服务不会自动启动];
如果没有安装vsftpd,则提示用户是否安装,安装后提示用户是否需要启动该服务;
在vsftpd已安装的前提下,检测服务状态,如果处于dead,提示用户是否启动
[root@servera bash]# vim vsftp_install_test.sh
(rpm -qa | grep vsftpd)
if [ $? -eq 1 ];then
echo "==================================================================="
echo "vsftpd is not installed!"
read -p "You want to install vsftpd, yes or no ?" result
if [ $result == "yes" ];then
echo "vsftpd is installing ,Please wait for 5s..."
yum install vsftpd -y
sleep 5
echo "vsftpd is install successfully!"
read -p "Do you want start vsftpd service, yes or no ?" result_2
if [ $result_2 == "yes" ];then
echo "==================================================================="
echo "vsftpd starting , Please wait for 3s..."
systemctl start vsftpd
sleep 3
echo "vsftpd start successfully!"
else
echo "==================================================================="
echo "exited"
exit 3
fi
else
echo "==================================================================="
echo "exited"
exit
fi
else
echo "==================================================================="
this_pid=$$
(ss -luntp | grep vsftpd | grep -v "grep" | grep -v "$this_pid")
if [ $? -eq 1 ];then
read -p "vsftpd installed and active (dead) ,Do you want start vsftpd servic
e, yes or no ?" result_2
if [ $result_2 == "yes" ];then
echo "===================================================================
"
echo "vsftpd starting , Please wait for 3s..."
systemctl start vsftpd
sleep 3
echo "vsftpd start successfully!"
else
echo "===================================================================
"
echo "exited"
exit 4
fi
else
echo "==================================================================="
echo "vsftpd installed and active (running)"
exit 5
fi
fi
测试:
[root@servera bash]# bash vsftp_install_test.sh
===================================================================
vsftpd is not installed!
You want to install vsftpd, yes or no ? yes
.....
Complete!
vsftpd is install successfully!
Do you want start vsftpd service, yes or no ? yes
===================================================================
vsftpd starting , Please wait for 3s...
vsftpd start successfully!
[root@servera bash]# bash vsftp_install_test.sh
vsftpd-3.0.3-34.el8.x86_64
===================================================================
vsftpd installed and active (dead) ,Do you want start vsftpd service, yes or no ?yes
===================================================================
vsftpd starting , Please wait for 3s...
vsftpd start successfully!
[root@servera bash]# bash vsftp_install_test.sh
vsftpd-3.0.3-34.el8.x86_64
===================================================================
tcp LISTEN 0 32 *:21 *:* users:(("vsftpd",pid=11416,fd=3))
===================================================================
vsftpd installed and active (running)