LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 11-23-2010, 10:06 PM   #1
tank junior
Member
 
Registered: Apr 2010
Posts: 42

Rep: Reputation: 0
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
 
Old 11-23-2010, 10:16 PM   #2
MS3FGX
Guru
 
Registered: Jan 2004
Location: NJ, USA
Distribution: Slackware, Debian
Posts: 5,852

Rep: Reputation: 351Reputation: 351Reputation: 351Reputation: 351
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.
 
Old 11-23-2010, 10:23 PM   #3
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 369Reputation: 369Reputation: 369Reputation: 369
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.

Last edited by Dark_Helmet; 11-23-2010 at 10:28 PM.
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Getting MAC address from Ethernet packet in C++ eljofi Programming 9 10-13-2013 02:21 AM
Ethernet MAC Addresses database; MAC address-based processing cctualatin Linux - Newbie 1 04-14-2009 08:59 AM
changing ethernet mac address and retaining the same after reboot guy24x Linux - Hardware 3 02-06-2009 06:51 AM
Viewing ethernet activity using a MAC address noonmid27 Linux - Networking 6 04-03-2007 09:31 AM
Possible to change ethernet MAC address using kernel parameters? rollo Linux - Networking 2 02-23-2006 05:16 PM


All times are GMT -5. The time now is 04:09 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration