Detecting a change of IP address in Linux
Asked Answered
V

7

16

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

Varian answered 29/4, 2010 at 16:18 Comment(2)
How quickly would you like to know? Is it long enough that some sort of polling is acceptable? (not that I would recommend polling if a proper notification is available)Seka
#8542753Widner
M
27

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.

Mcdonald answered 13/12, 2013 at 19:22 Comment(4)
what do you do when you're behind a NAT? your IP is fixed but your global one changes, how can you detect that?Waffle
@Waffle you can't do that with DHCPcd; you might find it in some other way, maybe running something still in dhcpcd-run-hooks, but in general is not dhcpcd responsibility to know the public IP.Gantrisin
i actually came here by googling how to detect when a the public IP changesGomez
@Waffle I use Universal Plug aNd Play to get my public IP address but I'm not sure that it works with all routers: upnpc -s -4 | grep ExternalIPAddress | awk '{print $3}' Otherwise, this fallback works too: curl -4 ifconfig.coHarquebusier
W
20

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)

Warram answered 29/4, 2010 at 22:16 Comment(1)
That looks good - very good; thank you... but not available on the BusyBox version of ip in ucLinux!Varian
C
2

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.

Clobber answered 29/4, 2010 at 21:0 Comment(2)
The solution I think I'll go with is to inotify monitor /proc/net/route , and then use the SIOCGIFADDR ioctl to query the address every time the routing table changes.Varian
You probably can't monitor /proc files using inotify. /proc isn't 'actually' a file system but rather a kernel interface.Clobber
R
1

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!

Renewal answered 14/6, 2011 at 1:36 Comment(1)
There is the $ missing for the OLD_IP. This helped me a lot, i changed it to get the external IP of my Home Internet and write it to nsupdate.info to get a free dynDNS. It might help someone so: OLD_IP=cat ip.txt NEW_IP=dig +short myip.opendns.com @resolver1.opendns.com if [ $NEW_IP != $OLD_IP ]; then /usr/bin/curl --silent "CUSTOMDOMAIN.nsupdate.info:[email protected]/nic/…" fi echo $NEW_IP > ip.txtDufresne
V
1

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

Vying answered 18/12, 2013 at 4:19 Comment(0)
G
0

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

Gains answered 29/4, 2010 at 21:3 Comment(1)
D-bus would be nice, but this is embedded ucLinux. Sorry I should have made that clear.Varian
P
0

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="[email protected]"
    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
Portent answered 16/10, 2013 at 17:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.