/ Published in: Python
Examples:
$ sudo ./macrandom.py
[OK] Mac address of ath0 changed to 00:24:be:d2:a3:f6.
$ sudo ./macrandom.py wlan0
ifconfig: interface wlan0 does not exist
[ERR] wlan0 not changed.
Expand |
Embed | Plain Text
#! /usr/bin/env python # macrandom.py # Changes network interface card (nic) mac address # for a random one. :) # Required to run with sudo: # $ sudo ./macrandom.py [wlanN] # Works on FreeBSD or Linux. # ksaver, April 2011. # Public Domain Code. import os import platform import random import sys default_iface = 'ath0' def random_mac(): mac = ['00'] for i in xrange(5): octet = random.randint(0, 255) mac.append('%02X' % octet) return ':'.join(mac) def main(iface): new_mac = random_mac() if 'freebsd' in platform.system().lower(): ifconf_cmd = '/sbin/ifconfig %s ether %s' % (iface, new_mac) elif 'linux' in platform.system().lower(): ifconf_cmd = '/sbin/ifconfig %s down hw ether %s; \ /sbin/ifconfig %s up' % (iface, new_mac, iface) else: print 'Your OS is not currently supported.\n' sys.exit(1) do_ifconf = os.system(ifconf_cmd) if do_ifconf == 0: print '[OK] Mac address of %s changed to %s.\n' % (iface, new_mac) else: print '[ERR] %s not changed.' % iface if __name__ == '__main__': if len(sys.argv) > 1: main(sys.argv[1]) else: main(default_iface)
Comments
Subscribe to comments
You need to login to post a comment.

I know you can improve the code. Your comments are welmome (and encouraged) :)
hey! Well my first suggestion is to check out the %X string formatter for generating your MAC address ;) it should make your code much cleaner than the mess your currently using. I created something exactly like this with slightly different features a while back and its currently on a backup drive and I am on vacation so when i get back i will share my version of it and you can take w/e features you want from it :)
@ghoulmaster Right now I'm checking %X :) I will be waiting for your code... Thank you for your suggestions!!
mac.append('%02X' % octet)
:-O
:) see how clean that is? Oh and im getting a friend today to access my script and upload it to me so hopefully I can share it by the end of the day :)
@ghoulmaster Yes, I was trying to do that, but I was unable to figure out how to do it... Thanks for advising, and I'll stay online to watch out you script. Greets :-)