[Swan-dev] extract MAC address from "ip -o link sow dev eth0" - awk

Antony Antony antony at phenome.org
Fri Sep 20 09:08:39 UTC 2019


There is an annoying issue when running inside docker/namespace.  The proxy 
arp function in _updown.netkey would fails. It has been bugging me for 
years!
Now I tracked it down to two lines in _updown.netkey.in

Here my first attempt to replace it using "ip link " command.  

programs/_updown.netkey/_updown.netkey.in
-           if [ -r /sys/class/net/${iface}/address ]; then
-               macaddr=$(cat /sys/class/net/${iface}/address)
+           if ip -o link show dev ${iface}; then
+               macaddr=$(ip -o link show dev ${iface} | awk '$2 != "${iface}:" {print $(NF-4)}')

I am not sure "NF-4" is good idea. The fear is a hardcoded position of the 
MAC address may not very portable to older versions "ip". Will it be always 
at a -4 in "ip -o" output.  If not we need an awk magic line.

Tuomo, would you write a better awk oneliner?
Also this has two "ip -o" calls may be we can do in one?
May be match something like "link/ether 96:01:66:8b:74:96 brd" and just 
print the MAC e.g 96:01:66:8b:74:96 

background:
inside namespace/docker "eth0" is a valid interface name.
However, it does not exist /sys/class/net/* because it is veth type. AFIK 
for veth only one end appear in /sys/class/net/${iface}. Note Namespaces and 
host seems to share /sys/class/*

Here is the output for reference.
ip -o link show dev eth0
6508: eth0 at if6509: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP mode DEFAULT group default qlen 1000\    link/ether 96:01:66:8b:74:96 brd ff:ff:ff:ff:ff:ff link-netnsid 0 

regards,
-antony


More information about the Swan-dev mailing list