parsing ifconfig output

I'm trying to gather information on the interfaces on a large number of servers.
If I run ifconfig I will get: eth0 Link encap:Ethernet HWaddr 00:50:56:A2:27:C1
inet addr:10.145.xxx.xxx Bcast:10.152.45.255 Mask:255.255.254.0
-----
eth1 Link encap:Ethernet HWaddr 00:50:56:A2:1D:21
inet addr:10.145.xxx.xxx Bcast:10.136.27.255 Mask:255.255.254.0
------
I have tried :

/sbin/ifconfig -a | egrep "inet|Link" | awk '{ if ( $2 == "Link" ) {print  $1} else { if( $1 == "inet" )  { print $2 }}}'

Which gives me:eth0
addr:10.152.44.18
eth1
addr:10.136.26.137
What I want is this:eth0 addr:10.145.xxx.xxx
eth1 addr:10.145.xxx.xxx
Some interfaces do not have addresses so I cant just concatenate sequential lines. Is there a simple way for me to get the format I'm looking for ?

Thanks

Try this. This will give the interfaces which had ip address assigned.

ifconfig -a | awk '$2~/^Link/{_1=$1;getline;if($2~/^addr/){print _1" "$2}}'

I am using Linux/Fedora. If you are using Solaris, output of ifconfig might differ.

regards,
Ahamed

1 Like

Nice ! I have it working for all my Solaris and Linux servers now
if [ `uname | awk '{print $1}'` == "Linux" ]; then /sbin/ifconfig -a | awk '$2~/^Link/{_1=$1;getline;if($2~/^addr/){print _1" "$2}}'; else ifconfig -a | awk '$2~/^flags/{_1=$1;getline;if($1~/^inet/){print _1" "$2}}';fi