用shell脚本实现ping网段中的所有主机,代码如下:
复制代码代码示例:
#!/bin/sh
# ping all host # find from /etc/hosts for host info,and filter IP address cat /etc/hosts | grep -v ^# |grep -v ^$ | while read LINE do for M in `awk '{print $1}'` do ping -c1 $M done done 以上脚本比较简单,对在/etc/hosts/中配置的主机,进行单次ping检测。
代码说明:
调用示例,如下图:
对以上脚本进行改进,以实现如下的功能:
改进后的脚本如下:
复制代码代码示例:
#!/bin/sh
# ping all host # find from /etc/hosts for host info,and filter IP address cat /etc/hosts | grep -v ^# |grep -v ^$ | while read LINE do for M in `awk '{print $1}'` do if ping -w 1 -c 1 $M | grep "100%" >/dev/null then echo "$M is down" else echo "$M is up" fi done done
调用示例,如下图: |