LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Ethernet/MAC address using Python (https://www.linuxquestions.org/questions/linux-newbie-8/ethernet-mac-address-using-python-846166/)

tank junior 11-23-2010 10:06 PM

Ethernet/MAC address using Python
 
Hi,

How do you get correct ethernet/mac id of your local network card using python script?

Most of the article suggests to parse the output of ipconfig /all on windows and ifconfig on linux. I am sure that this solution would work on windows but I am a linux starter and not sure the reliability of ifconfig on linux distros.

Couple of other solutions on linux are:
Code:

def getHwAddr(ifname):
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    info = fcntl.ioctl(s.fileno(), 0x8927,  struct.pack('256s', ifname[:15]))
    return ''.join(['%02x:' % ord(char) for char in info[18:24]])[:-1]

getHwAddr("eth0")

Code:

ifname = 'eth0'
print open('/sys/class/net/%s/address' % ifname).read()

I am also unsure about privileges when using above solutions on *nix platforms.
An article here says that you have to be root or need appropriate permissions to use ifconfig command.

Need some pointers....

Cheers

Prashant

MS3FGX 11-23-2010 10:16 PM

What do you mean by reliability? ifconfig is a standard Unix command, it will be available on any Linux distro and the switches and output should always remain the same. Even if there were slight variations, it wouldn't be anything that would effect something as simple as getting and setting the IP.

Dark_Helmet 11-23-2010 10:23 PM

Quote:

I am sure that this solution would work on windows but I am a linux starter and not sure the reliability of ifconfig on linux distros.
ifconfig is part of the net-tools package. The net-tools package is no longer maintained. iproute2 is a new collection of tools intended to replace net-tools.

Two things however:
1. iproute2 was supposed to replace net-tools beginning around the 2.2 kernel release.
2. I have still not seen a linux distribution that doesn't have ifconfig

So, it's taking a long time to abandon net-tools. You're not likely to run into problems relying on ifconfig for quite some time to come. Conversely, your script should anticipate iproute2 for the future. ideally, your script would support both.

Regarding permissions... you do not need root permissions to run ifconfig. With no arguments, ifconfig displays information about the network interfaces--everything you're looking for. You only need root privileges if you want to change how a network interface is configured (IP address, netmask, etc.). I'm not familiar with the tools in iproute2, but I imagine a non-root user would be able to display network configuration just like with ifconfig.

EDIT: Here's a wikipedia link: ifconfig. And, though it wasn't meant this way, MS3FGX's comment is anecdotal evidence of how little has happened in switching to iproute2. It's hard to switch when the general population isn't aware that there's supposed to be a switch.


All times are GMT -5. The time now is 09:44 PM.