在 Linux 中检测 IP 地址的变化
有谁知道在 Linux 中检测 IP 地址变化的方法吗?假设我正在运行 dhcpcd,并且它分配了一个新的 IP 地址,有没有办法在它发生变化时收到通知?我无法使用 D-Bus,因为这是一个嵌入式 ucLinux 版本,没有它。
inotify 对 /proc/net/ 中的某些内容进行处理?
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 on something in /proc/net/ ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
这是一个老问题,但我会为那些通过谷歌到达的人(比如我自己)回答。经过一段时间的挣扎,我发现您不一定需要为此轮询或破解 C 解决方案。就我而言,我想在 IP 更改时更新我的家庭服务器(动态 dns)域。
如果您正在运行 dhcpcd,那么您很幸运。
dhcpcd
将在发生任何情况时运行挂钩脚本。请参阅man dhcpcd-run-hooks
(在线此处)。基本上,您需要修改或创建自己的dhcpcd.enter-hook
或dhcpcd.exit-hook
,具体取决于您想要对事件提供的数据执行的操作。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.
If you are running
dhcpcd
, you are in luck.dhcpcd
will run hook scripts when anything happens. Seeman dhcpcd-run-hooks
(online here). Basically you will want to modify or create your owndhcpcd.enter-hook
ordhcpcd.exit-hook
depending on what you want to do with the data provided by the event.该命令
将向您展示这种情况的发生。它使用了一些相当棘手且没有很好记录的 netlink API(至少对于人类来说是无法理解的)。然而,它能够得到内核的各种事件的通知,例如分配的IP、路由表和链路状态的变化(例如有人拔掉了网络)
The command
will show you this kind of thing happening. It uses some the netlink API which is rather tricky and not documented well (at least for humans to understand). However, it is able to get notified by the kernel of various events, such as changes of assigned IPs, routing tables and link status (e.g. someone unplugged the network)
由于 DHCP 活动会发送到 syslogd,因此您可以创建一个命名管道,将 syslog 流量定向到该管道并监视该流以获取 IP 地址更新。请参阅“man syslogd”和“man syslog.conf”。
编辑:另一种方法是使用 inotify 来监视接口的 DHCP 租用文件。在 Ubuntu 9.10 下,位于 /var/lib/dhcp3 目录中。
Since DHCP activity is sent to syslogd you could create a named pipe, direct syslog traffic to it and watch the stream for IP address updates. See 'man syslogd' and 'man syslog.conf'.
Edit: Another approach would be to use inotify to monitor the DHCP leases file for the interface. Under Ubuntu 9.10 that is in the /var/lib/dhcp3 directory.
我想到的是每 10 分钟左右从 cron 运行这个脚本,具体取决于你的链接。
如果我写得正确,它只会在 IP 更改时进行 nsupdate,因此不会在区域的主服务器上创建不适当的负载。
未测试!
What I thought of was running this script from cron every 10 or so minutes, depending on your link.
If I wrote this right, it only nsupdates when there is an IP change, so no undue load is creater on the zone's master server.
Not tested!
这是一个较旧的线程,但万一有人像我一样发现它,我不久前写了一些在 Linux 中进行网络更改检测/通知的东西(主要是为了帮助 VPN 用户),并且感谢一些咄咄逼人的朋友,我将其提供给其他人使用。现在它是一个宠物项目,我正在积极维护它,因此欢迎提出功能请求和反馈。
http://code.google.com/p/ipcheck/source/browse /ipcheck.sh
This is an older thread but in case someone finds it like I did, I wrote something that does network change detection/notification in Linux awhile back (mostly targeted at helping VPN users), and thanks to some pushy friends I put it up for others to use. It's a pet project now and I'm actively maintaining it, so feature requests and feedback are welcome.
http://code.google.com/p/ipcheck/source/browse/ipcheck.sh
我认为您可以使用 dbus 在现代 Linux 发行版上执行此操作。如果您的发行版使用 NetworkManager,请参阅此文档以获取有关其 dbus 接口的信息:
http://people.redhat.com/dcbw/NetworkManager/NetworkManager%20DBUS%20API.txt
I think you can use dbus to do this on modern Linux distributions. If your distribution uses NetworkManager, see this document for information about its dbus interface:
http://people.redhat.com/dcbw/NetworkManager/NetworkManager%20DBUS%20API.txt
如果您有一个运行 DD-WRT 的路由器,并且在访问路由器时正在使用状态页,您可以使用脚本... wget 状态页,cat 获取 IP 地址并将其写入文件进行比较,当最新的 wget IP 地址与比较文件中的地址发生变化时,发送电子邮件。
我在 linksys wrt54g 路由器上运行 dd-wrt 并使用以下脚本:
它从 192.168.3.1 获取路由器状态页面,在页面上使用 cat (index.html) 并使用 grep 查找 wan IP 地址,然后将其写入文件 (gotip.txt)。
将捕获的 ip (gotip.txt) 与当前工作 ip (workingip.txt) 进行比较。如果ip地址不同,我会收到一封新ip的send mail发送的电子邮件,并将新的工作ip写入workingip.txt文件中。
Cron 每 5 分钟左右运行一次,我将 cron 输出静音到 /dev/null
If you have a router running DD-WRT and have the status page in use when going to the router, you can, with a script... wget the status page, cat for the ip address and write it to a file for comparison, have an email send when the latest wget ip address has changed from what is in the comparison file.
I'm running dd-wrt on a linksys wrt54g router and use this script:
It wgets the router status page from 192.168.3.1, uses cat on the page (index.html) and greps for the wan ip address, then writes it to a file (gotip.txt).
A comparison is made between the captured ip (gotip.txt) and the current working ip (workingip.txt). If the ip addresses are different, I get an email sent by send mail of the new ip, and the new working ip is written into the workingip.txt file.
Cron run this every 5 min or so and I have the cron output silenced to /dev/null