简体   繁体   中英

using bash how can i get the network device name and ip address in one line?

I want to get the Network Interface device name (ens###) along with its associated IP address (###.###.###.###). I have solutions to get one or the other but I have not been able to find something that can output each pair (Name + IP) to a line.

Here is a command to get IP

ip address | grep -Eo 'inet (addr:)?([0-9]*\.){3}[0-9]*' | grep -Eo '([0-9]*\.){3}[0-9]*' | grep -v '127.0.0.1'

And here I can get the device name

ip address | grep -v lo | cut -d ' ' -f2 | tr ':' '\n' | awk NF

However I would like a way to get both which would output each set to their own line, something like this

ens32 10.0.0.100
ens33 10.1.0.100

EDIT:

Here is a sample output of ip address

[root@centos ~]# ip address
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: ens32: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:00:00:00:00:00 brd ff:ff:ff:ff:ff:ff
    inet 10.0.0.100/23 brd 10.0.1.255 scope global dynamic ens32
       valid_lft 83040sec preferred_lft 83040sec
    inet6 0000::000:0000:0000:0000/64 scope link
       valid_lft forever preferred_lft forever
3: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:00:00:00:00:00 brd ff:ff:ff:ff:ff:ff
    inet 10.1.0.100/24 brd 10.0.2.255 scope global dynamic ens33
       valid_lft 1277sec preferred_lft 1277sec
    inet6 0000::000:0000:0000:0000/64 scope link
       valid_lft forever preferred_lft forever

SOLUTIONS:

Both of these will give me the same desired output. Thanks for the help!

ip -o addr show scope global | awk '/^[0-9]:/{print $2, $4}' | cut -f1 -d '/'
ip -o addr show scope global | tr -s ' ' | tr '/' ' ' | cut -f 2,4 -d ' '

Well, awk , as always, works like a charm.

ip address | 
awk '
    /^[0-9]:/{
        name=substr($2, 1, length($2) - 1)
    }
    /^[ ]*inet /{
        split($2, a, "/")
        if (name != "lo")
            print name,a[1]
    }
'

will ouptut:

ens32 10.0.0.100
ens33 10.1.0.100
  1. If the line starts with a number and doublescore, then get the name from the second field except remove the : with substr.
  2. If the line starts with inet and spaces, that means that the second arg has the ip address. I also remove the netmask suffix with a simple split.
  3. If the interface name is lo we don't print the output, thus filtering loopback interface.

If you need a one-liner, try this (thanks to Dougie for the more refined ip command) :

ip -oneline -4 addr show scope global | tr -s ' ' | tr '/' ' ' | cut -f 2,4 -d ' '

-oneline forces output for each interface to a single line.

Then we cut out just the interface name and IP from the output, tr anslating it a bit along the way (for cut to get rid of extra stuff).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM