I need to translate the result of the ifconfig -a command 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 this with sed or something similar. At the moment, 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 output of ifconfig -a is ( ... means the omitted 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 conversion is that the IP address is on the second line of the name and type of interface. A used the -N argument for concat lines, but they are concat pairs, and if the interface name is on an odd line, I have problems: the script skips the interface on an odd line like this (Note that there is no mention of 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 do not know how to insert a null address if ifconfig -a does not have an IP address.
source share