Linux 我可以从已知的 MAC 地址确定当前 IP 吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13552881/
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
Can I determine the current IP from a known MAC Address?
提问by ddoxey
I have a shell script which uses etherwake to wake up a machine on my local network. After the machine is awake, I'm not sure of the IP address.
我有一个 shell 脚本,它使用 etherwake 来唤醒本地网络上的机器。机器唤醒后,我不确定IP地址。
While trying to answer my own question I came up with:
在试图回答我自己的问题时,我想出了:
ip=$(ping -c 1 hostname | head -1 | awk '{print }' | sed 's/[()]//g')
This solution stipulates that I know the hostname of the remote machine, which isn't so onerous.
这个解决方案规定我知道远程机器的主机名,这并不麻烦。
Is there a way to get the IP if all I know is the MAC address?
如果我只知道 MAC 地址,有没有办法获得 IP?
采纳答案by Neal
I don't think there is a single command to do this. One hack would be to do a ping scan or a broadcast ping on the subnet and then query the arp table for the IP address of the MAC address. Obviously not an ideal solution. Example:
我认为没有一个命令可以做到这一点。一种方法是在子网上执行 ping 扫描或广播 ping,然后查询 arp 表以获取 MAC 地址的 IP 地址。显然不是一个理想的解决方案。例子:
nmap -sP 192.168.1.0/24 >/dev/null && arp -an | grep <mac address here> | awk '{print }' | sed 's/[()]//g'
Here nmap will do a ping scan and populate your arp cache. Once the scan is done, the arp command can be used to print the arp table and then you pull out the IP address with grep/awk. You could try replacing nmap with a broadcast ping, but that probably isn't as reliable.
这里 nmap 将执行 ping 扫描并填充您的 arp 缓存。扫描完成后,可以使用 arp 命令打印 arp 表,然后使用 grep/awk 提取 IP 地址。您可以尝试用广播 ping 替换 nmap,但这可能不那么可靠。
回答by David Mulder
I wrote a python module that can do this:
我写了一个可以做到这一点的python模块:
>>> from ethip import ethip
>>> print ethip.getip('00:1E:C9:56:3C:8E', '10.5.42.255')
10.5.42.3
I just makes rapid arp requests to find the ip, then caches what it finds. The code is on github.
我只是发出快速的 arp 请求来查找 ip,然后缓存它找到的内容。代码在github上。
回答by hanoo
I would simply use
我会简单地使用
ip neighbor | grep "00:1E:C9:56:3C:8E" | cut -d" " -f1
回答by Lenar Hoyt
The other methods presented here were unreliable, e.g. the output of ip neighbor
did not always contain the most recent state, so I ended up re-scanning the network using arp-scan
, and hence I simply used the output of the scanning to obtain the IP address for a given MAC address.
此处介绍的其他方法不可靠,例如 的输出ip neighbor
并不总是包含最新状态,因此我最终使用 重新扫描网络arp-scan
,因此我只是使用扫描的输出来获取给定的 IP 地址MAC地址。
For scanning a single network interface, simply use this:
要扫描单个网络接口,只需使用以下命令:
arp-scan -q -l --interface en4 2>/dev/null | grep "00:1E:C9:56:3C:8E" | cut -d$'\t' -f1
The following command scans multiple network interfaces at once:
以下命令一次扫描多个网络接口:
{ arp-scan -q -l --interface en0 2>/dev/null & arp-scan -q -l --interface en4 2>/dev/null } | grep "00:1E:C9:56:3C:8E" | cut -d$'\t' -f1
回答by eaz
Neal'sanswer takes indeed too long. I had to get it work with a 60k+ IPs range. The trick to make this work is to check arp table after each ping. This also fixes the root problem : no need. I did it in Java (see threadedScan() here) because I was on windows and needed a solution which wouldn't spawn thousands of cmd prompts while trying to ping with start command. And it works faster (~10 sec for my 60k range) with a fixedThreadPool.
尼尔的回答确实花了太长时间。我必须让它在 6 万多个 IP 范围内工作。使这项工作的技巧是在每次 ping 后检查 arp 表。这也解决了根本问题:不需要。我是用 Java 完成的(请参阅此处的 threadedScan()),因为我在 Windows 上并且需要一个解决方案,该解决方案在尝试使用 start 命令进行 ping 时不会产生数千个 cmd 提示。使用fixedThreadPool,它的工作速度更快(对于我的60k 范围约10 秒)。
回答by Vegstar
You could try the arp command and grep by mac address
您可以尝试通过 mac 地址使用 arp 命令和 grep
arp -a | grep "00:00:00:00:00:00"
(replace with your own mac addr)
(替换为你自己的mac地址)