Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now!
  #1  
Old February 3rd, 2003, 05:43 AM
surajit surajit is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Posts: 2 surajit User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Question Ethernet card address Through Python or C

I'm getting a problem to access the ethernet card address through python.
There are two ways to do it:

1. use "/sbin/ifconfig" and then do the string manipulation after taking the output using popen.

>>> import os
>>> p=os.popen("/sbin/ifconfig eth0")
>>> t=p.read()
>>> p.close()
>>> import re
>>> re.search("HWaddr ([0-9a-fA-F:]+)",t).group(1)
'00:00:86:5F:DF:AC'

BUT ....

I'm not sure this will run in a language independent way. If I'm using Linux with German lang. then will this work? Actually it is the most crude way to arrive to the solution.

I've progressed a little bit in the second way..

2. use the socket and fcntl and IN (linux specific library).

import socket
import fcntl
import IN
s= socket.socket(socket.AF_INET,socket.SOCK_STREAM)
fd = s.fileno()
fcntl.ioctl(fd,IN.SIOCSIFHWADDR,'')
But the last line raises error..

Traceback (most recent call last):
File "<stdin>", line 1, in ?
IOError: [Errno 19] No such device

Anyway I want in linux only not in a platform independent way. For windows I've already solved the problem with netbios library.

I'll expect the solution in Linux platform in a language independent way.

Reply With Quote
  #2  
Old April 14th, 2003, 10:23 PM
jstewart jstewart is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2003
Posts: 1 jstewart User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Try this:

import socket
import fcntl
import string
import sys
import exceptions

def getINTF():
f = open("/proc/net/dev")
ret = []
iflist = f.readlines()
f.close()
for line in iflist:
if ':' not in line:
continue
words=string.split(line,':')
ifname, rest = words[0],words[1:]
ifname = string.strip(ifname)
ret.append(ifname)
return ret

def hexy(n):
return "%02x" % (ord(n))

def getMAC(ifname):
SIOCGIFHWADDR = 0x8927 # magic number
s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
ifname = string.strip(ifname)
ifr = ifname + '\0'*(32-len(ifname))
try:
r= fcntl.ioctl(s.fileno(),SIOCGIFHWADDR,ifr)
addr = map(hexy,r[18:24])
ret = (':'.join(map(str, addr)))
except IOError, e:
ret = ''
return ret

list = getINTF()
for i in list:
print i, getIP(i), getMAC(i), getUP(i)

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Ethernet card address Through Python or C


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway