使用 Python 搜索具有 MAC 地址的主机
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/85577/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Search for host with MAC-address using Python
提问by Nate
I'd like to search for a given MAC address on my network, all from within a Python script. I already have a map of all the active IP addresses in the network but I cannot figure out how to glean the MAC address. Any ideas?
我想在我的网络上搜索给定的 MAC 地址,所有这些都来自 Python 脚本。我已经有了网络中所有活动 IP 地址的映射,但我不知道如何收集 MAC 地址。有任何想法吗?
回答by Allen
You need ARP. Python's standard library doesn't include any code for that, so you either need to call an external program (your OS may have an 'arp' utility) or you need to build the packets yourself (possibly with a tool like Scapy.
你需要ARP。Python 的标准库不包含任何代码,因此您要么需要调用外部程序(您的操作系统可能有一个“arp”实用程序),要么需要自己构建数据包(可能使用像Scapy这样的工具。
回答by Benjamin W. Smith
I don't think there is a built in way to get it from Python itself.
我认为没有内置的方法可以从 Python 本身获取它。
My question is, how are you getting the IP information from your network?
我的问题是,您如何从网络中获取 IP 信息?
To get it from your local machine you could parse ifconfig (unix) or ipconfig (windows) with little difficulty.
要从本地计算机获取它,您可以轻松解析 ifconfig (unix) 或 ipconfig (windows)。
回答by Sylvain Defresne
If you want a pure Python solution, you can take a look at Scapyto craft packets (you need to send ARP request, and inspect replies). Or if you don't mind invoking external program, you can use arping
(on Un*x systems, I don't know of a Windows equivalent).
如果你想要一个纯 Python 解决方案,你可以看看Scapy来制作数据包(你需要发送 ARP 请求,并检查回复)。或者,如果您不介意调用外部程序,则可以使用arping
(在 Un*x 系统上,我不知道 Windows 等效项)。
回答by Martin Cote
It seems that there is not a native way of doing this with Python. Your best bet would be to parse the output of "ipconfig /all" on Windows, or "ifconfig" on Linux. Consider using os.popen() with some regexps.
似乎没有使用 Python 执行此操作的本机方式。最好的办法是在 Windows 上解析“ipconfig /all”的输出,或者在 Linux 上解析“ifconfig”的输出。考虑使用 os.popen() 和一些正则表达式。
回答by EmmEff
You would want to parse the output of 'arp', but the kernel ARP cache will only contain those IP address(es) if those hosts have communicated with the host where the Python script is running.
您可能希望解析“arp”的输出,但内核 ARP 缓存将仅包含那些 IP 地址,前提是这些主机已与运行 Python 脚本的主机进行了通信。
ifconfig can be used to display the MAC addresses of local interfaces, but not those on the LAN.
ifconfig 可用于显示本地接口的 MAC 地址,但不能显示 LAN 上的 MAC 地址。
回答by Joe Skora
Mark Pilgrim describes how to do this on Windows for the current machine with the Netbios module here. You can get the Netbios module as part of the Win32 package available at python.org. Unfortunately at the moment I cannot find the docs on the module.
Mark Pilgrim 描述了如何在 Windows 上使用 Netbios 模块在当前机器上执行此操作。您可以在 python.org 上获取作为 Win32 包的一部分的 Netbios 模块。不幸的是,目前我找不到模块上的文档。
回答by nsayer
Depends on your platform. If you're using *nix, you can use the 'arp' command to look up the mac address for a given IP (assuming IPv4) address. If that doesn't work, you could ping the address and then look, or if you have access to the raw network (using BPF or some other mechanism), you could send your own ARP packets (but that is probably overkill).
取决于你的平台。如果您使用 *nix,则可以使用“arp”命令查找给定 IP(假设为 IPv4)地址的 mac 地址。如果这不起作用,您可以 ping 地址然后查看,或者如果您可以访问原始网络(使用 BPF 或其他一些机制),您可以发送自己的 ARP 数据包(但这可能有点矫枉过正)。
回答by akiva
as python was not meant to deal with OS-specific issues (it's supposed to be interpreted and cross platform), i would execute an external command to do so:
由于 python 不是为了处理特定于操作系统的问题(它应该被解释和跨平台),我会执行一个外部命令来这样做:
in unix the command is ifconfig
在 unix 中,命令是 ifconfig
if you execute it as a pipe you get the desired result:
如果将其作为管道执行,则会得到所需的结果:
import os
myPipe = os.popen2("/sbin/ifconfig","a")
print(myPipe[1].read())