Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to format output of ifconfig

Tags:

linux

bash

sed

I need to transfom the result of command ifconfig -a to the following format

IFACE eth0 192.168.30.8 Ethernet
IFACE eth1 212.233.112.171 Ethernet
IFACE lo 127.0.0.1 Local Loopback 
IFACE pan0 0.0.0.0 Ethernet
IFACE tunl0 0.0.0.0 IPIP Tunnel

I know that I should do that with the sed or something similar. For now I have the following "script":

ifconfig -a | sed -r -n -e 'N' -e 's/(\w+)(\s*)(Link\sencap:)(\w+(\s\w+)*)([^\n]*)\n\s+(inet\saddr:)([0-9]{1,3}(\.[0-9]{1,3}){3}).*/IFACE \1 \8 \4/p'

The original ifconfig -a output is (... means ommited parts)

eth0      Link encap:Ethernet  HWaddr f4:ce:46:99:22:57
          inet addr:192.168.30.8  Bcast:192.168.31.255  Mask:255.255.254.0
          ...
eth1      Link encap:Ethernet  HWaddr 00:23:7d:fd:a2:d0
          inet addr:212.233.112.171  Bcast:212.233.112.175  Mask:255.255.255.240
          ...
lo        Link encap:Local Loopback
          inet addr:127.0.0.1  Mask:255.0.0.0
          ...
pan0      Link encap:Ethernet  HWaddr f6:d0:8a:0e:7b:95
          BROADCAST MULTICAST  MTU:1500  Metric:1
          ...
tunl0     Link encap:IPIP Tunnel  HWaddr
          NOARP  MTU:1480  Metric:1
          ...

The first problem with this transformation is that IP address resides on the second line from the interface name and type. A used the -N argument to concat the lines, but they are being concat in pairs, and if the name of the interface is on odd line - I have troubles: script skips the interface on the odd line, like this (NOTE that there are no mentioning about pan0 in the output.):

IFACE eth0 192.168.30.8 Ethernet 
IFACE eth1 212.233.112.171 Ethernet 
IFACE lo 127.0.0.1 Local Loopback
IFACE tunl0 IPIP Tunnel

The second problem is that I don't know how to insert zero-address when there are no IP adress in ifconfig -a output.

like image 806
Anton Boritskiy Avatar asked Jan 14 '23 07:01

Anton Boritskiy


2 Answers

Note that ifconfig output varies with platforms, so this is not a very portable way of doing it.

As to a sed solution, I think you need to break the parsing into multiple steps, something like this should work (GNU sed):

parse.sed

s/^([^ ]+) */\1\n/                               # interface name
s/Link encap:(.*)(  |$).*/\1/                    # link encapsulation
N                                                # append next line to PS
/inet addr/! s/\n[^\n]*$/\n0.0.0.0\n/            # use 0.0.0.0 if no "inet addr"
s/ *inet addr:([^ ]+).*/\1\n/                    # capture ip address if present
s/\n[^\n]*$//                                    # cleanup the last line
s/([^\n]+)\n([^\n]+)\n([^\n]+)/IFACE \1 \3 \2/p  # print entry
s/.*//                                           # empty PS
: loop                                           # \
N                                                #  \
/^\n$/b                                          #   skip until next empty line
s/.*//                                           #  /
b loop                                           # /

Explanation

The idea is to capture all the relevant information in pattern space with newline delimiters.

Testing

Run like this:

ifconfig -a | sed -rf parse.sed

Output:

IFACE eth0 192.168.30.8 Ethernet
IFACE eth1 212.233.112.171 Ethernet
IFACE lo 127.0.0.1 Local Loopback
IFACE pan0 0.0.0.0 Ethernet
IFACE tunl0 IPIP Tunnel
like image 139
Thor Avatar answered Jan 16 '23 20:01

Thor


I've already accepted the Thor's answer, but here is my solution, which is a bit different. I was thinking it over while waiting for an answer.

ip addr | sed -r ':a;N;$!ba;s/\n\s/ /g' | sed -r -n -e 's/^([0-9]+):\s(\w+).*(link\/(\w+))\s[a-f0-9:.]{,17}\sbrd\s[a-f0-9:.]{,17}\s*(inet\s([0-9]{1,3}(\.[0-9]{1,3}){3})).*/IFACE \2 \6 \4/p' -e 's/^([0-9]+):\s(\w+).*(link\/(\w+))\s[a-f0-9:.]{,17}\sbrd\s[a-f0-9:.]{,17}.*/IFACE \2 0.0.0.0 \4/p'

output:

IFACE lo 127.0.0.1 loopback
IFACE eth0 192.168.30.8 ether
IFACE eth1 212.233.112.171 ether
IFACE pan0 0.0.0.0 ether
IFACE tunl0 0.0.0.0 ipip

the main advantage - it can be called as is. The second advantage is that the ordering of interfaces coinsides with interface indexes (I got this when started comparing results of ifconfig and ip addr).

But Thorn's solution is much more readable and maintainable.

like image 29
Anton Boritskiy Avatar answered Jan 16 '23 22:01

Anton Boritskiy