首页 > 解决方案 > 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.

标签: linuxpython-3.x

解决方案


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.


推荐阅读