企业内部网段检测有多少主机存活
前言:这个脚本初衷来自于之前看了网络班苟国齐的空间发布的脚本,今天改版了一下,感觉挺方便的。
环境简介:
IP地址段 | 外网 | 内网 |
10.0.0.0 | Yes | No |
172.16.1.0 | No | Yes |
1).编写脚本
[[email protected] 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).执行脚本
[[email protected] 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网关地址。
[[email protected] 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]
时间: 2024-11-03 03:42:55