How to test whether a secondary inet address exists on an eth interface?
2
votes
2
answers
389
views
RHEL9
Sometimes server 1 has the secondary address 10.143.170.80/24, and sometimes server 2 has that secondary address. My script needs to test which server has that secondary address.
However,
ip address show dev ${VirtDev} secondary
always returns 0, whether or not the secondary address exists or not.
Server 1:
ip address show dev $VirtDev secondary
echo $?
0
Server 2:
ip address show dev $VirtDev secondary
2: ens33: mtu 1500 qdisc mq state UP group default qlen 1000
link/ether 00:50:56:8e:73:35 brd ff:ff:ff:ff:ff:ff
altname enp2s1
inet 10.143.170.80/24 scope global secondary ens33:0
valid_lft forever preferred_lft forever
echo $?
0
This works, but seems janky:
Exists=$(ip address show dev $VirtDev secondary)
[ -n "$Exists" ] && echo exists || echo not exists
Is there a better way?
EDIT: when parsing json...
ip -j -4 a ls to 10.143.170.80 | jq -e .[].addr_info
[
{},
{
"family": "inet",
"local": "10.143.170.80",
"prefixlen": 24,
"scope": "global",
"secondary": true,
"label": "ens33:0",
"valid_life_time": 4294967295,
"preferred_life_time": 4294967295
}
]
ip -j -4 a ls to 10.143.170.80 | jq -e .[].addr_info.local
jq: error (at :1): Cannot index array with string "local"
Asked by RonJohn
(1421 rep)
Jul 29, 2025, 03:50 PM
Last activity: Jul 30, 2025, 06:35 AM
Last activity: Jul 30, 2025, 06:35 AM