czwartek, 12 stycznia 2017

RARP batch script

This is my batch script with functionality of reversed arp protocol. It is useful for finding IP address of computers with dynamic IP address like Raspberry Pi.
@ECHO OFF
REM "find" command error codes: 
REM  0 – The string you were searching for was found.
REM  1 – The string you were searching for was not found.
REM  2 – This means you had a bad switch or your parameters were incorrect.
 
REM To test: "arp -d [IP]" cancels entry from arp table. Root privilages are required.

setlocal enabledelayedexpansion

set IPclass=%1
set MACaddress=%2

set "result="

IF "%MACaddress%"=="" (
 ECHO Ver. 3.0
 ECHO Usage: rarp [IP class] [MAC address]
 ECHO My popular MAC addresses:
 ECHO Raspberry B = "b8-27-eb-xx-xx-xx"
 GOTO break
) ELSE (
 FOR /l %%i IN (1,1,254) DO (
  CALL :searchMAC
  
  IF ERRORLEVEL 1 GOTO break
  
  ping -n 1 -w 1 %IPclass%.%%i | find "Reply" > NUL
 )
)

REM There will be a for cycle if 'arp -a ^| findstr %MACaddress%' gives a reult of at least one line!!
:searchMAC
SET ERR=0
FOR /f "tokens=*" %%j IN ('arp -a ^| findstr %MACaddress%') DO (
 ECHO Found: %%j
 
 SET ERR=1
)
IF %ERR% == 0 ECHO|set /p="."
EXIT /b %ERR%

:break