Search code examples
linuxawkipip-addressethernet

Display ethernet interface and its corresponding IP address from Linux "ip a s"


I am trying to list all the ethernet device names and its assigned IP address available in the RHEL node using the output of ip addr show command

I print all the network interfaces names only with the following:

ip a s | awk -F: '/^[^ ]/ {print $2}'

I am trying to get the output in the below format by applying more logic and coding in the above awk command (or anything preferably sed or perl possibly a one-liner):

eth0: 10.xx.xx.xx
eth1: 172.xx.xx.xx

Also, loopback lo device has to be ignored in the output as this output goes to ansible inventory file after verification


Solution

  • Try

    ip --oneline addr show
    

    which should be reasonably convenient to parse with Awk.

    ip --oneline addr show | awk '$3 == "inet" && $2 != "lo" { print $2 ": " $4 }
    

    Maybe see also ip --brief which is even more compact.

    For more advanced usage, there is also ip --json addr show which outputs very detailed information about all interfaces in JSON format. See also the ip manual page.