码迷,mamicode.com
首页 > 其他好文 > 详细

检测局域网内主机存活脚本

时间:2016-01-17 16:18:30      阅读:250      评论:0      收藏:0      [点我收藏+]

标签:ip;局域网


    我们在对局域网的网络情况进行维护时,经常需要收集网络中存活的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四月天” 博客,请务必保留此出处http://msiyuetian.blog.51cto.com/8637744/1735804

检测局域网内主机存活脚本

标签:ip;局域网

原文地址:http://msiyuetian.blog.51cto.com/8637744/1735804

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!