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

Antony Antony antony at phenome.org
Tue Sep 24 06:44:35 UTC 2019

On Fri, Sep 20, 2019 at 05:41:15PM -0400, Lennart Sorensen wrote:
> On Fri, Sep 20, 2019 at 11:08:39AM +0200, Antony Antony wrote:
> > 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)}')
> Looking at docker containers here, eth0 is in /sys/class/net/ in
> the container and appears to have the correct mac address showing.
> Matches what ip command shows.  Not sure why yours would not have it.

Interesting, I am not sure why it is missing for me. I use nsenter.
May be differnce between "ip netns" and "nsenter". nsenter give access to
mountns and utsns.

When I look inside namespace using nsenter. I see the host's  
/sys/class/net/ and there is no eth0 there.

/sys/class/net/ sudo /usr/bin/nsenter 
--net=/run/netns/east-xauth-pluto-13 --uts=/run/utsns/east-xauth-pluto-13 

ip link show dev eth1
144: eth1 at if145: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP mode DEFAULT group default qlen 1000
    link/ether 82:1d:f3:3f:2c:84 brd ff:ff:ff:ff:ff:ff link-netnsid 0

ls -lt /sys/class/net/eth0
ls: cannot access 'ls': No such file or directory

my theory is host end exist in /sys/class/net ; ie ifindex 145 exist but not 

grep 145 /sys/class/net/*/ifindex

grep 144  /sys/class/net/*/ifindex
is empty 

More information about the Swan-dev mailing list