我知道我们可以使用带有 RTM_F_NOTIFY 标志的“NETLINK_ROUTE”套接字来通知用户路由根据 RFC3549 发生变化。但是我找不到一种方法来在 arp 表更改时通知用户。
PS:我用的是linux内核3.0.6
最佳答案
应该使用 NETLINK_ROUTE
套接字和 bind()
到组 RTNLGRP_NEIGH
。
之后可以使用 recv()
获取 ndmsg
通知。请注意,必须使用所有通知,否则套接字将在溢出后的下一个 recv()
引发 ENOBUF
异常。
或使用外部 ip(8)
实用程序:
$ ip monitor all # get all the notifications
$ ip monitor neigh # get only arp notifications
或使用 Python library :
from pyroute2 import IPRoute
from pprint import pprint
ip = IPRoute()
ip.bind() # subscribe to all the events
while True:
pprint(ip.get())
关于linux - 如何使用 netlink 监控用户空间守护进程中的内核 arp 表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13016941/