企业内部网段检测有多少主机存活
1)通过ping命令来实现
ping -c 1 -w 1 ip
-c: 表示次数,1 为1次
-w: 表示deadline, time out的时间,单位为秒,1为1秒。
注: a)如果一个一个的去ping等待的时间很长,所以用到了多线程在后台执行 b)如果不用多线程的话有一个问题就是在跑for循环的时候ctrl +c只能停止当前循环的那个,停止之后会到下一个for循环,退不出来,还得再开一个终端把脚本的进程kill掉,不方便,这可以用trap解决 c)如果出来的结果的关键字是【确定】,最好LANG=EN改为英文,不然日后出问题也不好排错,还是用英文的吧,这样截取东西也方便 然后根据上面三条。搞成如下脚本... 入门级别都不算。
2)编写脚本
[root@localhost scripts]#cat check_hosts.sh #!/bin/bash ./etc/rc.d/init.d/functions ping="ping -w1 -c 1" ip="10.0.0." #trap "echo"bye-bye";exit" 2 for i in `seq 1 254` do { $ping ${ip}${i} >/dev/null if [$? -eq 0 ];then action "${ip}${i}......." /bin/true >>true.txt else action "${ip}${i}......." /bin/false >>false.txt fi }& done
3)执行脚本
[root@localhostscripts]#sh check_hosts.sh
4)操作后检查一下结果
[root@localhostscripts]# cat true.txt
10.0.0.13....... [ OK ]
10.0.0.1....... [ OK ]
10.0.0.56....... [ OK ]
10.0.0.10....... [ OK ]
[root@localhostscripts]#
转载于:https://blog.51cto.com/yulianhui/1725766