在我的函数中得到“ TypeError:无法在类似字节的对象上使用字符串模式”。我正在尝试将MAC地址打印到屏幕上

我的get_current_mac函数:

def get_current_mac(interface):
    ifconfig_result = subprocess.check_output(["ifconfig",interface])
    mac_address_search_result = re.search(r"\w\w:\w\w:\w\w:\w\w:\w\w:\w\w",ifconfig_result)

    if mac_address_search_result:
        return mac_address_search_result.group(0)
    else:
        print("[-] Could not read MAC address.")

然后我想将结果打印到屏幕上

options = get_arguments()
current_mac = get_current_mac(options.interface)
print("Current MAC address is: " + str(current_mac))

整个错误是:

Traceback (most recent call last):
  File "./MAC_changer.py",line 38,in <module>
    current_mac = get_current_mac(options.interface)
  File "./MAC_changer.py",line 30,in get_current_mac
    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.7/re.py",line 183,in search
    return _compile(pattern,flags).search(string)
TypeError: cannot use a string pattern on a bytes-like object

第38行是:options = get_arguments()

第30行是:mac_address_search_result = re.search(r"\w\w:\w\w:\w\w:\w\w:\w\w:\w\w",ifconfig_result)

hzcker 回答:在我的函数中得到“ TypeError:无法在类似字节的对象上使用字符串模式”。我正在尝试将MAC地址打印到屏幕上

您需要将类似字节的对象转换为字符串,这可以使用decode方法来完成:

ifconfig_result = ifconfig_result.decode('utf-8')

this link中,您可以了解有关将字节转换为字符串的更多信息

本文链接:https://www.f2er.com/2788720.html

大家都在问