如何编写一个 Linux Bash 脚本来告诉我 LAN 中哪些计算机处于开启状态?
如果我可以给它一个 IP 地址范围作为输入会有所帮助。
最佳答案
我建议使用 nmap 的 ping-scan 标志,
$ nmap -sn 192.168.1.60-70
Starting Nmap 4.11 ( http://www.insecure.org/nmap/ ) at 2009-04-09 20:13 BST
Host 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}
之类的东西也很有用 - 这使得 dir1
和 dir2
,每个包含 sub1
和 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; done
1
2
3
ping
接下来,ping.. ping 命令会因操作系统、发行版/版本的不同而有所不同(我目前使用的是 OS X)
默认情况下(同样,在 OS X 版本的 ping
上)它会一直 ping 直到被中断,这对这个不起作用,所以 ping -c 1
只会尝试发送一个数据包,这足以确定机器是否已启动。
另一个问题是超时值,在这个版本的 ping 上似乎是 11 秒。它使用 -t
标志进行了更改。一秒钟应该足以查看本地网络上的机器是否处于事件状态。
所以,我们将使用的 ping 命令是..
$ ping -c 1 -t 1 192.168.1.1
PING 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 && echo "192.168.1.1 起来了!"
另一种方法是使用 ping 的退出代码。如果 ping 命令有效,它将以 exit-code 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 输出
最后,我们不需要看到 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 code
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"
fi
done
或者,使用 &&
方法,在单行中:
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 超时标志设置为 1 秒)。它一次只能运行一个 ping 命令。解决此问题的明显方法是使用线程,因此您可以运行并发命令,但这超出了您应该使用 bash 的范围..
"Python threads - a first example"解释了如何使用 Python 线程模块来编写多线程 ping'er.. 虽然在那时,我还是会再次建议使用 nmap -sn
..
关于linux - 如何编写一个 Linux bash 脚本来告诉我 LAN 中哪些计算机处于打开状态?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/733418/