Jump to content

Python Script To Get Mac Address ...


Trip
 Share

Recommended Posts

import commands
def getmac(iface):
    data = commands.getoutput("ifconfig " + 1face)
    words = data.split()
    found = 0
    for x in words:
        #print x
        if found != 0:
            mac = x
            break
        if x == "HWaddr":
            found = 1
    if len(mac) == 0:
        mac = 'Mac not found'
    mac = mac[:17]
    return mac

im sure there is a better method for looping round the word list numerically rather than having to wait for the next step to grab the mac ... ie.

if words[x].strip() == 'HWaddr':

mac = words[x+1]

break

but havent found an easy way of grabbing the upper bound number from the string array.

if you know a better way of doing this please let me know ;0)

Edited by Trip
Link to comment
Share on other sites

Okay I got a couple ideas for yea..

If you wanna keep it sexy and pythonic, how about some RegEx B)

import commands, re
def getmac(iface):
    ifconfig = commands.getoutput("ifconfig " + iface)
    mac = re.search('\w\w:\w\w:.+\n', ifconfig)
    if mac is None:
        parsedMac = 'Mac not found'
    else:
        parsedMac = mac.group()
    print parsedMac #Or use return here.
getmac('eth0')

Considering your rockin' ifconfig already, could also get kinda bourne with it (More my style :D )

import commands
def getmac(iface):
    ifconfig = commands.getoutput("ifconfig " + iface + "| grep HWaddr | awk '{ print $5 }'")
    if len(ifconfig) != 17:
        mac = 'Mac not found'
    else:
        mac = ifconfig
    print mac #Or use return here.
getmac('eth0')

Hope this helps you out a bit.. If you wanna dive into the 1337 world of python, I recommend these awsome videos from google to start out.

http://www.youtube.com/results?search_query=google+python+class&aq=f

Enjoy!

Link to comment
Share on other sites

nice dude ill check them out in 2mins ...

check this out ... :D

import commands
def getmac(iface):
    words = commands.getoutput("ifconfig " + iface).split()	
    for x in range(0,len(words)):
        if words[x].strip() == "HWaddr":
            mac = words[x+1].strip()
            break
    if len(mac) == 0:
        mac = 'Mac not found'
    mac = mac[:17]
    return mac

much better :D

just checked yours ... this would be better

import commands
def getmac(iface):
    ifconfig = commands.getoutput("ifconfig " + iface + "| grep HWaddr | awk '{ print $5 }'").strip() #<< added strip to remove leading / trailing spaces

    if len(ifconfig) == 0:
        mac = 'Mac not found'
    else:
        mac = ifconfig[:17] #<< return the left 17 characters
    return mac

sometimes aircrack messes up my mac address

rather than 00:22:77:33:66:44

it shows up as ...

00:22:77:33:66:44:00:00:00:00:00:00

thats why i added the left 17 thing ;)

just tested it ... works beautifully

import commands
def getmac(iface):
    ifconfig = commands.getoutput("ifconfig " + iface + "| grep HWaddr | awk '{ print $5 }'").strip()
    if len(ifconfig) == 0:
        mac = 'Mac not found'
    else:
        mac = ifconfig[:17]
    return mac
print getmac('wlan0')

Edited by Trip
Link to comment
Share on other sites

Ah, kind of a weird issue. But I have seen that 00:00:00:00:00 pop up with airdrop and airodump a few times as well, never really figured out why.

But hey whatever gets the job done! Hope it helped, Good luck.

Link to comment
Share on other sites

Ah, kind of a weird issue. But I have seen that 00:00:00:00:00 pop up with airdrop and airodump a few times as well, never really figured out why.

But hey whatever gets the job done! Hope it helped, Good luck.

hehe wanna feel leet dude ??? ....

def getmac():
	""" returns the MAC address of the current interface """
	global IFACE

	proc_mac = subprocess.Popen(['ifconfig',IFACE], stdout=subprocess.PIPE, stderr=open(os.devnull, 'w'))
	proc_mac.wait()
	lines = proc_mac.communicate()[0]
	if lines == None:
		return 'NO MAC'

	for line in lines:
		line = lines.split('\n')[0]
		line=line[line.find('HWaddr ')+7:]
		if line.find('-') != -1:
			macnum=line.split('-')
			mac=''
			for i in xrange(0, len(macnum)):
				mac=mac+macnum[i]
				if i < 5:
					mac=mac+':'
				else:
					break
			return mac
		else:
			return line.strip()

look at the mess that is in wifite LOL omg even my code is better hehe yours is uber ROTFL

yo dude check this out ... http://code.google.com/p/wifite/issues/detail?id=48

Edited by Trip
Link to comment
Share on other sites

Yeah my god.. All those nested iterations and statements.. I'm not sure if it could be any more un-pythonic lol

And I am honestly no python master, I consider myself a beginner at best really..

If you wanna check out some real pro's hop into #python. Some of the guys in there really know there stuff (although some don't always explain it in the easiest manner in my experience).

Edited by leg3nd
Link to comment
Share on other sites

  • 1 year later...

Come one guys, you've written way too much code for something so simple. Try this:

#!/usr/bin/python 

import commands

def getmac(iface):
    words = commands.getoutput("ifconfig " + iface).split()
    if "HWaddr" in words:
	return words[ words.index("HWaddr") + 1 ]
    else:
        return 'MAC Address Not Found!'

if __name__ == "__main__":
	print getmac( "wlan0" )

You can also swap out 'if' and 'else' with 'try' and 'except' if you would like to raise an exception error on not finding the MAC address.

Link to comment
Share on other sites

Another way for no reason other than I'm bored.

#!/usr/bin/python

import os

def findmac(iFace):

    f=os.popen('ifconfig ' + iFace)
    output = f.read()
    f.close()

    if 'HWaddr' in output:
        return 'MAC ADDRESS: ' + output[(output.find('HWaddr')+6):(output.find('HWaddr')+24)]

    else:
        return 'MAC NOT FOUND!'

if __name__ == '__main__':
    print findmac('wlan0')

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...