Detecting a change of IP address in Linux

扶醉桌前 提交于 2019-11-29 02:59:39

问题


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/ ?


回答1:


The command

ip monitor

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)




回答2:


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. 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.




回答3:


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.




回答4:


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.

#!/bin/bash

OLD_IP=`cat ip.txt`

NEW_IP=`/sbin/ifconfig  | awk -F "[: ]+'{ print $4}'` #adapted from something I got from the internets.

if [ $NEW_IP != OLD_IP ]; then
    nsupdate <commands> #it seems like the keys need to be in the same directory from where nsupdate was called
fi

echo $NEW_IP > ip.txt

exit 0 #not sure if this is necessary

Not tested!




回答5:


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




回答6:


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




回答7:


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

#!/bin/bash

getip=$(wget http://192.168.3.1/)
cat index.html | grep "wan_ipaddr" > gotip.txt

gotip=$(cat gotip.txt)
compare=$(cat workingip.txt)

if [[ "$compare" != "$gotip" ]]
    then 
    EMAIL="youremail@foo.net"
    EMAILMESSAGE="/home/pi/ipmessage.txt"
    echo "ip address is now $gotip" >> $EMAILMESSAGE
    /usr/sbin/sendmail -t "$EMAIL" < $EMAILMESSAGE
    rm ipmessage.txt
    cp gotip.txt workingip.txt
    rm index.html

else
echo "done"
rm index.html
fi


来源:https://stackoverflow.com/questions/2738935/detecting-a-change-of-ip-address-in-linux

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!