Mac Changer Code in Python – 4

Mac Changer Code in Python – 4

import subprocess
import re
import optparse

def change_mac(interface,new_mac):
     print("[+] Changing MAC address of Interface" + interface + "to" +new_mac)
     subprocess.call(["ifconfig", interface,"down"])
     subprocess.call(["ifconfig", interface, "hw", "ether", new_mac])
     subprocess.call(["ifconfig", interface, "up"])

def get_arg():
    parser = optparse.OptionParser()
    parser.add_option('-i', dest='interface', help="Please Enter the Interface name for which You want to change the MAC address")
    parser.add_option('-m', dest='mac', help='Please enter the new MAC')
    (options, argumants) = parser.parse_args()
    if not options.interface:
        print("Please Enter Valid Interface Name")
    if not options.mac:
        print("Please Enter Valid MAC")
    return parser.parse_args()

def get_current_mac(interface):
    ifconfig_result=subprocess.check_output(["ifconfig", interface])
    mac_address_search_result=re.search(r"\w\w:\w\w:\w\w:\w\w",ifconfig_result)

    if mac_address_search_result:
        return mac_address_search_result.group(0)
    else:
        print("[-] Could Not read MAC Address of ", interface)

options = get_arg()

current_mac=get_current_mac(options.interface)
print("Current MAC address is: ", current_mac)
change_mac(options.interface, options.mac)
current_mac=get_current_mac(options.interface)

if current_mac==options.mac:
    print("Success")
else:
    print("Unsuccessful")