我想写一个模块来查找内核中的路由表以获取网关ip,并使用该ip查找arp缓存以获取网关的mac地址。
最佳答案
不确定为什么需要内核模块。用户空间中提供了查找默认 gw mac 地址所需的一切...
#!/usr/bin/env python
import re
import socket
from struct import pack
hex_gateway = re.findall('\t00000000\t([0-9A-F]*)\t', open('/proc/net/route').read())[0]
if not hex_gateway: sys.exit(1)
gw_ip = socket.inet_ntoa(pack('I', int(hex_gateway, 16)))
gw_mac = False
for line in open('/proc/net/arp').readlines():
if line.startswith(gw_ip):
gw_mac = line.split()[3]
break
if gw_mac:print gw_mac
else:sys.exit(1)
关于c - 如何编写一个内核模块来查找内核中的路由表和arp缓存?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9596735/