import subprocess
import optparse
import re
def get_arguments():
parser = optparse.OptionParser()
parser.add_option("-i", "--interface", dest="interface", help="Interface for changing its MAC address")
parser.add_option("-m", "--mac", dest="new_mac", help="New MAC address")
(options, args) = 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 address, use --help for more info.")
else:
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"])
options = get_arguments()
change_mac(options.interface, options.new_mac)
ifconfig_result = subprocess.check_output(["ifconfig", options.interface])
print(ifconfig_result)
mac_address_search_result = re.search(r"\w\w:\w\w:\w\w:\w\w:\w\w:\w\w", ifconfig_result)
print("[+] Your current MAC address is: ", mac_address_search_result.group(0))
I wrote that in Pycharm in Kali Linux in my Windows 11 Virtualbox. Then in kali Terminal Emulator (as root user) typed the following command:
python mac_changer.py -i eth0 -m 00:11:22:22:22:99
The output is:
[+] Changing MAC address for eth0 to 00:11:22:22:22:99
b'eth0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500\n inet 10.0.2.16 netmask 255.255.255.0 broadcast 10.0.2.255\n ether 00:11:22:22:22:99 txqueuelen 1000 (Ethernet)\n RX packets 32294 bytes 36342047 (34.6 MiB)\n RX errors 0 dropped 0 overruns 0 frame 0\n TX packets 20187 bytes 1888532 (1.8 MiB)\n TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0\n\n'
Traceback (most recent call last):
File "/home/kali/PycharmProjects/mac_changer/mac_changer.py", line 30, in <module>
mac_address_search_result = re.search(r"\w\w:\w\w:\w\w:\w\w:\w\w:\w\w", ifconfig_result)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/lib/python3.11/re/__init__.py", line 176, in search
return _compile(pattern, flags).search(string)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: cannot use a string pattern on a bytes-like object
What’s the problem here?