我们在对局域网的网络情况进行维护时,经常需要收集网络中存活的IP,这时候我们可以写一个shell脚本,自动收集某一网段的IP。执行该脚本,在线的IP绿色显示,离线的IP红色显示;并分别写入到指定IP状态列表文件中。
[root@tpp ~]# cd /usr/local/bin/
[root@tpp 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@tpp bin]# chmod +x alive.sh
查看71网段的所有IP状态:
[root@tpp ~]# 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@tpp bin]# cat /tmp/up.txt
[root@tpp bin]# cat /tmp/down.txt
欢迎大家来我的51cto博客一起分享探讨Linux知识:http://msiyuetian.blog.51cto.com/
[root@tpp ~]# cd /usr/local/bin/
[root@tpp 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@tpp bin]# chmod +x alive.sh
查看71网段的所有IP状态:
[root@tpp ~]# 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@tpp bin]# cat /tmp/up.txt
[root@tpp bin]# cat /tmp/down.txt
欢迎大家来我的51cto博客一起分享探讨Linux知识:http://msiyuetian.blog.51cto.com/
0
echo 有显示颜色的功能,"\033[;31m \033[0m" 这一串就是显示颜色。具体的用法你可以参考这个网址:http://man.linuxde.net/,里面有颜色的介绍。
Rohero 发表于 2016-1-19 14:33
颜色怎么显示出来的
echo 有显示颜色的功能,"\033[;31m \033[0m" 这一串就是显示颜色。具体的用法你可以参考这个网址:http://man.linuxde.net/,里面有颜色的介绍。
编辑回复