theuniverseisflat
theuniverseisflat

Reputation: 881

How to list network interfaces that are configured with IP with the interface name

I am looking for a command line to show the interfaces and the ip associated with the interface. I run the command ifconfig -a | grep -inet .. but I need to print the interface also . How can I also print the interface name?

Command

ifconfig -a | grep inet

Input

eth1      Link encap:Ethernet  HWaddr 40:A8:F0:2D:B3:98
          inet addr:10.33.211.67  Bcast:10.33.211.79      
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

eth2      Link encap:Ethernet  HWaddr 8C:DC:D4:AD:A6:EF
          inet addr:64.15.238.227  Bcast:64.15.238.239  
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:3532763832 errors:0 dropped:0 overruns:0 frame:0

eth8      Link encap:Ethernet  HWaddr 40:A8:F0:2D:B3:9A
          BROADCAST MULTICAST  MTU:1500  Metric:1
          RX packets:3532763832 errors:0 dropped:0 overruns:0 frame:0

Desired output

eth1    inet addr:10.33.211.67  Bcast:10.33.211.79 Mask:255.255.255.240
eth2    inet addr:64.15.238.227 Bcast:64.15.238.239  Mask:255.255.255.240
eth8    ----------  blank because it has not been configured 

Upvotes: 0

Views: 120

Answers (1)

Mad Physicist
Mad Physicist

Reputation: 114548

Try the following sed pipeline:

ifconfig -a | sed -n -e 's/^\([[:alnum:]]\+[[:space:]]\+\).*$/\1/p' -e 's/^[[:space:]]\+\(inet .*\)$/\1/p' | sed 'N;s/\n/ /'

The first sed command selects lines that start with an interface name or a bunch of spaces followed by inet. The second one removes every other newline from the result based on Putting Two Consecutive Lines into One Line with Perl / AWK. Output will be:

eth1       inet addr:10.33.211.67  Bcast:10.33.211.79      
eth2       inet addr:64.15.238.227  Bcast:64.15.238.239  
eth8      

Upvotes: 3

Related Questions