shell语言练习3

目录

1.判断当前主机的CPU生产商,其信息在/proc/cpuinfo文件中vendor_id一行中

2.根据用户输入成绩,判断优良中差(A,B,C,D, 注意边界问题)

3.判断 sshd 进程是否运行,如果服务启动打印启动,未启动则打印未启动(使用查看进程和端口两种方式)

4.检查主机是否存活,并输出结果(使用for循环实现:主机数>=2)

5.编写脚本,判断当前系统剩余内存大小,如果低于100M,邮件报警管理员,使用计划任务,每10分钟检查一次。


1.判断当前主机的cpu生产商,其信息在/proc/cpuinfo文件vendor_id一行中

[root@server shell]# vim cpu_type.sh

#result=AuthenticAMD

result=`cat /proc/cpuinfo |grep vendor_id |cut -d " " -f 3`

#if  [ $result == "AuthenticAMD" ]

if [[ $result =~ [[:space:]]*AuthenticAMD$ ]]

then

    echo "cputype:AMD"

#elif [  $result == "GenuineIntel" ]

elif [[ $result =~ [[:space:]]*GenuineIntel$ ]]

  then

    echo "cputype:intel"

else

    echo "unkNow"

fi

2.根据用户输入成绩,判断优良中差(A,B,C,D, 注意边界问题)

[root@server shell]# vim grade.sh

read -t 10 -p "input the grade:" grade

if [[ -z $grade ]]

  then

    echo "the grade can not be absent"

    exit 1

fi

expr $grade '+' 1 &> /dev/null

return=$?

if [[ $return != 0 ]]

  then

    echo "input a number"

    exit 2

fi

if [ $grade -lt 0 -o $grade -gt 100 ]

  then

  echo "input a number between 0 and 100"

  exit 3

fi

case $grade in

  8[5-9]|9[0-9]|100)

  echo A

  ;;

  7[0-9]|8[0-4])

  echo B

  ;;

  6[0-9])

  echo C

  ;;

  *)

  echo D

  ;;

esac

3.判断 sshd 进程是否运行,如果服务启动打印启动,未启动则打印未启动(使用查看进程和端口两种方式)

[root@server shell]# vim check_ssh.sh

result=`ps -ef |grep sshd |grep -v grep |wc -l `

if test $result -gt 1

then

  echo "sshd is running"

else

  echo "sshd is dead"

fi

count=`ss -lntup |grep -w 22 |wc -l`

if [ $count -lt 1 ]

then

  echo "sshd is dead"

else

  echo "sshd is running "

fi

4.检查主机是否存活,并输出结果(使用for循环实现:主机数>=2)

[root@server shell]# vim ping.sh

for i in {128..135}

do

  ping -c 3 -i 0.5 -W 1 192.168.110.$i &> /dev/null

  if test $? -eq 0

  then

    echo "192.168.110.$i is up"

  else

    echo "192.168.110.$i is down"

  fi

done

5.编写脚本,判断当前系统剩余内存大小,如果低于100M,邮件报警管理员,使用计划任务,每10分钟检查一次。

[root@server shell]# vim residue_cpu.sh

size=`free -m |grep Mem |tr -s " "|cut -d " " -f 4`

if [[ $size <=100 ]]

then

  echo "the memory is not enough" |mail -s "residue memory" zhao

fi

[root@server shell]# crontab -e

*/10 * * * * /shell/residue_cpu.sh

相关文章

显卡天梯图2024最新版,显卡是电脑进行图形处理的重要设备,...
初始化电脑时出现问题怎么办,可以使用win系统的安装介质,连...
todesk远程开机怎么设置,两台电脑要在同一局域网内,然后需...
油猴谷歌插件怎么安装,可以通过谷歌应用商店进行安装,需要...
虚拟内存这个名词想必很多人都听说过,我们在使用电脑的时候...
win11本地账户怎么改名?win11很多操作都变了样,用户如果想要...