1

Recently, I discussed a linux box that was missing a network script for one of its interfaces. Now I have a similar situation. Somehow, an interface eth1 was installed onto a CentOS box without an IP address. It shows up in ifconfig eth1 as this:

# ifconfig eth1
eth1: flags=4098<BROADCAST,MULTICAST>  mtu 1500
        ether 00:50:56:a9:cd:2e  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

I can fix this programmatically by iterating over the interfaces. I prefer to use ip -o for scripting purposes because the -o flag says to put everything on one line. Sadly, ip -o skips over any interface without an IP, even if I ask specifically for the non-addressed interface:

# ip -o addr show dev eth1
# ip addr show dev eth1
4: eth1: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN group default qlen 1000
    link/ether 00:50:56:a9:cd:2e brd ff:ff:ff:ff:ff:ff
# ip -o addr show dev eth0
[snip reasonable output]
# echo $?
0
# ip -o addr show dev eth1
# echo $?
0

How would you best solve this? I can use a Perl module but I'd prefer using generic shell commands.

1
  • Please specify what you are trying to achieve exactly.
    – Totor
    Commented Aug 20, 2022 at 1:00

2 Answers 2

1

So maybe you have a custom tool that gives a non-zero exit code when there is not an address found:

[root@localhost]~# ./localaddr -4 enp0s8
[root@localhost]~# echo $?
2
[root@localhost]~# ./localaddr -4 enp0s3
192.168.9.10
[root@localhost]~# echo $?
0

But that would be pretty similar to using a Perl module, which probably uses a similar interface to get the goods.

You could also ask that the ip folks return a non-zero exit code, maybe when a flag is set, if there is not an address.

Otherwise, you'll probably have to do a more expensive calculation involving more commands:

[root@localhost]~# ip -json
Option "-json" is unknown, try "ip -help".

... which is not present on Centos 7 so portability to older Linux may be problematic here. Meanwhile on a less historic Linux system,

[root@localhost]~# ip -4 -json addr show dev enp0s3 | jq -e '.[].addr_info[].local' ; echo $?
"192.168.9.11"
0
[root@localhost]~# ip -4 -json addr show dev enp0s8 | jq -e '.[].addr_info[].local' ; echo $?
4

... but firing up a shell pipeline instead of running a single program seems less efficient to me, so I probably would not use it.

1
  • That jq trick may be the best possible solution, at least so far as ip returns 0 no matter what. Sadly, it is not available on the environment I'm working with.
    – Lucky
    Commented Aug 22, 2022 at 18:07
1

Your question

Answering you is difficult because your question is:

How would you best solve this?

"this" being:

ip -o skips over any interface without an IP

but I don't know what your are trying to achieve exactly, so that my answer may or may not meet your purpose here.

My answer

Anyway, if you want to list every interface whether they have an IP address or not use ip link instead of ip addr:

ip -o link show  # will list all interfaces
ip -o link show dev eth1  # will show eth1

This will give you MAC address, up/down status and some other info.

Why it didn't work

The -o flag shows one result per line. If you ask to see addresses, it will iterate on the list of addresses and show you one address per line, or nothing if there are no addresses to iterate over.

5
  • I think the main issue as far as I was concerned is that ip -o addr show dev eth1 will return 0 (success) while no such address exists.
    – Lucky
    Commented Aug 22, 2022 at 17:57
  • I think what I really wanted was a way to avoid ifconfig which would entail parsing lots of lines. That said, perl has some wizardry to make it less painful.
    – Lucky
    Commented Aug 22, 2022 at 17:59
  • @Lucky you still don't say what your are trying to achieve by parsing this data. I still don't know your goal here so that I cannot think with you of an appropriate solution. Help me help you is what I'm saying here.
    – Totor
    Commented Aug 23, 2022 at 11:31
  • I am looking for the easiest possible way to list all interfaces regardless of configuration state of said interfaces.
    – Lucky
    Commented Aug 24, 2022 at 16:42
  • @Lucky If that is so, go for ip -o link show.
    – Totor
    Commented Aug 25, 2022 at 12:58

You must log in to answer this question.

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