【问题标题】:Why is the output displaying memory code?为什么输出显示内存代码?
【发布时间】:2020-01-30 17:29:42
【问题描述】:

我正在尝试将 MAC 地址与 ifconfig 命令隔离开来,但输出显示的是我认为是内存代码的内容。我在 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

【问题讨论】:

  • str(mac_1),mac_1 是一个函数,因此它会打印该函数在内存中的位置。
  • 改为致电str(mac_1, 'eth0')
  • mac_1() 不返回任何内容。你希望它打印什么?
  • @samthegolden 你的意思是str(mac_1('eth0'))
  • 是的@Barmar 我的坏

标签: python regex memory subprocess output


【解决方案1】:

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

def mac_1(network):  # network is the argument

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

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

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

【讨论】:

    【解决方案2】:

    我几天前就知道了,还没有时间回复。这是我一直在寻找的结果:

    输入

    #!/usr/bin/env python
    import subprocess
    import re
    
    output_check = subprocess.check_output(['ifconfig'])
    check = re.search(r"\w\w:\w\w:\w\w:\w\w:\w\w:\w\w", output_check)
    
    print("Mac Address is: " + check.group(0))
    

    输出

    Mac Address is: 08:69:18:f9:85:06
    

    谢谢大家的帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-09
      • 1970-01-01
      • 2017-09-21
      相关资源
      最近更新 更多