In this tutorial , we are going to implement a simple MAC address changer tool in Python .
Full Code of the MAC_changer tool can be found on Bhavya’s Github .
Now let’s begin !
First let’s import all the important modules that we are going to use in our code .
We imported 3 modules that we are going to use.
subprocess – This module allows us to run shell commands from our program . optparse – This module provides us a user friendly cli to use in our program. re – This module provides us the power of regular expressions .
Now lets see the second part of our program .
Here , we are creating a function named argument . The work of this function will be to parse the arguments provided while start of the program.
In this function , we created a object to the OptionParser class of optparse module Then we added two options to the parser object , First option is for the interface for which we are going to change the MAC address for . “-i or –interface” can be used to initialize the interface and the value will be stored in the destination variable interface . help is used to give information about the specific option and for the information on what it does . Second option is for the new MAC address that we are going to set for the specified interface . “-m or –mac ” can be used to initialize the interface and the value will be stored in the destination variable new_mac . Finally Arguments are parsed and returned from function .
A new function named checker is created . This function accepts two arguments a and b (interface argument and mac argument) . This function is used to check if the argument values – interface and mac address are not empty and valid . The if statement checks if the interface argument is empty then it prints the info message and exits the program . The elif statement checks if the mac address argument is empty then it prints the info message and exits the program . Now , regular expression is used to check if the provided MAC address is in correct format .If not, a info message is printed and the program is exited .
A function named is change_mac is created , this program’s role is to change the MAC address of the specified interface to specified MAC address .
IMPORTANT – A command returns 0 as its exit code in Linux if it runs successfully else a non zero number is returned if any error occurs .
first we are executing a command to shut down the interface using the pre installed utility in Linux , ifconfig , We are storing its exit code in a variable named first . Then we are executing a second command to change the specified interface’s MAC address to a new MAC address using the same ifconfig utility . We are storing the exit code for this command in a variable named second . Then we are executing a third command to start the interface back using the same ifconfig utility , we are storing exit code for this command in a variable named third . If any command fails to run successfully , an error message will be printed and the program will exit .
Now , the argument function is called and its return value ( arguments and etc ) are stored in varibles o , a .
Secondly , checker function is called with arguments as interface and new mac address to check if they are not empty and valid .
finally if everything went right , change_mac function is called with arguments as interface and new MAC address to do the real job .
Now if the MAC address was changed successfully , a success message is printed and program execution is completed .
use python3 macchanger.py –help for info .
use python3 macchanger.py -i interface -m new_mac_address to change the mac address of the specified interface .
Full code looks like this – check it out on github .
Voila , A simple MAC changer tool in Python !