c - 如何编写一个内核模块来查找内核中的路由表和arp缓存?

标签 c linux embedded linux-kernel kernel-module

我想写一个模块来查找内核中的路由表以获取网关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/

相关文章:

c - 使用 HCS12 微 Controller 防止读取撕裂

c - 使用埃拉托斯特尼筛法求素数之和

c - 如何在 MacRuby 中创建 C 指针?

Linux TortoiseSVN

c - Bootstrap 。 ARM CORTEX M0+ 重定位中断表组件错误

STM32驱动开发中的C编译错误

c - 使用 linux create_timer 和 sigaction API 时出现段错误

C - 创建字符串矩阵时遇到问题

linux - 在Django中连接服务器linux ubuntu系统

访问环境变量的C代码