我建议使用nmap的ping-scan标志,
$ nmap -sn 192.168.1.60-70Starting Nmap 4.11 ( http://www.insecure.org/nmap/ ) at 2009-04-09 20:13 BSTHost machine1.home (192.168.1.64) appears to be up.Host machine2.home (192.168.1.65) appears to be up.Nmap finished: 11 IP addresses (2 hosts up) scanned in 0.235 seconds
就是说,如果您想自己编写(足够公平),这就是我的方法:
for ip in 192.168.1.{1..10}; do ping -c 1 -t 1 $ip > /dev/null && echo "${ip} is up"; done..以及以上命令各部分的说明:
生成IP地址列表
例如,您可以使用
{1..10}语法生成数字列表。$ echo {1..10}1 2 3 4 5 6 7 8 9 10(它对于-之类的东西也很有用
mkdir {dir1,dir2}/{sub1,sub2}-使得dir1and
dir2,每个包含
sub1and
sub2)
因此,要生成IP列表,我们将执行以下操作
$ echo 192.168.1.{1..10}192.168.1.1 192.168.1.2 [...] 192.168.1.10循环
要在bash中循环播放某些内容,请使用
for:
$ for thingy in 1 2 3; do echo $thingy; done123
平
接下来,执行ping操作。ping命令会因不同的操作系统,不同的发行版/版本而有所不同(我目前正在使用OS X)
默认情况下(同样,在OS X版本上
ping),它将一直ping直到被中断为止,这将无法正常工作,因此
ping -c1将仅尝试发送一个数据包,这应该足以确定计算机是否已启动。
另一个问题是超时值,在此版本的ping上似乎是11秒。使用
-t标志更改了它。一秒钟应该足以查看本地网络上的计算机是否处于活动状态。
因此,我们将使用的ping命令是..
$ ping -c 1 -t 1 192.168.1.1PING 192.168.1.1 (192.168.1.1): 56 data bytes--- 192.168.1.1 ping statistics ---1 packets transmitted, 0 packets received, 100% packet loss
检查ping结果
接下来,我们需要知道机器是否答复。
&&如果第一个成功,我们可以使用运算符来运行命令,例如:
$ echo && echo "It works"It works$ nonexistantcommand && echo "This should not echo"-bash: nonexistantcommand: command not found
好,所以我们可以做..
ping -c 1 -t 1 192.168.1.1 &&回显“ 192.168.1.1已启动!”
另一种方法是使用ping的退出代码。ping命令将以退出代码0(成功)退出(如果成功),并且以非零代码退出(如果失败)。在bash中,您可以获得带有变量的最后一个命令退出代码
$?
因此,要检查该命令是否有效,我们可以这样做。
ping -c 1 -t 1 192.168.1.1;if [ $? -eq 0 ]; then echo "192.168.1.1 is up";else echo "ip is down";fi
隐藏ping输出
最后一件事,我们不需要看平输出,所以我们可以重定向
stdout到
/dev/null与
>重定向,例如:
$ ping -c 1 -t 1 192.168.1.1 > /dev/null && echo "IP is up"IP is up
要重定向
stderr(丢弃
ping: sendto: Host is down消息),请使用
2>-例如:
$ errorcausingcommand-bash: errorcausingcommand: command not found$ errorcausingcommand 2> /dev/null$
剧本
所以,要结合所有这些。
for ip in 192.168.1.{1..10}; do # for loop and the {} operator ping -c 1 -t 1 192.168.1.1 > /dev/null 2> /dev/null # ping and discard output if [ $? -eq 0 ]; then # check the exit pre echo "${ip} is up" # display the output # you could send this to a log file by using the >>pinglog.txt redirect else echo "${ip} is down" fidone或者,使用该
&&方法,在一种情况下:
for ip in 192.168.1.{1..10}; do ping -c 1 -t 1 $ip > /dev/null && echo "${ip} is up"; done问题
它很慢。每个ping命令大约需要1秒(因为我们将-t
timeout标志设置为1秒)。它一次只能运行一个ping命令。解决此问题的明显方法是使用线程,因此您可以运行并发命令,但这超出了应使用bash的范围。
“ Python线程-第一个示例”说明了如何使用Python线程模块编写多线程Ping’er。尽管在这一点上,我还是建议再次使用
nmap -sn..



