我们在对局域网的网络情况进行维护时,经常需要收集网络中存活的IP,这时候我们可以写一个shell脚本,自动收集某一网段的IP。执行该脚本,在线IP绿色显示,离线IP红色显示;并分别写入到指定IP状态列表文件中。
[root@centos ~]# cd /usr/local/bin/
[root@centos bin]# vim alive.sh
#!/bin/bash NETWORK=$1 for HOST in $(seq 1 254) do ping -c1 -w1 $NETWORK.$HOST &>/dev/null && result=0 || result=1 if [ "$result" == 0 ];then echo -e "\033[32;1m$NETWORK.$HOST is up \033[0m" echo "$NETWORK.$HOST" >> /tmp/up.txt else echo -e "\033[;31m$NETWORK.$HOST is down \033[0m" echo "$NETWORK.$HOST" >> /tmp/down.txt fi done |
赋予脚本执行权限:
[root@centos bin]# chmod +x alive.sh
查看71网段的所有IP状态:
[root@centos ~]# sh alive.sh 192.168.71
192.168.71.1 is up 192.168.71.2 is up 192.168.71.3 is down 192.168.71.4 is down 192.168.71.5 is up 192.168.71.6 is down 192.168.71.7 is up 192.168.71.8 is down 192.168.71.9 is down 192.168.71.10 is down 192.168.71.11 is up 192.168.71.12 is down 192.168.71.13 is up 192.168.71.14 is up 192.168.71.15 is down 192.168.71.16 is down 192.168.71.17 is down 192.168.71.18 is down 192.168.71.19 is up 192.168.71.20 is up 192.168.71.21 is up 192.168.71.22 is up 192.168.71.23 is up 192.168.71.24 is up 192.168.71.25 is down 192.168.71.26 is up ... ... |
我们也可以查看生成的IP状态列表文件
[root@centos bin]# cat /tmp/up.txt
[root@centos bin]# cat /tmp/down.txt
本文转自 M四月天 51CTO博客,原文链接:http://blog.51cto.com/msiyuetian/1735804,如需转载请自行联系原作者