3

Open-Vswitch creates virtual interfaces, they all have this pattern: s[digit]-eth[digit] For example: s1-eth1, s1-eth2, s12-eth3, s2-eth3 ...

I want to show information of -for example- all interfaces starting with s1 (in our example, it must give s1-eth1 and s1-eth2).

I tried this command, but it shows only the first line of information:

enter image description here

I am more interested about information like RX packets, TX packets, erros, HWaddr.. like this:

enter image description here

Dimareal
  • 133
  • 1
  • 5

7 Answers7

7

ifconfig's output has a blank line between each interface, making it perfectly suited for reading and processing in "paragraph mode" in a scripting language like awk or perl. A "paragraph" is any block of text separated from other blocks by one-or-more empty lines.

For example:

ifconfig | awk -v RS='' '/^s1-eth[12]:/ {print}'

The awk example above sets awk's record separator (RS) to the empty string, causing it to process paragraphs instead of individual lines. In this case, each paragraph is tested for a match against ^s1-eth[12]: and is printed if it matches.

Example output from my system:

$ ifconfig | awk -v RS='' '/^(ppp0|lo):/ {print}'
lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 64237556  bytes 36962222928 (34.4 GiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 64237556  bytes 36962222928 (34.4 GiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
ppp0: flags=4305<UP,POINTOPOINT,RUNNING,NOARP,MULTICAST>  mtu 1412
        inet ip.ip.ip.ip  netmask 255.255.255.255  destination ip.ip.ip.ip
        ppp  txqueuelen 3  (Point-to-Point Protocol)
        RX packets 28220997  bytes 19305565357 (17.9 GiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 21719890  bytes 3009382446 (2.8 GiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

Note that this is no longer printed in paragraphs - there is no empty line between each interfaces "record". If you want paragraphs in your output, set the output record separator (ORS) to two newlines (\n\n). e.g.

ifconfig | awk -v RS='' -v ORS='\n\n' '/^(ppp0|lo):/ {print}'

To do something similar with ip's output rather than ifconfig's is slightly more complicated. ip doesn't produce neatly paragraph-separated output. It does, however, output each interface in a block with the interface's number followed by a colon and a space at the beginning of a line, with the rest of that interfaces details following.

To make use of this, set RS to the regular expression ((^|\n)[0-9]+:). That matches any digits followed by a colon and a space ([0-9]+:) that are either at the beginning of the file (^, to match the first record, which would otherwise be skipped) or immediately after a newline.

For example, on my system:

$ ip addr | awk -v RS='(^|\n)[0-9]+: ' '/^(lo|eth0):/ {print}'
lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    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
eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast master br0 state UP group default qlen 1000
    link/ether 01:33:ed:b0:13:a9 brd ff:ff:ff:ff:ff:ff

(my eth0 doesn't have any IP addresses because I have it set up as a bridge for VMs and docker containers. The IP addresses are on the br0 interface instead)

cas
  • 1
  • 7
  • 119
  • 185
4

You could use grep -An to print the matching line and the n following ones, but that is a bit untidy since the number of lines may vary.

Probably better to use awk. This would print all sections that start with enp1 or enp3. The first rule !/^ / {p=0} clears the variable p if the line does not start with a space, the second /^enp[13]/ {p=1} sets if it the line starts with the interface names we want, and then we print if p is set. The lines that start with spaces only match the last rule, so they're printed based on the previous value of p.

# /sbin/ifconfig -a  |awk '!/^ / {p=0} /^enp[13]/ {p=1}; p'
enp1s0    Link encap:Ethernet  HWaddr ... 
          inet addr:...
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:135284106 errors:0 dropped:0 overruns:0 frame:0
          TX packets:144695 errors:0 dropped:0 overruns:0 carrier:2
          collisions:0 txqueuelen:1000 
          RX bytes:8292624956 (8.2 GB)  TX bytes:16595674 (16.5 MB)
enp3s0    Link encap:Ethernet  HWaddr ...  
          BROADCAST MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

In your case, you'd of course use a pattern like /^s1-eth/.

ilkkachu
  • 133,243
  • 15
  • 236
  • 397
3

I want to show information of -for example- all interfaces starting with s1

Using net-tools:

ifconfig | grep s1 -A9

Using iproute2:

ip -s link | grep s1 -A7
GAD3R
  • 63,407
  • 31
  • 131
  • 192
2

For your current ifconfig output:

ifconfig | grep -A7 --no-group-separator '^s1-eth'
RomanPerekhrest
  • 29,703
  • 3
  • 43
  • 67
0

For post-processing, with newer versions of iproute2 you may want to use the JSON output format that is meant for that:

ip -j -s link show |
  jq -r '
    .[]|
    select(.ifname|test("^s\\d+-eth\\d+$"))|
    [.ifname, .stats64.rx.bytes, .stats64.tx.bytes]|
    @csv'

For instance to get a CSV output with interface name, RX and TX bytes.

Stéphane Chazelas
  • 522,931
  • 91
  • 1,010
  • 1,501
-2
 ifconfig | awk '/^s[[:digit:]]+-eth[[:digit:]]+:/ {flag=1} /!^[[:digit:]]+-eth[[:digit:]]+:/ {flag=0}flag'

Using awk, pattern match to as outlined, setting a print flag to 1 if it exists and 0 if not. Print based on flag.

Raman Sailopal
  • 1,432
  • 8
  • 8
-2

Previous answers suggested:

ifconfig: which is deprecated.

ip -s link: which doesn't display all the information that ifconfig displays (such as IP address).

I wanted to offer another command that worked for me. So if you're looking for IP address(es) of an interface, you might consider:

For the raw interface output:

ip a | grep s1 -A3

For IPv4 only:

ip -4 a | grep s1 -A1

ip a is an abbrevation for ip address. -A1 and -A3 paremeters determine the number of lines to display after the last occurrence of your text. If you modify the parameters in ip command, you can change the -A parameter's number to fit as well.

ybalcanci
  • 1
  • 1
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Mar 09 '23 at 10:01
  • Can you expand on your answer so that it is evident how it is different from some of the other answers that also suggest using `ip` (or `ifconfig`) together with `grep`? – Kusalananda Mar 09 '23 at 11:54