tags:

views:

193

answers:

3

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

A: 

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.

Benjamin Franz
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.
Joel Holdsworth
You probably can't monitor /proc files using inotify. /proc isn't 'actually' a file system but rather a kernel interface.
Benjamin Franz
A: 

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

Ori Pessach
D-bus would be nice, but this is embedded ucLinux. Sorry I should have made that clear.
Joel Holdsworth
+1  A: 

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)

MarkR
That looks good - very good; thank you... but not available on the BusyBox version of ip in ucLinux!
Joel Holdsworth