Python 有没有什么快速的方法来获取MAC地址 of the 默认网关?
我什么也做不了ARP 请求来自Linux我正在运行代码的机器,所以它必须直接来自ARP表.
以下 DevFS 文件将提供此信息:
/proc/net/arp
/proc/net/route
查找以 0/0 作为主机/掩码的路由条目:
Iface Destination Gateway Flags RefCnt Use Metric Mask MTU Window IRTT
eth0 00000000 B91A210A 0003 0 0 100 00000000 0 0 0
并转换网关字段(它是小端十六进制... grr):
import struct
from IPy import IP
address = IP(struct.unpack('<I', struct.pack('>I', int(i, 16)))[0])
#Converts 'B91A210A' to IP('10.33.26.185')
从那里,您可以在 arp 表中找到您的默认网关:
IP address HW type Flags HW address Mask Device
10.33.26.185 0x1 0x2 e6:92:ec:f5:af:f7 * eth0
如果没有显示,请发出一次 ping,然后再次检查,然后失败。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)