Linux很多比较好用的shell脚本程序用于监控服务器状态查询一些信息

本文提供了一系列用于检查和监控Linux系统状态的Shell脚本,包括系统信息查看、硬盘利用率监控、网络连通性测试等功能,适用于系统管理员日常维护工作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

查看linux系统信息

#!/bin/bash
#
#---------------------------------------
#Author:                    MartinHe
#Date:                      2019-03-16
#FileName:                  systeminfo.sh
#URL:                       https://blog.51cto.com/5033330 
#Desciption:                The test Scirpt
#Copyright(c)               2019 All rights reserved
#---------------------------------------
export Start_Color="\e[1;32m"
export End_Color="\e[0m"
Ipaddr=`ifconfig | grep -Eo --color=auto "(\<([1-9]|[1-9][0-9]|[1-9][0
-9]{2}|2[0-4][0-9]|25[0-5])\>\.){3}\<([1-9]|[1-9][0-9]|[1-9][0-9]{2}|2
[0-4][0-9]|25[0-5])\>"| head -1`
Version=`cut -d" "  -f4 /etc/redhat-release`
KerneVer=`uname -r`
Cpu_Info=`lscpu | grep "Model name:"|tr -s " " |cut -d: -f2`
Mem_Info=`free -mh|tr -s " "|cut -d" " -f2 |head -2| tail -1`
HD_Info=`lsblk | grep "^sd\{1,\}"| tr -s " "|cut -d" " -f1,4`
echo -e "The System Hostname is : $Start_Color `hostname` $End_Color"
echo -e "The System IP ADDER is : $Start_Color $Ipaddr $End_Color"
echo -e "The System Version  is : $Start_Color $Version $End_Color"   
echo -e "The System kerneVer is : $Start_Color $KerneVer $End_Color"
echo -e "The System Cpu_Info is :$Start_Color $Cpu_Info $End_Color"
echo -e "The System Mem_Info is : $Start_Color $Mem_Info $End_Color"
echo -e "The System HD_Info  is : $Start_Color \n$HD_Info $End_Color"
disk.sh
unset Start_Color
unset End_Color
unset Ipaddr
unset Version
unset KerneVer
unset Cpu_Info
unset Mem_Info
unset HD_Info

统计文件空行总数

#!/bin/bash
#
#---------------------------------------
#Author:                    MartinHe
#Date:                      2019-03-20
#FileName:                  argsum.sh
#URL:                       https://blog.51cto.com/5033330 
#Desciption:                The test Scirpt
#Copyright(c)               2019 All rights reserved
#---------------------------------------
if [ $# -lt 1 ];then
    echo "At least one argument(must be file)."
    exit 10
elif [ -e $1 ] && [ -f $1 ];then
    Space_Line=`grep "^$" $1 | wc -l`
    echo "the $1 has ${Space_Line} space line"                        
fi

 

检测硬盘利用率,如果超过百分之17就报警,并发送wall

#!/bin/bash                                                           
#
#---------------------------------------
#Author:                    MartinHe
#Date:                      2019-03-20
#FileName:                  checkdisk.sh
#URL:                       https://blog.51cto.com/5033330 
#Desciption:                The test Scirpt
#Copyright(c)               2019 All rights reserved
#---------------------------------------
Use_disk=`df |grep "^/dev/sd"| grep -Eo "\<[0-9]+\>%"|tr -d %|sort -nr
|head -1`
[ $Use_disk -ge 17 ] && wall "space will full"

 

某ip是否在线

ping -c1 -W1 $1 &> /dev/null && echo "$1 is up" || echo "$1 is donw"

显示两个文件空行的和

#!/bin/bash                                                                                                      
#
#---------------------------------------
#Author:                    MartinHe
#Date:                      2019-03-19
#FileName:                  sumspace.sh
#URL:                       https://blog.51cto.com/5033330 
#Desciption:                The test Scirpt
#Copyright(c)               2019 All rights reserved
#---------------------------------------
if [ $# -lt 2 ];then
    echo "Usage: sumspace.sh /PATH/TO/SOMEFILE /PATH/TO/SOMEFILE"
    exit 10
elif [ $# -gt 2 ];then
    echo "two argument only."
    exit 20
else
    FILE1=`grep "^$" $1|wc -l`
    FILE2=`grep "^$" $2|wc -l`
    SPACE_SUM=$[$FILE1+$FILE2]
    echo "The Space SUM is: $SPACE_SUM."
fi

 

 

选择YES or NO

#!/bin/bash
#
#---------------------------------------
#Author:                    MartinHe
#Date:                      2019-03-20
#FileName:                  yesorno.sh
#URL:                       https://blog.51cto.com/5033330 
#Desciption:                The test Scirpt
#Copyright(c)               2019 All rights reserved
#---------------------------------------
read -p "Do you agree?(yes or NO):" ANS
ans1=`echo $ANS | grep -E -i "^[y]([e][s])?$"|tr [:upper:] [:lower:]`
ans2=`echo $ANS | grep -E -i "^[n][o]$"|tr [:upper:] [:lower:]`
if [ "$ans1" = "yes" ];then
    echo "It's ok"
    exit
elif [ "$ans2" = "no" ];then
    echo "It's not ok"
    exit
else
    echo "Wrong input"
fi

 

批量改某路径下的文件名后缀(更改后的文件名粉色显示)

DIR=/data/script36/workdir/
cd $DIR
END=log
BC="\e[1;"
EC="\e[0m"
for file in *;do
    file_name=`echo $file | sed -r 's@^(.*)\..*$@\1@'`
    mv $file ${file_name}.$END
    echo -e "${BC}31m $file $EC is rename ${BC}35m ${file_name}.$END $EC success"                                                                 
done

 监控http服务运行状况(while)

#!/bin/bash
#
#---------------------------------------
#Author:                    MartinHe
#Date:                      2019-04-09
#FileName:                  monitor.httpd_while.sh
#URL:                       https://blog.51cto.com/5033330 
#Desciption:                The test Scirpt
#Copyright(c)               2019 All rights reserved
#---------------------------------------
SLEEPING=20
SERVICE=httpd
LOG=/var/log/monitor_$SERVICE.log
while true;do
    if killall -0 $SERVICE &> /dev/null;then
        true
    else
        systemctl restart $SERVICE
        echo "AT `date +'%F %T'` $SERVICE restart" | tee -a $LOG | mail -s "warning $SERVICE" root
    fi
    sleep $SLEEPING
done

 根据访问lastb,禁止10次以上ip访问(while)

#!/bin/bash
#
#---------------------------------------
#Author:                    MartinHe
#Date:                      2019-04-10
#FileName:                  denyip_while.sh
#URL:                       https://blog.51cto.com/5033330 
#Desciption:                The test Scirpt
#Copyright(c)               2019 All rights reserved
#---------------------------------------
LOG=cat /data/script36/ss.log | sed -nr '/^ESTAB/s@.* ([0-9.]+):[0-9]+.* ([0-9.]+):[0-9]+.*$@\2@p'| sort | uniq -c
cat $LOG | while read num ip;do
    if [ $num -gt 10 ];then
        iptables -A INPUT -s $ip -j REJECT
    fi
done

转载 https://blog.51cto.com/5033330/2367417 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值