Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.9k views
in Technique[技术] by (71.8m points)

in python using re.py to extract from a substring error

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Probably, you have to decode the output of subprocess.check_output:

subprocess.check_output(["ifconfig", options.interface]).decode('utf-8')

Take a look at the documentation of check_output:

By default, this function will return the data as encoded bytes. The actual encoding of the output data may depend on the command being invoked, so the decoding to text will often need to be handled at the application level.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...