6

How does one get ip addr show to only show links of a specific type, without using a secondary process like grep to do filtering?

According to man 8 ip-address, this should be possible using the following, but when I run these, I get no results:

ip addr show type "link/loopback"
ip addr show type "loopback"
7
  • According to the man page there is no type "loopback", you have to choose from the list of types: TYPE := [ bridge | bridge_slave | bond | bond_slave | can | dummy | hsr | ifb | ipoib | macvlan | macvtap | vcan | veth | vlan | vxlan | ip6tnl | ipip | sit | gre | gretap | erspan | ip6gre | ip6gretap | ip6erspan | vti | vrf | nlmon | ipvlan | lowpan | geneve | macsec ]
    – eblock
    Commented Sep 25, 2020 at 7:28
  • 1
    So there is no way to filter on loopback or ether. That's disappointing. Commented Sep 25, 2020 at 7:41
  • It appears so, yes. But I'm not the most linux experienced user so there may be something else I'm not aware of.
    – eblock
    Commented Sep 25, 2020 at 8:01
  • 1
    You can use the fact that loopback is usually named lo and filter on IFNAME instead, so ip addr show lo will work. Not sure if it is relevant for your use though.
    – Fiisch
    Commented Sep 25, 2020 at 11:16
  • @Fiisch I think OP wants to filter link/ether, link/loopback, etc. lines, instead of filtering an interface.
    – Quasímodo
    Commented Sep 25, 2020 at 11:56

2 Answers 2

1

The short and simple:

hostname -i

Usually, I'm not looking for loopback but the ip so

hostname -I 

And if you want both while reducing the textual noise and adding color

ip addr | grep inet 
2
  • You basically gave him exactly what he said he wasn't looking for.
    – deltaray
    Commented Jan 14, 2023 at 16:04
  • @deltaray I apologize. Commented Jan 22, 2023 at 21:34
0

I wound up doing this in Python. I haven't tried translating this using grep.

import re
import shlex
import subprocess

""" Return a list of interface name strings """

RE_PATT = "^\d+: (\w+).+\n +link/loopback"
proc = subprocess.run(
    shlex.split("ip link show"),
    stdout=subprocess.PIPE,
    universal_newlines=True,
)
interface_list = re.findall(RE_PATT, proc.stdout, re.MULTILINE)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .