1思路:sar -n DEV 1 10监测网卡进出流量,如果进出流量都为0则网卡异常 2#!/bin/bash 3LANG=en#定义语言 4if [ $# -ne 1 ] || ! ip addr show $1 &>/dev/null;then 5#输入参数是否为1, ! ip addr show判断网卡是否存在 6 echo "Please input correct network card." 7 exit 8fi 9echo "Checking..."#输出检测信息 10sar_in=`sar -n DEV 1 10 |grep " $1 "|grep -i average:|awk '{print $5}'` 11#进网卡流量 12sar_out=`sar -n DEV 1 10 |grep " $1 "|grep -i average:|awk '{print $6}'` 13#出网卡流量 14if [ "$sar_in" == "0.00" ] && [ "$sar_out" == "0.00" ];then 15#判断进出网卡流量是否都为0 16 while true#while 循环执行判断是否重启网卡 17 do 18 read -p "The network card is down,do you want to restart it?(y or n) " net 19 case $net in 20 y) 21 echo "Restarting..."#输出重启信息 22 ifdown $1 2>/dev/null 23 ifup $1 2>/dev/null 24 if [ $? -eq 0 ];then#判断重启是否成功 25 echo "The network card was restarted successfully!" 26 break#退出循环 27 else 28 echo "DEV $1 failed to start!" 29 break#退出循环 30 fi 31 ;; 32 n) 33 echo "Bye!" 34 exit 1 35 ;; 36 *) 37 echo "Please input y or n." 38 continue#重新循环 39 ;; 40 esac 41 done 42else 43 echo "The network card is in good condition!" 44fi 45使用 :sh dev_sar.sh eth0
50.检测网卡状态脚本
Wesley13
2021-10-11
1215 0 0
点赞
收藏
评论区
加载中...