fbpx

How to use Python to create a MAC Address changer

In this tutorial we will build a mac address changer using Pycharm. The result will be a working product where you no longer have to go through the commands to bring your interface down, change the address, and bring it back up. It will display the current mac address, the changing mac address, and display if it was changed successfully or not. Saving you time in commands!

Subscribe to The Secret Letters of a Hacker
#!/usr/bin/env python

import subprocess
import optparse
import re


def get_arguments():
    parser = optparse.OptionParser()
    parser.add_option("-i", "--interface", dest="interface", help="Interface to change MAC address")
    parser.add_option("-m", "--mac", dest="new_mac", help="New MAC address")
    (options, arguments) = parser.parse_args()
    if not options.interface:
        parser.error("[-] need interface DUHHH")
    elif not options.new_mac:
        parser.error("[-] need mac address DUHHH")
    return options


def change_mac(interface, new_mac):
    print("[+] Changing MAC address 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_address_search_result = re.search(r"\w\w:\w\w:\w\w:\w\w:\w\w:\w\w", ifconfig_result)

    if mac_address_search_result:
        return mac_address_search_result.group(0)
    else:
        print("[-] No MAC address found")


options = get_arguments()

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 address successfully changed to " + current_mac)
else:
    print("[-] MAC address did not change")

music credited by:

Chee Zee Cave – Video Classica by Kevin MacLeod is licensed under a Creative Commons Attribution license (https://creativecommons.org/licenses/by/4.0/)
Source: http://incompetech.com/music/royalty-free/index.html?isrc=USUAN1100685
Artist: http://incompetech.com/

Leave Comment

Your email address will not be published.

0

Your Cart