Advertisement
sM4rT

mac_changer

Nov 16th, 2018
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.84 KB | None | 0 0
  1. #!usr/bin/env python
  2.  
  3. #Call the modules
  4. import subprocess
  5. import optparse
  6. import re
  7.  
  8. #Create a function to record input through commands
  9. def get_input():
  10.     parser = optparse.OptionParser()
  11.     parser.add_option("-i", "--interface", dest="interface", help="Interface to change its MAC adress")
  12.     parser.add_option("-m", "--mac", dest="new_mac", help="New MAC adress")
  13.     (options, arguments) = parser.parse_args()
  14.     if not options.interface:
  15.         parser.error("[-] Please specify an interface, use --help for more info.")
  16.     elif not options.new_mac:
  17.         parser.error("[-] Please specify a MAC adress, use --help for more info.")
  18.     return options
  19.  
  20. #Create a function for this code
  21. def change_mac(interface, new_mac):
  22.     print("[+] Changing MAC adress for " + interface + " to " + new_mac)
  23.     subprocess.call(["ifconfig", interface, "down"])
  24.     subprocess.call(["ifconfig", interface, "hw", "ether", new_mac])
  25.     subprocess.call(["ifconfig", interface, "up"])
  26.  
  27. def get_current_mac(interface):
  28.     ifconfig_result = subprocess.check_output(["ifconfig", interface])
  29.     mac_adress_search_result = re.search(r"\w\w:\w\w:\w\w:\w\w:\w\w:\w\w", ifconfig_result)
  30.     if mac_adress_search_result:
  31.         return mac_adress_search_result.group(0)
  32.     else:
  33.         print("[-] Could not read MAC adress")
  34.  
  35. #Calling the functions
  36. options = get_input()
  37.  
  38. current_mac = get_current_mac(options.interface)
  39. print("Current MAC: " + str(current_mac))
  40.  
  41. change_mac(options.interface, options.new_mac)
  42.  
  43. current_mac = get_current_mac(options.interface)
  44. if current_mac == options.new_mac:
  45.     print("[+] MAC adress changed succesfully to " + current_mac)
  46. else:
  47.     print("[-]MAC adress did not get changed due to an unexpected error.")
  48.  #EXPLANATORY LINES NOT USED
  49. #-----------------------------
  50. #Options to record input through commands
  51. #parser = optparse.OptionParser()
  52. #parser.add_option("-i", "--interface", dest="interface", help="Interface to change its MAC adress")
  53. #parser.add_option("-m", "--mac", dest="new_mac", help="New MAC adress")
  54. #(options, arguments) = parser.parse_args()
  55.  
  56. #Ask the user directly about the options
  57. #interface = input("Interface? ")
  58. #new_mac = input("New MAC? ")
  59.  
  60. #User input through command(without definig function)
  61. #interface = options.interface
  62. #new_mac = options.new_mac
  63.  
  64. #User information about the process
  65. #print("[+] Changing MAC adress for " + interface + " to " + new_mac)
  66.  
  67. #Commands executed
  68. #subprocess.call(["ifconfig", interface, "down"])
  69. #subprocess.call(["ifconfig", interface, "hw", "ether", new_mac])
  70. #subprocess.call(["ifconfig", interface, "up"])
  71.  
  72. #Less secure alternative way
  73. #subprocess.call("ifconfig " + interface + " down", shell=True)
  74. #subprocess.call("ifconfig " + interface + " hw ether " + new_mac, shell=True)
  75. #subprocess.call("ifconfig " + interface + " up", shell=True)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement