I'm attending a Python course and I'm at the section on “Extracting a substring using Regex”. As an example, we are building a MAC address changer.
Here my code so far:
#!/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 its 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("[-] please specify an interface, use --help for more info")
elif not options.new_mac:
parser.error("[-] please specify a MAC, use --help for more info")
return options
def change_mac(interface, new_mac):
subprocess.call(["sudo", "ifconfig", interface, "down"])
subprocess.call(["sudo", "ifconfig", interface, "hw", "ether", new_mac])
subprocess.call(["sudo", "ifconfig", interface, "up"])
print("[+] Changing MAC address for " + interface + " to " + new_mac)
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"ww:ww:ww:ww:ww", ifconfig_result)
print(mac_address_search_result.group(0))
As far as I can see in the tutorial, this is how it is supposed to be, but I am getting the following error:
Traceback (most recent call last):
File “mac_changer.py”, line 32, in
mac_address_search_result = re.search(r"ww:ww:ww:ww:ww", ifconfig_result)
File “/usr/lib/python3.8/re.py”, line 201, in search
return _compile(pattern, flags).search(string)
TypeError: cannot use a string pattern on a bytes-like object
I've looked on google for some info about this error, but I've found nothing that adapts to the mac changer.
To clarify, I am building and running this under Linux kali and it runs in terminal.
question from:
https://stackoverflow.com/questions/65641826/in-python-using-re-py-to-extract-from-a-substring-error 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…