Grabbing IP and zonename from multiline 'ifconfig' output

Hi There,

I have a Solaris server that has a bunch of zones configured and I am trying to write a script that will take all interfaces other than the loopback ones (e.g. lo0:3 etc) and present them so that I can easily determine the zone that owns the IP

So in the case of the following ifconfg output

# ifconfig -a
lo0: flags=2001000849<UP,LOOPBACK,RUNNING,MULTICAST,IPv4,VIRTUAL> mtu 8232 index 1
        inet 127.0.0.1 netmask ff000000
lo0:1: flags=2001000849<UP,LOOPBACK,RUNNING,MULTICAST,IPv4,VIRTUAL> mtu 8232 index 1
        zone myzone1
        inet 127.0.0.1 netmask ff000000
lo0:2: flags=2001000849<UP,LOOPBACK,RUNNING,MULTICAST,IPv4,VIRTUAL> mtu 8232 index 1
        zone myzone2
        inet 127.0.0.1 netmask ff000000
lo0:3: flags=2001000849<UP,LOOPBACK,RUNNING,MULTICAST,IPv4,VIRTUAL> mtu 8232 index 1
        zone myzone3
        inet 127.0.0.1 netmask ff000000
lo0:4: flags=2001000849<UP,LOOPBACK,RUNNING,MULTICAST,IPv4,VIRTUAL> mtu 8232 index 1
        zone myzone4
        inet 127.0.0.1 netmask ff000000	
nxge3: flags=1000843<UP,BROADCAST,RUNNING,MULTICAST,IPv4> mtu 1500 index 3
        inet 10.90.24.145 netmask ffffff00 broadcast 10.90.24.255
        groupname MAIN
        ether 0:21:28:e5:xx:xx
nxge3:1: flags=1000843<UP,BROADCAST,RUNNING,MULTICAST,IPv4> mtu 1500 index 3
        inet 0.0.0.0 netmask ff000000 broadcast 0.255.255.255
nxge3:2: flags=1000843<UP,BROADCAST,RUNNING,MULTICAST,IPv4> mtu 1500 index 3
        zone myzone1
        inet 10.90.24.82 netmask ffffff00 broadcast 10.90.24.255
nxge3:3: flags=1000843<UP,BROADCAST,RUNNING,MULTICAST,IPv4> mtu 1500 index 3
        zone myzone2
        inet 10.90.24.81 netmask ffffff00 broadcast 10.90.24.255
nxge3:4: flags=1000843<UP,BROADCAST,RUNNING,MULTICAST,IPv4> mtu 1500 index 3
        zone myzone3
        inet 10.90.24.83 netmask ffffff00 broadcast 10.90.24.255
nxge3:5: flags=1000843<UP,BROADCAST,RUNNING,MULTICAST,IPv4> mtu 1500 index 3
        zone myzone4
        inet 10.90.24.84 netmask ffffff00 broadcast 10.90.24.255

I would get something like

iface		ip			zonename
nxge3		10.90.24.145	
nxge3:1		0.0.0.0			
nxge3:2		10.90.24.82		myzone1
nxge3:3		10.90.24.81		myzone2
nxge3:4		10.90.24.83		myzone3
nxge3:5		10.90.24.84		myzone4

Im a little unsure how I would get it to treat each individual block of ifconfig output as a seperate entity as clearly the virtual IP's for the zones are laid out differently by ifconfig than the non zone/main IP's (eg. nxge3) .. youll notice that the ip address line is positioned differently.

I have to capture all valid IPs in the report whether they are linked to a zone or not ..

Any advice or guidance on what would be a good starting point for me would be greatly appreciated, as im not really sure how to tackle this one

Regards

Try piping the output to the below code:-

awk 'BEGIN { print "iface ip zonename"; } /^nxge/,/[.*]$/ {
        if(match($1,/nxge/)>0)
                printf("\n%s",$1);
        if(match($1,/inet/)>0)
                printf(" %s",$2);
        if(match($1,/zone/)>0)
                printf(" %s",$2);
} '
1 Like

thats brilliant, thanks for that

the output appears as

iface ip zonename

nxge3: 10.90.24.145
nxge3:1: 0.0.0.0
nxge3:2: myzone1 10.90.24.82
nxge3:3: myzone2 10.90.24.81
nxge3:4: myzone3 10.90.24.83
nxge3:5: myzone4 10.90.24.84

Ive been having a play around with it as id really like the IP address to appear in the second column as per the headings. To do that, woul I have to play around with awk variables and print them all all at the end??

any advice would be great