3

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.

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)

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

arp -a > arp.txt

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

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 is how 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.

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

Thanks in advance for any help.

eumoria
  • 31
  • 1
  • 1
  • 2

2 Answers2

2

Pipe the output to find and use a for loop.

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

@for /f "tokens=1,2,3 delims= " %%i in ('arp -a ^| find "%~1"') do (
    ping %%i 
)
Drew Chapin
  • 7,779
  • 5
  • 58
  • 84
  • 1
    You can use `%~1` which will remove any surrounding quotes, if they exist, otherwise it will act the same as `%1`. Off topic, but this allows a construct like `"%~1"` to be used in any comparison and will handle quoted or unquoted terms and always return a quoted term. – foxidrive Apr 03 '14 at 10:29
1

to find the line:

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

To find the IP-Address only:

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

(write %%i instead of %i if using it in a batchfile)

Stephan
  • 53,940
  • 10
  • 58
  • 91