Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!usr/bin/env python
- #Call the modules
- import subprocess
- import optparse
- import re
- #Create a function to record input through commands
- def get_input():
- parser = optparse.OptionParser()
- parser.add_option("-i", "--interface", dest="interface", help="Interface to change its MAC adress")
- parser.add_option("-m", "--mac", dest="new_mac", help="New MAC adress")
- (options, arguments) = parser.parse_args()
- if not options.interface:
- parser.error("[-] Please specify an interface, use --help for more info.")
- elif not options.new_mac:
- parser.error("[-] Please specify a MAC adress, use --help for more info.")
- return options
- #Create a function for this code
- def change_mac(interface, new_mac):
- print("[+] Changing MAC adress for " + interface + " to " + new_mac)
- subprocess.call(["ifconfig", interface, "down"])
- subprocess.call(["ifconfig", interface, "hw", "ether", new_mac])
- subprocess.call(["ifconfig", interface, "up"])
- def get_current_mac(interface):
- ifconfig_result = subprocess.check_output(["ifconfig", interface])
- mac_adress_search_result = re.search(r"\w\w:\w\w:\w\w:\w\w:\w\w:\w\w", ifconfig_result)
- if mac_adress_search_result:
- return mac_adress_search_result.group(0)
- else:
- print("[-] Could not read MAC adress")
- #Calling the functions
- options = get_input()
- current_mac = get_current_mac(options.interface)
- print("Current MAC: " + str(current_mac))
- change_mac(options.interface, options.new_mac)
- current_mac = get_current_mac(options.interface)
- if current_mac == options.new_mac:
- print("[+] MAC adress changed succesfully to " + current_mac)
- else:
- print("[-]MAC adress did not get changed due to an unexpected error.")
- #EXPLANATORY LINES NOT USED
- #-----------------------------
- #Options to record input through commands
- #parser = optparse.OptionParser()
- #parser.add_option("-i", "--interface", dest="interface", help="Interface to change its MAC adress")
- #parser.add_option("-m", "--mac", dest="new_mac", help="New MAC adress")
- #(options, arguments) = parser.parse_args()
- #Ask the user directly about the options
- #interface = input("Interface? ")
- #new_mac = input("New MAC? ")
- #User input through command(without definig function)
- #interface = options.interface
- #new_mac = options.new_mac
- #User information about the process
- #print("[+] Changing MAC adress for " + interface + " to " + new_mac)
- #Commands executed
- #subprocess.call(["ifconfig", interface, "down"])
- #subprocess.call(["ifconfig", interface, "hw", "ether", new_mac])
- #subprocess.call(["ifconfig", interface, "up"])
- #Less secure alternative way
- #subprocess.call("ifconfig " + interface + " down", shell=True)
- #subprocess.call("ifconfig " + interface + " hw ether " + new_mac, shell=True)
- #subprocess.call("ifconfig " + interface + " up", shell=True)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement