Posted By

ksaver on 04/08/11


Tagged

random macaddress


Versions (?)

macrandom.py


 / 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.

  1. #! /usr/bin/env python
  2. # macrandom.py
  3. # Changes network interface card (nic) mac address
  4. # for a random one. :)
  5. # Required to run with sudo:
  6. # $ sudo ./macrandom.py [wlanN]
  7. # Works on FreeBSD or Linux.
  8. # ksaver, April 2011.
  9. # Public Domain Code.
  10.  
  11. import os
  12. import platform
  13. import random
  14. import sys
  15.  
  16. default_iface = 'ath0'
  17.  
  18. def random_mac():
  19. mac = ['00']
  20. for i in xrange(5):
  21. octet = random.randint(0, 255)
  22. mac.append('%02X' % octet)
  23. return ':'.join(mac)
  24.  
  25. def main(iface):
  26. new_mac = random_mac()
  27.  
  28. if 'freebsd' in platform.system().lower():
  29. ifconf_cmd = '/sbin/ifconfig %s ether %s' % (iface, new_mac)
  30. elif 'linux' in platform.system().lower():
  31. ifconf_cmd = '/sbin/ifconfig %s down hw ether %s; \
  32. /sbin/ifconfig %s up' % (iface, new_mac, iface)
  33. else:
  34. print 'Your OS is not currently supported.\n'
  35. sys.exit(1)
  36.  
  37. do_ifconf = os.system(ifconf_cmd)
  38. if do_ifconf == 0:
  39. print '[OK] Mac address of %s changed to %s.\n' % (iface, new_mac)
  40. else:
  41. print '[ERR] %s not changed.' % iface
  42.  
  43. if __name__ == '__main__':
  44. if len(sys.argv) > 1:
  45. main(sys.argv[1])
  46. else:
  47. main(default_iface)

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: ksaver on April 8, 2011

I know you can improve the code. Your comments are welmome (and encouraged) :)

Posted By: ksaver on April 8, 2011
  • I mean, Welcome! :)
Posted By: ghoulmaster on April 8, 2011

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 :)

Posted By: ksaver on April 8, 2011

@ghoulmaster Right now I'm checking %X :) I will be waiting for your code... Thank you for your suggestions!!

Posted By: ksaver on April 8, 2011

mac.append('%02X' % octet)

:-O

Posted By: ghoulmaster on April 9, 2011

:) 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 :)

Posted By: ksaver on April 9, 2011

@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 :-)

You need to login to post a comment.