如何自动获得 IP 地址更改通知 [英] how to get notified for IP address changes automatically
问题描述
在 linux 中是否有任何方法,例如回调或信号,以便在 IP 地址发生任何变化时获得通知.我想向内核注册回调或从内核获取信号,这是自动完成的,而不是我的代码在套接字上轮询.
In linux is there any method like callback or signal to get notified whenever there is any change in the IP Address. I want to register a callback with with kernel or get a signal from kernel which is done automatically rather my code polling on the socket.
提前致谢.
推荐答案
您通过 netlink 套接字接收来自内核的通知.
You receive notifications from the kernel via netlink sockets.
您需要创建一个 NETLINK_ROUTE
套接字并通过 bind()
将其订阅到 RTMGRP_IPV4_IFADDR
组来订阅 IP 更改.然后,您将收到 RTM_NEWADDR
和 RTM_DELADDR
类型的 netlink 消息,路由属性为 IFA_LOCAL
或 IFA_ADDRESS
.
You would need to create a NETLINK_ROUTE
socket and subscribe it to IP changes via bind()
ing it to the RTMGRP_IPV4_IFADDR
group. Then, you'll receive netlink messages of type RTM_NEWADDR
and RTM_DELADDR
with a route attribute of IFA_LOCAL
or IFA_ADDRESS
.
这篇关于如何自动获得 IP 地址更改通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!