Fang
Fang

Reputation: 2419

Getting the permanent address of an ethernet device

I'm looking for a way to retrieve the permanent address of an ethernet device in Python(3).

I've looked at various ways to do this:

I've looked at the code for ethtool, and the information I need seems to live in a field of struct dev called perm_addr:

static int ethtool_get_perm_addr(struct net_device *dev, void __user *useraddr)
{
    struct ethtool_perm_addr epaddr;

    if (copy_from_user(&epaddr, useraddr, sizeof(epaddr)))
        return -EFAULT;

    if (epaddr.size < dev->addr_len)
        return -ETOOSMALL;
    epaddr.size = dev->addr_len;

    if (copy_to_user(useraddr, &epaddr, sizeof(epaddr)))
        return -EFAULT;
    useraddr += sizeof(epaddr);
    if (copy_to_user(useraddr, dev->perm_addr, epaddr.size))
        return -EFAULT;
    return 0;
}

How can I access this information in Python? I'd prefer an all Python solution over opening a subprocess and calling ethtool.

Upvotes: 0

Views: 811

Answers (2)

Francois Scheurer
Francois Scheurer

Reputation: 1

#address named by userspace:
    ip a | egrep -A1 ${IFACE}:
    egrep -H . /sys/class/net/*/addr_assign_type /sys/class/net/*/address | egrep ${IFACE}/
#permanent address:
    ethtool -P ${IFACE} | egrep -i permanent
    egrep -H . /sys/class/net/${IFACE}/bonding_slave/perm_hwaddr
    egrep -H "Slave Interface:|Permanent HW addr:" /proc/net/bonding/storage | egrep -A1 "${IFACE}\$"
    #sles/centos: hwinfo --network | egrep " Permanent HW Address|SysFS ID" | egrep -A1 "${IFACE}\$"

Upvotes: 0

user2849202
user2849202

Reputation:

It is available through the file /sys/class/net/<iface>/address. The file /sys/class/net/<iface>/addr_assign_type indicates the type of address.

See https://www.kernel.org/doc/Documentation/ABI/testing/sysfs-class-net, and look for 'permanent' in that document.

Upvotes: 2

Related Questions