一分钟教你Liunx操作小技巧

 今日份的课题:

1.判断当前主机的CPU生产商,其信息在/proc/cpuinfo文件中vendor_id一行中
2.根据用户输入成绩,判断优良中差(A,B,C,D, 注意边界问题)
3.判断 sshd 进程是否运行,如果服务启动打印启动,未启动则打印未启动(使用查看进程和端口两种方式)
4.检查主机是否存活,并输出结果(使用for循环实现:主机数>=2)
5.编写脚本,判断当前系统剩余内存大小,如果低于100M,邮件报警管理员,使用计划任务,每10分钟检查一次。

如何判断当前主机的CPU生产商

首先通过命令

cat /proc/cpuinfo |grep vendor_id|cut -d":" -f2

获得CPU厂商

再将获得的结果通过正则表达式进行匹配

$vendor =~ [[:space:]]*GenuineIntel$

$vendor =~ [[:space:]]*GenuineIntel$

最终脚本如图:

#!/bin/bash
#########################
#File name:decide_cpu_vendor.sh
#Version:v1.0
#Email:admin@test.com
#Created time:2022-08-19 01:15:47
#Description:
#########################
vendor=`cat /proc/cpuinfo |grep vendor_id|cut -d":" -f2`
if [[ $vendor =~ [[:space:]]*GenuineIntel$ ]]
then echo "intel"
elif [[ $vendor =~ [[:space:]]*AuthenticAMD$ ]]
then echo 'AMD'
else
echo 'other'
fi

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

首先判断输入是否为空

再判断其类型

最后将其划分

#!/bin/bash
#########################
#File name:decide_cpu_vendor.sh
#Version:v1.0
#Email:admin@test.com
#Created time:2022-08-19 01:15:47
#Description:
#########################
read -p "please  input your  grade:" score
expr $score + 1 &> /dev/null
return_grade=$?
if [[ -z $score ]]
    then echo "no space!!!";exit 1
elif ! [[ $return_grade == 0 ]]
    then echo "just number!!!";exit 2
elif [[ $score>100 || $score<0  ]]
    then echo "please input your grade ps:0-100"
else
    echo "this is your garde: $score"
fi

case $score in
    8[5-9]|9[0-9]|100)
    echo "this is your level A"
    ;;
    7[5-9]|8[0-4])
    echo "this is your level B"
    ;;
    6[0-9]|7[0-4])
    echo "this is your level C"
    ;;
    *)
    echo "this is your level D"
esac

当输入为空时,如图:

当输入为其他时,如图:

当输入正确时,如图:

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

sshd_pro=`ps -ef|grep sshd|grep -v grep|wc -l`查看进程
sshd_port=`netstat -lntup | grep -w 22 | wc -l` 查看端口

倘若数量大于0则表示sshd正在运行

#!/bin/bash
#########################
#File name:sshd_running.sh
#Version:v1.0
#Email:admin@test.com
#Created time:2022-08-19 02:03:26
#Description:
#########################
sshd_pro=`ps -ef|grep sshd|grep -v grep|wc -l`
sshd_port=`netstat -lntup | grep -w 22 | wc -l`

if [[ $sshd_pro > 0 || $sshd_port > 0  ]]
    then echo "sshd is running"
else
    echo 'sshd not runnung'
fi

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

通过free -m|grep Mem|tr -s " "|cut -d" " -f4获取剩余内存大小

通过free  memory $size  less then 200 " | mail -s "warning" 121xxx@qq.com将邮件发送到邮箱

#!/bin/bash
#########################
#File name:save_size.sh
#Version:v1.0
#Email:admin@test.com
#Created time:2022-08-19 02:25:48
#Description:
#########################
size=`free -m|grep Mem|tr -s " "|cut -d" " -f4`
echo $size
if [ $size -lt 200  ]
    then echo " free  memory $size  less then 200 " | mail -s "warning" 121xxx@qq.com
fi

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值