reading IP address given via DHCP

Levi Pearson levipearson at gmail.com
Mon Sep 23 14:03:23 MDT 2013


On Mon, Sep 23, 2013 at 3:36 AM, Dan Egli <ddavidegli at gmail.com> wrote:
> Hey, does anyone know of a way to learn the IP address that was received
> from the DHCP server? I'm wondering about this. Piping the output of
> ifconfig through grep and then through cut I know would work, but it seems
> like a very clumsy method of learning that eth0 was assigned 192.168.0.16,
> for example. Is there a better way? I'd think there would be, but I can't
> think of one.
>
>
>
> This is only at the shell level, so it's not like I can query the kernel
> directly. Now files from /proc or /sys are, of course, acceptable targets.
> :) I don't know those directories well enough to even begin to guess where
> such a file may be.

This will depend in some cases on which dhcp client you use, but there
are numerous ways:

1. Look at the output of ifconfig/ip addr/etc.
2. Look at the log output of the dhcp daemon in /var/log/whatever
3. Add a hook in the dhcp client's shell script infrastructure to
write out the lease info
4. Look at the lease info it already writes out somewhere to a file
called something like <ifname>.lease
5. Run tcpdump with a filter set up for dhcp traffic and parse the lease grant!
6. Write a program that establishes a netlink socket to the network
system and listen for address change events! (I don't recall the
netlink API of that subsystem well enough to say how, I mostly use the
generic netlink interface to nl80211 to do wifi signal surveys).

I could probably come up with some more as well.  I didn't even
mention the /sys or /proc filesystems!

       --Levi


More information about the PLUG mailing list