在运维日常工作中,shell 脚本堪称运维神器。随着 AI 大模型的迅猛发展,其智能编码能力让脚本实现质的飞跃,大模型写出来的脚本,也需要经验丰富的运维工程师把关审核,确保脚本的安全性、稳定性和适配性,本次分享 10 个使用的 shell 脚本案例,助你在工作中灵活运用,提升运维效率。
1、检测两台服务器制定目录下的文件一致性
######################################检测两台服务器指定目录下的文件一致性######################################通过对比两台服务器上文件的md5值,达到检测一致性的目的dir=/data/webb_ip=192.168.88.10#将指定目录下的文件全部遍历出来并作为md5sum命令的参数,进而得到所有文件的md5值,并写入到指定文件中find $dir -type f|xargs md5sum > /tmp/md5_a.txtssh $b_ip "find $dir -type f|xargs md5sum > /tmp/md5_b.txt"scp $b_ip:/tmp/md5_b.txt /tmp#将文件名作为遍历对象进行一一比对for f in `awk '{print 2} /tmp/md5_a.txt'`do#以a机器为标准,当b机器不存在遍历对象中的文件时直接输出不存在的结果if grep -qw "$f" /tmp/md5_b.txtthenmd5_a=`grep -w "$f" /tmp/md5_a.txt|awk '{print 1}'`md5_b=`grep -w "$f" /tmp/md5_b.txt|awk '{print 1}'`#当文件存在时,如果md5值不一致则输出文件改变的结果if [ $md5_a != $md5_b ]thenecho "$f changed."fielseecho "$f deleted."fidone
2、定义一个颜色输出字符串函数
#方法1:function echo_color() {if [ $1 == "green" ]; thenecho -e "[32;40m$2[0m"elif [ $1 == "red" ]; thenecho -e "[31;40m$2[0m"fi}#方法2:function echo_color() {case $1 ingreen)echo -e "[32;40m$2[0m";;red)echo -e "[31;40m$2[0m";;*)echo "Example: echo_color red string"esac}#使用方法:echo_color green "test"
function 关键字定义一个函数,可加或不加。
3、从 FTP 服务器下载文件
if [ $# -ne 1 ]; thenecho "Usage: $0 filename"fidir=$(dirname $1)file=$(basename $1)ftp -n -v << EOF # -n 自动登录open 192.168.1.10 # ftp服务器user admin passwordbinary # 设置ftp传输模式为二进制,避免MD5值不同或.tar.gz压缩包格式错误cd $dirget "$file"EOF
4、检查软件包是否安装
if rpm -q sysstat &>/dev/null; thenecho "sysstat is already installed."elseecho "sysstat is not installed!"fi
5、检查服务状态
PORT_C=$(ss -anu |grep -c 123)PS_C=$(ps -ef |grep ntpd |grep -vc grep)if [ $PORT_C -eq 0 -o $PS_C -eq 0 ]; thenecho "内容" | mail -s "主题" [email protected]fi
6、检查主机存活状态
方法1:将错误IP放到数组里面判断是否ping失败三次
IP_LIST="192.168.18.1 192.168.1.1 192.168.18.2"for IP in $IP_LIST; doNUM=1while [ $NUM -le 3 ]; doif ping -c 1 $IP > /dev/null; thenecho "$IP Ping is successful."breakelse# echo "$IP Ping is failure $NUM"FAIL_COUNT[$NUM]=$IPlet NUM++fidoneif [ ${#FAIL_COUNT[*]} -eq 3 ];thenecho "${FAIL_COUNT[1]} Ping is failure!"unset FAIL_COUNT[*]fidone
方法2:将错误次数放到FAIL_COUNT变量里面判断是否ping失败三次
IP_LIST="192.168.18.1 192.168.1.1 192.168.18.2"for IP in $IP_LIST; doFAIL_COUNT=0for ((i=1;i<=3;i++)); doif ping -c 1 $IP >/dev/null; thenecho "$IP Ping is successful."breakelse# echo "$IP Ping is failure $i"let FAIL_COUNT++fidoneif [ $FAIL_COUNT -eq 3 ]; thenecho "$IP Ping is failure!"fidone
ping_success_status() {if ping -c 1 $IP >/dev/null; thenecho "$IP Ping is successful."continuefi}IP_LIST="192.168.18.1 192.168.1.1 192.168.18.2"for IP in $IP_LIST; doping_success_statusping_success_statusping_success_statusecho "$IP Ping is failure!"done
7、监控CPU、内存和硬盘利用率
DATE=$(date +%F" "%H:%M)IP=$(ifconfig eth0 |awk -F [ :]+ /inet addr/{print $4} ) # 只支持CentOS6MAIL="[email protected]"if ! which vmstat &>/dev/null; thenecho "vmstat command no found, Please install procps package."exit 1fiUS=$(vmstat |awk NR==3{print $13} )SY=$(vmstat |awk NR==3{print $14} )IDLE=$(vmstat |awk NR==3{print $15} )WAIT=$(vmstat |awk NR==3{print $16} )USE=$(($US+$SY))if [ $USE -ge 50 ]; thenecho "Date: $DATEHost: $IPProblem: CPU utilization $USE" | mail -s "CPU Monitor" $MAILfi
2)内存
DATE=$(date +%F" "%H:%M)IP=$(ifconfig eth0 |awk -F [ :]+ /inet addr/{print $4} )MAIL="[email protected]"TOTAL=$(free -m |awk /Mem/{print $2} )USE=$(free -m |awk /Mem/{print $3-$6-$7} )FREE=$(($TOTAL-$USE))# 内存小于1G发送报警邮件if [ $FREE -lt 1024 ]; thenecho "Date: $DATEHost: $IPProblem: Total=$TOTAL,Use=$USE,Free=$FREE" | mail -s "Memory Monitor" $MAILfi
3)硬盘
#方法1:function echo_color() {if [ $1 == "green" ]; thenecho -e "[32;40m$2[0m"elif [ $1 == "red" ]; thenecho -e "[31;40m$2[0m"fi}#方法2:function echo_color() {case $1 ingreen)echo -e "[32;40m$2[0m";;red)echo -e "[31;40m$2[0m";;*)echo "Example: echo_color red string"esac}#使用方法:echo_color green "test"0
8、批量主机磁盘利用率监控
前提监控端和被监控端SSH免交互登录或者密钥登录。写一个配置文件保存被监控主机SSH连接信息,文件内容格式:IP User Port
#方法1:function echo_color() {if [ $1 == "green" ]; thenecho -e "[32;40m$2[0m"elif [ $1 == "red" ]; thenecho -e "[31;40m$2[0m"fi}#方法2:function echo_color() {case $1 ingreen)echo -e "[32;40m$2[0m";;red)echo -e "[31;40m$2[0m";;*)echo "Example: echo_color red string"esac}#使用方法:echo_color green "test"1
9、检查网站可用性
1)检查URL可用性
#方法1:function echo_color() {if [ $1 == "green" ]; thenecho -e "[32;40m$2[0m"elif [ $1 == "red" ]; thenecho -e "[31;40m$2[0m"fi}#方法2:function echo_color() {case $1 ingreen)echo -e "[32;40m$2[0m";;red)echo -e "[31;40m$2[0m";;*)echo "Example: echo_color red string"esac}#使用方法:echo_color green "test"2使用方法:check_url www.baidu.com
2)判断三次URL可用性
思路与上面检查主机存活状态一样。
#方法1:function echo_color() {if [ $1 == "green" ]; thenecho -e "[32;40m$2[0m"elif [ $1 == "red" ]; thenecho -e "[31;40m$2[0m"fi}#方法2:function echo_color() {case $1 ingreen)echo -e "[32;40m$2[0m";;red)echo -e "[31;40m$2[0m";;*)echo "Example: echo_color red string"esac}#使用方法:echo_color green "test"3方法2:错误次数保存到变量
#方法1:function echo_color() {if [ $1 == "green" ]; thenecho -e "[32;40m$2[0m"elif [ $1 == "red" ]; thenecho -e "[31;40m$2[0m"fi}#方法2:function echo_color() {case $1 ingreen)echo -e "[32;40m$2[0m";;red)echo -e "[31;40m$2[0m";;*)echo "Example: echo_color red string"esac}#使用方法:echo_color green "test"4#方法1:function echo_color() {if [ $1 == "green" ]; thenecho -e "[32;40m$2[0m"elif [ $1 == "red" ]; thenecho -e "[31;40m$2[0m"fi}#方法2:function echo_color() {case $1 ingreen)echo -e "[32;40m$2[0m";;red)echo -e "[31;40m$2[0m";;*)echo "Example: echo_color red string"esac}#使用方法:echo_color green "test"510、检查MySQL主从同步状态
#方法1:function echo_color() {if [ $1 == "green" ]; thenecho -e "[32;40m$2[0m"elif [ $1 == "red" ]; thenecho -e "[31;40m$2[0m"fi}#方法2:function echo_color() {case $1 ingreen)echo -e "[32;40m$2[0m";;red)echo -e "[31;40m$2[0m";;*)echo "Example: echo_color red string"esac}#使用方法:echo_color green "test"6原文 https://mp.weixin.qq.com/s/6YMJfIupbeq69J9xSgivvQ
推荐站内搜索:最好用的开发软件、免费开源系统、渗透测试工具云盘下载、最新渗透测试资料、最新黑客工具下载……




还没有评论,来说两句吧...