基本网络命令行shell:如何知道某些不响应ping的设备是否在LAN上存活?

时间:2015-06-05 20:54:07

标签: linux shell networking

我想编写一些简单的 shell程序,以查看现在是否有人真正连接到我的DD-WRT路由器。 只要这些路由器允许一些简单/平均的shell,我认为一个好的可编写脚本的方法可能进入路由器

ssh root@192.168.8.203 -p 1022

Host '192.168.8.203' is not in the trusted hosts file.
(fingerprint md5 ab:c5:94:c7:d3:56:52:1b:4b:8f:10:40:bf:1b:37:1a)
Do you want to continue connecting? (y/n) y
root@192.168.8.203's password:
==========================================================

 ____  ___    __        ______ _____         ____  _  _
 | _ \| _ \   \ \      / /  _ \_   _| __   _|___ \| || |
 || | || ||____\ \ /\ / /| |_) || |   \ \ / / __) | || |_
 ||_| ||_||_____\ V  V / |  _ < | |    \ V / / __/|__   _|
 |___/|___/      \_/\_/  |_| \_\|_|     \_/ |_____|  |_|

                       DD-WRT v24
                   http://www.dd-wrt.com

==========================================================


BusyBox v1.4.2 (2007-08-15 14:58:26 CEST) Built-in shell (ash)
Enter 'help' for a list of built-in commands.

...然后检查ARP 表:

~ # more /proc/net/arp
IP address       HW type     Flags       HW address            Mask     Device
192.168.20.100   0x1         0x0         00:00:00:00:00:00     *        br0
192.168.20.108   0x1         0x2         78:7E:61:C5:C6:7B     *        br0
192.168.20.129   0x1         0x2         F0:DB:F8:29:8F:1B     *        br0
192.168.20.110   0x1         0x0         28:5A:EB:35:CA:C9     *        br0
192.168.8.1      0x1         0x2         00:24:A5:C7:DD:BC     *        vlan1
192.168.20.111   0x1         0x2         F8:A9:D0:67:02:D4     *        br0
192.168.20.128   0x1         0x2         5C:97:F3:01:49:1C     *        br0

...然后,只要 DHCP关联表不够(设备断开连接后IP地址可能会保留数小时),尝试 ping 一些局域网(我的例子中为192.168.20.x)设备:

~ # ping 192.168.20.111
PING 192.168.20.111 (192.168.20.111): 56 data bytes
64 bytes from 192.168.20.111: icmp_seq=0 ttl=64 time=131.8 ms
64 bytes from 192.168.20.111: icmp_seq=1 ttl=64 time=265.5 ms
64 bytes from 192.168.20.111: icmp_seq=2 ttl=64 time=83.5 ms
--- 192.168.20.111 ping statistics ---
4 packets transmitted, 3 packets received, 25% packet loss
round-trip min/avg/max = 83.5/160.2/265.5 ms

但是,正如您所知,有时设备不响应ping 请求,即使它们处于活动状态(即:Windows 7的默认行为):

~ # ping 192.168.20.108
PING 192.168.20.108 (192.168.20.108): 56 data bytes
--- 192.168.20.108 ping statistics ---
10 packets transmitted, 0 packets received, 100% packet loss

我无法执行某些nmap扫描,因为它没有安装在DD-WRT上。

是否有任何可编写脚本的方法可以知道局域网上的无人应答IP地址是活着(设备是否已开启)?

2 个答案:

答案 0 :(得分:3)

如果您的路由器有arping,您可以使用它来执行以太网级ping。这比ICMP低,并且基本上是不可阻止的。

$ arping -I eth0 192.168.28.1
ARPING 192.168.28.1 from 192.168.28.130 eth0
Unicast reply from 192.168.28.1 [00:15:60:FF:8B:40]  1.176ms
Unicast reply from 192.168.28.1 [00:15:60:FF:8B:40]  1.249ms

此测试验证远程主机的NIC至少响应ARP请求。 ARP请求是指您的计算机询问谁拥有这样的 IP地址?&#34;并且远程计算机响应,&#34;那是我,我的MAC地址是 XYZ 。&#34;计算机不会阻止ARP请求,因为这样做会使以太网通信无法进行。

arping并不一定告诉您操作系统是响应式的,但它确实告诉您计算机已启动。此外,与ICMP ping相反,ARP请求不可路由。您只能检查物理连接到的主机。

答案 1 :(得分:1)

  

即使@JohnKugelman的回答很好,我也希望这个回答   更便携,更简单的设备。

似乎一个简单的arp -a命令会通过网络ARP请求来尝试定位ARP缓存设备:

~ # arp -a
Kelseys-iPhone (192.168.10.116) at <incomplete> on br0
danielas-iPhone (192.168.10.119) at <incomplete> on br0
iPad-de-Mery (192.168.10.114) at F0:DB:F8:29:8F:1B [ether] on br0
? (192.168.8.1) at 00:24:A5:C7:DD:BC [ether] on vlan1
sinsanguguiiPad (192.168.10.102) at <incomplete> on br0
android-a52cac45f5022d72 (192.168.10.115) at 90:B6:86:C1:5B:8F [ether] on br0

因此,按IP地址或incomplete模式 greping 就足够了(例如上面的输出):

arp -a | grep "192.168.10.119" | grep "incomplet"
if [ $? -eq 0 ] ; then echo "Device not responding to ARP requests on network" ; fi

在我的DD-WRT路由器上进行了测试。

(请注意,我使用incomplet而不是incomplete来使其适用于西班牙语,也许还有更多语言:-))