Just gimme the IP!
Dustin Kirkland
kirkland at ubuntu.com
Tue Jun 14 01:58:21 UTC 2011
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?
--
:-Dustin
Dustin Kirkland
Ubuntu Core Developer
More information about the ubuntu-devel
mailing list