Just gimme the IP!

Scott Kitterman ubuntu at kitterman.com
Tue Jun 14 02:06:19 UTC 2011


On Monday, June 13, 2011 09:58:21 PM Dustin Kirkland 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.
> 
> Here are a few I've found in some server packages:
>   IP=`ifconfig  | grep 'inet addr:'| grep -v '127.0.0.1' | cut -d: -f2
> 
> | awk '{ print $1}'|head -n 1`
> 
>   MYIP=$(ifconfig eth0 | grep "inet addr" | awk -F":" '{print $2}' |
> awk '{print $1}')
>   addr_withprefix=$(ip addr show label $default_interface scope global
> 
> | awk '$1 == "inet" { print $2 }' | sed "s:/.*::")
> 
> 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.
> 
> 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
> 
> $ ipaddr
> 192.168.1.109
> 
> $ ipaddr virbr0
> 192.168.122.1
> 
> $ time ./ipaddr tap0
> 10.13.17.92
> real    0m0.011s
> 
> What do you think?  Have you seen scripts or packaging in Ubuntu that
> would benefit from a solid, recommended ip determination utility?
> Where should it live?  net-tools?  iproute?  Somewhere else?

I'm not sure where it should live, but I think it should have a different name 
as it's not a generic IP address utility, but one for a single specific 
purpose.  Maybe sysip?

Scott K



More information about the ubuntu-devel mailing list