reading IP address given via DHCP

Michael Torrie torriem at gmail.com
Mon Sep 23 08:22:29 MDT 2013


On 09/23/2013 03:36 AM, Dan Egli 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.

Yes, parsing the output from ifconfig is perfectly acceptable and widely
done.  Be aware that the interface names can, under certain
circumstances, change, and also some computers have multiple interfaces.
 Right now on my desktop machine, for example, I have the KVM virtual
machine system installed, and that automatically creates a bridge, br0,
which is now actually my main interface, though eth0 is still present
and still up (it's part of the bridge).

On all of my machines, dhclient is used to talk to the dhcp server.  It
also happens to record the lease information in /var/lib/dhclient.  So a
script could parse the information in there possibly.  Or maybe just see
if dhclient is running, grab the interface it's using, and parse
ifconfig's output for that interface.


More information about the PLUG mailing list