首页 > 解决方案 > 为什么输出显示内存代码?

问题描述

我试图从ifconfig命令中分离出 MAC 地址,但输出显示的是我认为是内存代码的内容。我在 VB Kali 上运行 python 2.7。

root@osboxes:~# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 10.0.2.22  netmask 255.255.255.0  broadcast 10.0.2.255
        inet6 fe80::1171:bf6a:17f8:972  prefixlen 64  scopeid 0x20<link>
        ether 08:00:27:f8:15:03  txqueuelen 1000  (Ethernet)
        RX packets 2593  bytes 3769210 (3.5 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 794  bytes 57174 (55.8 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

输入:

#!/usr/bin/env python

import subprocess
import re


def mac_1(network):
    output_check = subprocess.check_output(['ifconfig', network])
    re.search(r"\w\w:\w\w:\w\w:\w\w:\w\w:\w\w", output_check)
    # print(output_check)


print('[+] MAC Address is ' + str(mac_1))

输出代码:

/root/PycharmProjects/test001/venv/bin/python /root/PycharmProjects/test001/test001.py
[+] MAC Address is <function mac_1 at 0x7ff4ed70e3b0>

Process finished with exit code 0

标签: pythonregexmemorysubprocessoutput

解决方案


str(mac_1), mac_1 是一个函数,因此它正在打印该函数在内存中的位置。您需要使用定义中指定的参数调用它:

def mac_1(network):  # network is the argument

mac_1函数也没有返回类型,因此即使您使用参数调用它,它也只会 print None。因此,您需要返回搜索的输出。

虽然我可能建议只使用不同的netifaces。然后就这么简单

>>> addrs = netifaces.ifaddresses('eth0')
>>> addrs[netifaces.AF_LINK]
[{'addr': '00:12:34:56:78:9a'}]

推荐阅读