python - 为什么输出显示内存代码?
问题描述
我试图从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
解决方案
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'}]
推荐阅读
- python - 如何让 pprint.pprint 理解换行符?
- java - Mouselistener 不使用 if 语句
- laravel-5.4 - 在 laravel 4.2 和 5+ 中获取每周/每月记录?
- c++ - C++ GDI+ 清除前一个屏幕
- vue.js - 导入组件时未找到 VueJS 模块错误
- android - Android相机预览有“花边”或“马赛克”的边框使用SurfaceTexture,为什么图片不能完全填满屏幕?
- c# - 社区工具包控件的 XAML 错误
- django - django 如何确保一个持久连接不会同时被多线程共享?
- angular - 如何在 ng build 上添加“严格”代码检查(没有生产标志)?
- scala - 将完整类型提升为单子变压器