一次检查多个接口

Check multiple interfaces at one time

#!/bin/bash

if /sbin/ethtool eth0 | grep -q "Link detected: yes"; then
    echo "The Link is up and online on $(hostname) as on $(date)" |
    mail -s "Network: Link is up" user@gmail.com

else
    echo "Link Detected = NO, Please check" | mail -s "Network: Link is down" user@gmail.com
fi

从上面的代码我可以看到一个接口是否已启动,但我需要的是我想知道是否所有接口都已启动,即“eth0、eth1、eth2”。

标准规则:使用for-循环重复多次但值不同。


Bash 中看起来像这样:

#!/bin/bash

text=""
correct=0
error=0

for DEV in eth0 eth1 eth2;
do
  if /sbin/ethtool $DEV | grep -q "Link detected: yes"; then
    text=$text"$DEV: The Link is up and online on $(hostname) as on $(date)\n"
    correct=$(($correct+1))
  else
    text=$text"$DEV: Link Detected = NO, Please check\n"
    error=$(($error+1))
  fi
done  

echo -e $text | mail -s "Network: correct $correct, error $error" user@gmail.com