Advertisement
Boomer101

mac_changer.py

Nov 9th, 2020
25
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.60 KB | None | 0 0
  1. #!/bin/env python
  2.  
  3. import subprocess
  4. import optparse
  5. import re
  6. def get_arguments():
  7. parser = optparse.OptionParser()
  8. parser.add_option("-i", "--Interface", dest="interface", help="Interface to change MAC address")
  9. parser.add_option("-m", "--mac", dest="new_mac", help="New MAC address")
  10. (options, arguments) = parser.parse_args()
  11. if not options.interface:
  12. parser.error("[-] Please specify Interface use --help for more info. ")
  13. elif not options.new_mac:
  14. parser.error("[-] Please specify new MAC use --help for more info. ")
  15. return options
  16. return parser.parse_args()
  17.  
  18. def change_mac(interface,new_mac):
  19. print("[+] Changing MAC Address for " + interface + " to " + new_mac)
  20. subprocess.call(["ifconfig", interface, "down"])
  21. subprocess.call(["ifconfig", interface, "hw", "ether", new_mac])
  22. subprocess.call(["ifconfig", interface, "up"])
  23.  
  24. def get_current_mac(interface):
  25. ifconfig_result = subprocess.check_output(["ifconfig", interface])
  26. mac_address_search_results = re.search(r"\w\w:\w\w:\w\w:\w\w:\w\w:\w\w", str(ifconfig_result))
  27.  
  28. if mac_address_search_results:
  29. return mac_address_search_results.group(0)
  30. else:
  31. print("[-] Could not read MAC address ")
  32.  
  33. options = get_arguments()
  34. current_mac = get_current_mac(options.interface)
  35. print("Current MAC = " + str(current_mac))
  36.  
  37. change_mac(options.interface, options.new_mac)
  38. current_mac = get_current_mac(options.interface)
  39. if current_mac ==options.new_mac:
  40. print("[+] MAC address was changed to " + current_mac)
  41. else:
  42. print("[-] MAC address did not change")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement