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): Code:
ifname = 'eth0' An article here says that you have to be root or need appropriate permissions to use ifconfig command. Need some pointers.... Cheers Prashant |
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.
|
Quote:
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. |