我想编写一些简单的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一些 LAN 端(在我的示例中为 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 上。
是否有任何可编写脚本的方法来了解 LAN 上未应答的 IP 地址是否事件(设备已打开)?
最佳答案
如果您的路由器有 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 地址?”远程计算机响应,“那是我,我的 MAC 地址是 XYZ。”计算机不会阻止 ARP 请求,因为这样做会使以太网通信无法进行。
arping
不一定告诉您操作系统有响应,但它确实告诉您计算机已开机。此外,与 ICMP ping 相比,ARP 请求不可路由。您只能检查与您物理连接的主机。
关于linux - 基本网络命令行 shell : how can I know if some device that don't answer to pings is alive on the LAN?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30675635/