【发布时间】:2021-01-10 09:55:52
【问题描述】:
我正在参加一门 Python 课程,我正在学习“使用正则表达式提取子字符串”部分。例如,我们正在构建一个 MAC 地址转换器。
到目前为止,这是我的代码:
#!/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"\w\w:\w\w:\w\w:\w\w:\w\w", ifconfig_result)
print(mac_address_search_result.group(0))
据我在教程中看到的,这是应该的,但我收到以下错误:
Traceback (most recent call last):
File “mac_changer.py”, line 32, in
mac_address_search_result = re.search(r"\w\w:\w\w:\w\w:\w\w:\w\w", 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
我在 google 上查看了有关此错误的一些信息,但我没有找到任何适合 mac 转换器的信息。
澄清一下,我正在 Linux kali 下构建和运行它,它在终端中运行。
【问题讨论】:
标签: python