问题描述
有没有人知道在Linux中检测IP地址更改的方法。假设我运行了dhcpcd,并且它分配了一个新的IP地址,有什么方法可以在它发生变化时收到通知吗?我不能使用D-Bus,因为这是一个没有它的嵌入式ucLinux构建。
Does anyone know a way to detect a change of IP address in Linux. Say I have dhcpcd running, and it assigns a new IP address, is there a way I can get a notification when it changes? I can't use D-Bus, because this is an embedded ucLinux build that doesn't have it.
inotify / proc / net /?
inotify on something in /proc/net/ ?
推荐答案
这是一个老问题,但我会回答那些将由谷歌到达的人(比如我自己)。经过一段时间的努力,我发现你不一定需要为此调查或破解C解决方案。对于我的情况,我想在IP更改时更新我的家庭服务器(动态DNS)域。
This is an old question, but I will answer for those who will arrive by Google (such as myself). After struggling for a while, I found out that you don't necessarily need to poll or hack a C solution for this. For my case, I wanted to update my home server's (dynamic dns) domain when the IP changes.
如果你正在运行 dhcpcd
,你很幸运。发生任何事情时, dhcpcd
将运行钩子脚本。参见 man dhcpcd-run-hooks
。基本上你会想要修改或创建你自己的 dhcpcd.enter-hook
或 dhcpcd.exit-hook
取决于具体内容你想要处理事件提供的数据。
If you are running dhcpcd
, you are in luck. dhcpcd
will run hook scripts when anything happens. See man dhcpcd-run-hooks
(online here). Basically you will want to modify or create your own dhcpcd.enter-hook
or dhcpcd.exit-hook
depending on what you want to do with the data provided by the event.
这篇关于在Linux中检测IP地址的变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!