Just gimme the IP!

Mathieu Trudel-Lapierre mathieu.tl at gmail.com
Tue Jun 14 16:09:18 UTC 2011

On Mon, Jun 13, 2011 at 9:58 PM, Dustin Kirkland <kirkland at ubuntu.com> wrote:
> Howdy ubuntu-devel!
> I'm seeing quite a bit of code duplication in scripts and packaging in
> Ubuntu around the determination of IP addresses.
> Most are permutations of 'ifconfig' or 'ip addr', and four to six
> pipes through awk, grep, sed, and/or cut.  Some others dig through
> /proc.  Some are buggy (ie, more than one ip address on the system,
> foreign locale but does not set LC_ALL=C, etc).  Many of them do their
> job well enough, but I can't help but think there's some room for
> improvement.
> In the interest of consistency, I'm wondering if it would make sense
> to create and maintain a stable, definitive utility somewhere in
> Ubuntu's default seed to provide the system's ip address,
> *succinctly*, quickly, and reliably.

Yes, it would. I also think it should be based off 'ip addr' where
possible, since that seems to provide information in the most
manageable way.

> I'd think it should:
>  a) default to ipv4, but support a -6|--ipv6 option
>  b) default to the interface providing the default route, but support
> an optional interface parameter
>  c) be very, very fast (ie, I looked at facter, but it's pretty slow)
> I have what I think is a decent working implementation of the above at:
>  * http://people.canonical.com/~kirkland/ipaddr

Looks really nice. I think you could simplify it just a bit with a
slightly different call of ip addr:

mtrudel at artemis ~ % ip -o -f inet addr show
1: lo    inet scope host lo
2: eth0    inet brd scope global eth0
6: virbr0    inet brd scope global virbr0
mtrudel at artemis ~ % ip -o -f inet6 addr show
1: lo    inet6 ::1/128 scope host \       valid_lft forever
preferred_lft forever
2: eth0    inet6 2001:470:1d:356::10/64 scope global \       valid_lft
forever preferred_lft forever
2: eth0    inet6 fe80::f24d:a2ff:fec3:96fe/64 scope link \
valid_lft forever preferred_lft forever
14: teredo    inet6 2001:0:53aa:64c:2838:4216:9fea:e0a8/32 scope global \

(-o stands for oneline, and output for each device actually is on one
line; in case this doesn't show up properly here)

Would it make sense to also have a way to get the current *public* ip address?

/ Matt

More information about the ubuntu-devel mailing list