4

I am looking to retrieve a list of network interfaces.

Currently I am returning the results of ip addr and then doing some regex/string searching from output like this:

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default 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 
84: eth0@if85: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default 
    link/ether 02:42:ac:11:00:02 brd ff:ff:ff:ff:ff:ff
    inet 172.17.0.2/16 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::42:acff:fe11:2/64 scope link tentative 
       valid_lft forever preferred_lft forever

I don't really care about all the meta data, I am just looking for the interface names. So I would like to instead get:

1: lo
84: eth0@if85

Is there a way to filter the results of the ip addr command? I can definitely do some other cli magic or just regex magic in my app, but it'd be nice to have ip itself filter.

5

Yes, you can.

Using grep with PCRE(-P):

ip addr | grep -Po '^\d+:\s+\K[^:]+'
  • ^\d+:\s+ matched the portion before interface name at the start, \K discards the match

  • [^:]+ gets the portion upto the next : i.e. the interface name

Similar logic using sed:

ip addr | sed -nE 's/^[[:digit:]]+:[[:blank:]]+([^:]+).*/\1/p'

On my system:

% ip addr | grep -Po '^\d+:\s+\K[^:]+'
lo
eth0
wlan0

% ip addr | sed -nE 's/^[[:digit:]]+:[[:blank:]]+([^:]+).*/\1/p'
lo
eth0
wlan0

What you should really do:

Linux kernel provides an interface for peeking into the hardware, it is sysfs, mounted on /sys.

You can get the interface name by just getting to the appropriate location of /sys, /sys/class/net/ directory precisely.

On my system:

% ls -1 /sys/class/net/
eth0
lo
wlan0

Each of these are directories, with the subdirectories containing files and directories having all the info regarding the interface.

Here's the listing of the contents of /sys/class/net/eth0 directory:

% ls -1 /sys/class/net/eth0                                    
addr_assign_type
address
addr_len
broadcast
carrier
carrier_changes
device
dev_id
dev_port
dormant
duplex
flags
gro_flush_timeout
ifalias
ifindex
iflink
link_mode
mtu
name_assign_type
netdev_group
operstate
phys_port_id
phys_port_name
phys_switch_id
power
queues
speed
statistics
subsystem
tx_queue_len
type
uevent

Answer to the edited question:

To get the interface name along with index:

grep:

ip addr | grep -o '^[0-9]\+:[^:]\+'

sed:

ip addr | sed -nE 's/^([[:digit:]]+:[^:]+).*/\1/p'

For each interface directory in /sys/class/net/, you can read the file ifindex. For example for interface eth0, the index file is:

/sys/class/net/eth0/ifindex
1
  • @enderland Each of the network devices listed is in fact a directory. Within the directory is ifindex which is the index that you are after. – garethTheRed Jun 24 '16 at 15:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.