Get IP from MAC adress?!

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
tvogel
Posts: 4
Joined: 24 Apr 2020 02:12

Get IP from MAC adress?!

#1 Post by tvogel » 24 Apr 2020 02:26

Hi, I´d like to get the IP of a given MAC adresss to ping a specific computer and check if it´s is online.
Yes, I just know the computers MAC adress - not it´s IP because it´s dynamic!

I startet with something like that and it works fine:

FOR /F %%i IN ('arp -a ^| find "12-34-56-78-90-ff"') DO set ip=%%i

But the problem is that I´d like to keep the MAC adress variable in the code (it´s given from outside to my skript).

So I tried something like that:

set mac=%1
FOR /F %%i IN ('arp -a ^| find "%mac%"') DO set ip=%%i

But the content of %mac% seams not to be processed and so the function will just return echo is off?!

Anybody out there who has a solution - even something totaly different which keeps track of my need of getting the IP of a given MAC adress???
ThanX a lot in advance - Thilo *;o(

aGerman
Expert
Posts: 3890
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Get IP from MAC adress?!

#2 Post by aGerman » 24 Apr 2020 15:17

What do you get if you invoke an

Code: Select all

echo %1
in your code?

Steffen

Eureka!
Posts: 97
Joined: 25 Jul 2019 18:25

Re: Get IP from MAC adress?!

#3 Post by Eureka! » 25 Apr 2020 13:48

This is what I use to connect an off-line NAS to a non-reserved DHCP address.
The intersting part for your case is the following line:

Code: Select all

   for /f "usebackq" %%x in (`arp -a ^| findstr /i "%MAC%"`) do set IP=%%x
.. but in case you want to use it for something similar as I did (waking up NAS, getting IP-address and mapping a network share), I posted the entire script (without the personal details).

Note: there is a echo <bell character> after the net use M: \\... line; I don't know if it survived the forum editor.
If not: enter ALT-0-0-7 after the echo statement.

Code: Select all

@echo off
rem echo on
setlocal

::_______________________________________________________
::
::	SETTINGS
::_______________________________________________________
::
  set MAC=00-11-22-33-44-55
  set MAXTRIES=50
  set INTERVAL=5
  set USER=xxxx
  set WW=xxxx
  set PROGRAM=C:\Tools\wakemeonlan-x64\WakeMeOnLan.exe


::_______________________________________________________
::
::	INIT
::_______________________________________________________
::
  set IP=

:: Disconnect old
   net use M: /d 2>nul
  

::_______________________________________________________
::
::	ACTION
::_______________________________________________________
::

:: Wake up NAS
   echo Waking up NAS

:: Wait for NAS to be alive
   For /L %%x in (1,1,%MAXTRIES%) DO call :Loop %%x

:: NAS running; IP known. Connect ...
   echo %TIME% - NAS running; IP known. Connect ...
   echo.

:: NAS is running. Wait 5 seconds for shares to become available.
   timeout 5
   
   net use M: \\%IP%\Media %WW% /user:%IP%\%USER%
   echo 
   echo NAS is on %IP%

goto :EOF



::===================================
:LOOP
::===================================
   IF defined IP goto :EOF
   echo %TIME% - attempt %1
   "%PROGRAM%" /wakeup %MAC%
   start /w "" "%PROGRAM%" /scan
   for /f "usebackq" %%x in (`arp -a ^| findstr /i "%MAC%"`) do set IP=%%x
   timeout %INTERVAL% > nul
goto :EOF




tvogel
Posts: 4
Joined: 24 Apr 2020 02:12

Re: Get IP from MAC adress?!

#4 Post by tvogel » 27 Apr 2020 10:57

TahnX a lot. Where could i get the file you use? -> "C:\Tools\wakemeonlan-x64\WakeMeOnLan.exe"

tvogel
Posts: 4
Joined: 24 Apr 2020 02:12

Re: Get IP from MAC adress?!

#5 Post by tvogel » 27 Apr 2020 11:06

aGerman wrote:
24 Apr 2020 15:17
What do you get if you invoke an

Code: Select all

echo %1
in your code?

Steffen
hi Steffen.
if i insert your code i´ll get the right output. %1 is echoing out the right ip.
but how to get this value in my variable %ip%?
thankX

tvogel
Posts: 4
Joined: 24 Apr 2020 02:12

Re: Get IP from MAC adress?!

#6 Post by tvogel » 27 Apr 2020 11:15

aGerman wrote:
24 Apr 2020 15:17
What do you get if you invoke an

Code: Select all

echo %1
in your code?

Steffen
sorry i was not right!
i will get back the mac adress - not the ip. so i´ve got still the same problem ...

Eureka!
Posts: 97
Joined: 25 Jul 2019 18:25

Re: Get IP from MAC adress?!

#7 Post by Eureka! » 29 Apr 2020 18:48

tvogel wrote:
27 Apr 2020 10:57
TahnX a lot. Where could i get the file you use? -> "C:\Tools\wakemeonlan-x64\WakeMeOnLan.exe"
nirsoft.net (first hit when you google "wakemeonlan.exe")

BTW: You didn't try my solution?

Post Reply