思路:sar -n DEV 1 10監測網卡進出流量,若是進出流量都爲0則網卡異常 #!/bin/bash LANG=en#定義語言 if [ $# -ne 1 ] || ! ip addr show $1 &>/dev/null;then #輸入參數是否爲1, ! ip addr show判斷網卡是否存在 echo "Please input correct network card." exit fi echo "Checking..."#輸出檢測信息 sar_in=`sar -n DEV 1 10 |grep " $1 "|grep -i average:|awk '{print $5}'` #進網卡流量 sar_out=`sar -n DEV 1 10 |grep " $1 "|grep -i average:|awk '{print $6}'` #出網卡流量 if [ "$sar_in" == "0.00" ] && [ "$sar_out" == "0.00" ];then #判斷進出網卡流量是否都爲0 while true#while 循環執行判斷是否重啓網卡 do read -p "The network card is down,do you want to restart it?(y or n) " net case $net in y) echo "Restarting..."#輸出重啓信息 ifdown $1 2>/dev/null ifup $1 2>/dev/null if [ $? -eq 0 ];then#判斷重啓是否成功 echo "The network card was restarted successfully!" break#退出循環 else echo "DEV $1 failed to start!" break#退出循環 fi ;; n) echo "Bye!" exit 1 ;; *) echo "Please input y or n." continue#從新循環 ;; esac done else echo "The network card is in good condition!" fi 使用 :sh dev_sar.sh eth0