企业内部网段检测有多少主机存活——shell脚本
2015-12-17 17:33
881 查看
企业内部网段检测有多少主机存活
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.sh4)操作后检查一下结果
[root@localhostscripts]# cat true.txt10.0.0.13....... [ OK ]
10.0.0.1....... [ OK ]
10.0.0.56....... [ OK ]
10.0.0.10....... [ OK ]
[root@localhostscripts]#
本文出自 “Linux高级运维之路” 博客,请务必保留此出处http://yulianhui.blog.51cto.com/10829691/1725766
相关文章推荐
- shell 括号学习
- win10系统Powershell重置应用失效的解决办法
- shell脚本中导入mysql数据&&执行mysql语句
- linux在shell中获取时间
- bash: useradd: command not found
- shell重定向调试信息
- 使用shell批处理进行不同参数运算
- shell小记:let
- bash的操作环境[转]
- 关于bash和dash
- Xshell使用教程及WinSCP使用教程
- shell脚本报错:-bash: xxx: /bin/sh^M: bad interpreter: No such file or directory
- Shell变量:Shell变量的定义、删除变量、只读变量、变量类型
- Linux下Shell编程解析
- Linux下Shell编程解析
- Shell 编程快速入门
- shell 命令之 find命令二
- shell 命令之 find 命令一
- .bash_profile和.bashrc的区别
- shell入门