问题描述
如何编写一个linux bash脚本,告诉我哪台计算机在我的局域网?
How can I write a linux bash script that tells me which computers are ON in my LAN ?
这将有助于如果我可以给它输入一个IP范围的的。
It would help if I could give it as input a range of IP's.
推荐答案
我会建议使用,nmap的平扫描标志,
I would suggest using nmap's ping-scan flag,
$ 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
这是说,如果你想自己动手(这是不够公平)写的,这是我会怎么做:
That said, if you want to write it yourself (which is fair enough), this is how I would do it:
for ip in 192.168.1.{1..10}; do ping -t 1 $ip > /dev/null && echo "${ip} is up"; done
..和上述命令的每一位的解释:
..and an explanation of each bit of the above command:
您可以使用 {1..10}
语法生成号码列表,例如..
You can use the {1..10}
syntax to generate a list of numbers, for example..
$ echo {1..10}
1 2 3 4 5 6 7 8 9 10
(它也是东西像的mkdir {DIR1,DIR2}有用/ {SUB1,SUB2}
- 这使得 DIR1
和 DIR2
,每个都包含 SUB1
和 SUB2
)
(it's also useful for things like mkdir {dir1,dir2}/{sub1,sub2}
- which makes dir1
and dir2
, each containing sub1
and sub2
)
所以,生成IP的列表,我们会做这样的事情。
So, to generate a list of IP's, we'd do something like
$ echo 192.168.1.{1..10}
192.168.1.1 192.168.1.2 [...] 192.168.1.10
循环
要遍历在bash的东西,你可以使用为
:
$ for thingy in 1 2 3; do echo $thingy; done
1
2
3
侦测
接下来,来ping。该ping命令变化的位具有不同操作系统的系统,不同的发行/版本(我使用OS X目前)
Pinging
Next, to ping.. The ping command varies a bit with different operating-systems, different distributions/versions (I'm using OS X currently)
在默认情况下(同样,在平
的OS X版本),它会ping通,直到中断,这是不会为这个工作,所以平-c 1
将只尝试发送一个数据包,这应该是足够的,以确定是否机器了。
By default (again, on the OS X version of ping
) it will ping until interrupted, which isn't going to work for this, so ping -c 1
will only try sending one packet, which should be enough to determine if a machine is up.
另一个问题是超时值,这似乎是这个版本平11秒。它的使用 -t
标志改变。一个第二应足够,以查看是否在本地网络上的机器是活着或不
Another problem is the timeout value, which seems to be 11 seconds on this version of ping.. It's changed using the -t
flag. One second should be enough to see if a machine on the local network is alive or not.
因此,我们将使用ping命令。
So, the ping command we'll use is..
$ 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
检查结果平安
接下来,我们需要知道,如果机器回答或不..
Checking ping result
Next, we need to know if the machine replied or not..
我们可以使用&放大器;&安培;
运营商如果第一次成功,比如运行一个命令:
We can use the &&
operator to run a command if the first succeeds, for example:
$ echo && echo "It works"
It works
$ nonexistantcommand && echo "This should not echo"
-bash: nonexistantcommand: command not found
好,所以我们可以做的。
Good, so we can do..
平-c 1 -t 1 192.168.1.1&安培;&安培;回声192.168.1.1到了!
ping -c 1 -t 1 192.168.1.1 && echo "192.168.1.1 is up!"
另一种方法是使用退出code从平.. ping命令将与×[出口code 0(成功),如果它的工作,和一个非零code。如果退出它失败了。在bash你得到的最后命令退出code。与变量 $?
The other way would be to use the exit code from ping.. The ping command will exit with exit-code 0 (success) if it worked, and a non-zero code if it failed. In bash you get the last commands exit code with the variable $?
所以,以检查命令的工作,我们应该这样做。
So, to check if the command worked, we'd do..
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
隐藏平输出
最后一件事,我们不需要看平输出,所以我们可以重定向标准输出
到的/ dev / null的
与>
重定向,例如:
$ ping -c 1 -t 1 192.168.1.1 > /dev/null && echo "IP is up"
IP is up
和重定向标准错误
(放弃平:SENDTO:主机已关闭
消息),可以使用 2 - ;
- 例如:
And to redirect stderr
(to discard the ping: sendto: Host is down
messages), you use 2>
- for example:
$ errorcausingcommand
-bash: errorcausingcommand: command not found
$ errorcausingcommand 2> /dev/null
$
剧本
因此,结合所有的..
The script
So, to combine all that..
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的..
Problem
It's slow.. Each ping command takes about 1 second (since we set the -t timeout flag to 1 second). It can only run one ping command at a time.. The obvious way around this is to use threads, so you can run concurrent commands, but that's beyond what you should use bash for..
说明如何使用Python线程模块编写多线程ping'er。虽然在这一点上,我要再次使用建议 nmap的-sn
..
"Python threads - a first example" explains how to use the Python threading module to write a multi-threaded ping'er.. Although at that point, I would once again suggest using nmap -sn
..
这篇关于我怎么能写一个linux bash脚本,告诉我哪台计算机在我的局域网?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!