微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐