windows 使用 ARP 从已知 MAC 地址获取未知 IP 地址?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22821271/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 11:29:15  来源:igfitidea点击:

Getting an unknown IP Address from known MAC address using ARP?

windowsbatch-filepingmac-addressarp

提问by eumoria

I am on a Windows platform (Win7) and I have a scenario where I know the MAC addresses of devices but their IPs are dynamically chosen when booting up.

我在 Windows 平台 (Win7) 上,我有一个场景,我知道设备的 MAC 地址,但在启动时动态选择它们的 IP。

I want to write a batch script that pings these specific devices to make sure they are alive. There are many other devices on the network that I don't want to ping, just a set of 10 specific MACs I want to get the IP address from then ping them only. They are all in the address scheme 10.1.(1-255).(1-255)

我想编写一个批处理脚本来对这些特定设备进行 ping 操作以确保它们处于活动状态。网络上还有许多我不想 ping 的其他设备,只是一组 10 个特定的 MAC,我想从中获取 IP 地址,然后仅 ping 它们。它们都在地址方案 10.1.(1-255).(1-255) 中

This much I know, I can ping the entire address spectrum and then

我知道的就这么多,我可以 ping 整个地址范围,然后

arp -a > arp.txt

...to output a document containing the list of IPs and associated MAC addresses in this format

...以这种格式输出包含 IP 和关联 MAC 地址列表的文档

Interface: 192.168.2.27 --- 0xb
  Internet Address      Physical Address      Type
  192.168.2.1           00-1f-90-c0-25-fd     dynamic   
  192.168.2.3           00-00-aa-a1-d3-78     dynamic   
  192.168.2.16          ac-72-89-a7-7e-98     dynamic   
  192.168.2.17          78-45-c4-2f-71-0b     dynamic   
  192.168.2.18          68-b5-99-8e-1c-35     dynamic   
  192.168.2.24          b8-ac-6f-30-00-34     dynamic   
  192.168.2.26          00-90-a9-6f-e0-be     dynamic   

My question ishow can I (via a batch script or other automated method) find the line of the MAC address I am interested in and put the IP address into a variable that I can then use.

我的问题是我如何(通过批处理脚本或其他自动化方法)找到我感兴趣的 MAC 地址行并将 IP 地址放入我可以使用的变量中。

In UNIX I can grep but in Windows I'm at a loss.

在 UNIX 中我可以 grep 但在 Windows 中我不知所措。

Thanks in advance for any help.

在此先感谢您的帮助。

回答by Drew Chapin

Pipe the output to findand use a forloop.

管道输出find并使用for循环。

for /f "tokens=1,2,3 delims= " %%i in ('arp -a ^| find "00-1f-90-c0-25-fd"') do (
    set ip=%%i
    set mac=%%j
)

You could alter this a bit to make a batch script that will ping by entering the MAC address like so pingbymac.bat 00-1f-90-c0-25-fd

您可以稍微更改一下以制作一个批处理脚本,该脚本将通过输入 MAC 地址来进行 ping 操作 pingbymac.bat 00-1f-90-c0-25-fd

@for /f "tokens=1,2,3 delims= " %%i in ('arp -a ^| find "%~1"') do (
    ping %%i 
)

回答by Stephan

to find the line:

找到该行:

arp -a |find "78-45-c4-2f-71-0b"

To find the IP-Address only:

仅查找 IP 地址:

for /f %i in ('arp -a ^|find "2c-27-d7-ef-f8-77"') do echo %i

(write %%iinstead of %iif using it in a batchfile)

(写%%i而不是%i在批处理文件中使用它)