前言:这个脚本初衷来自于之前看了网络班苟国齐的空间发布的脚本,今天改版了一下,感觉挺方便的。
环境简介:
IP地址段 | 外网 | 内网 |
10.0.0.0 | Yes | No |
172.16.1.0 | No | Yes |
1).编写脚本
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | [root@db02 oldboy20160103] # cat check_hosts.sh #!/bin/bash #no.1 Check functions [ ! -f /etc/ini .d /functions ] && source /etc/init .d /functions #no.2 judge $1 [ $ # -ne 1 ] && { echo "USAGE: $0 IPaddr" exit 1 } #no.3 PING= "ping -w1 -c 1" IP=$1 #no.4 for n in ` seq 10` do { $PING $1${n} &> /dev/null if [ $? - eq 0 ] then action "$1${n} is online" /bin/true else action "$1${n} is gone" /bin/false fi } done |
2).执行脚本
1 2 3 4 5 6 7 8 9 10 11 | [root@db02 oldboy20160103] # sh check_hosts.sh 10.0.0. 10.0.0.1 is online [ OK ] 10.0.0.2 is online [ OK ] 10.0.0.3 is gone [FAILED] 10.0.0.4 is gone [FAILED] 10.0.0.5 is gone [FAILED] 10.0.0.6 is gone [FAILED] 10.0.0.7 is online [ OK ] 10.0.0.8 is online [ OK ] 10.0.0.9 is gone [FAILED] 10.0.0.10 is gone [FAILED] |
提示:10.0.0.1 是我的NAT桥接地址,10.0.0.1 是我的NAT网关地址。
1 2 3 4 5 6 7 8 9 10 11 | [root@db02 oldboy20160103] # sh check_hosts.sh 172.16.1. 172.16.1.1 is gone [FAILED] 172.16.1.2 is gone [FAILED] 172.16.1.3 is gone [FAILED] 172.16.1.4 is gone [FAILED] 172.16.1.5 is gone [FAILED] 172.16.1.6 is gone [FAILED] 172.16.1.7 is online [ OK ] 172.16.1.8 is online [ OK ] 172.16.1.9 is gone [FAILED] 172.16.1.10 is gone [FAILED] |
本文出自 “Linux高级运维之路” 博客,请务必保留此出处http://yulianhui.blog.51cto.com/10829691/1732440