JSI Tip 7391. How do I retrieve the IP Address and associated MAC address of a client computer?


In tip 7390, we retrieved the IP Address of the current computer, by parsing a ping of the %ComputerName% environment variable.

If you also need the physical address (MAC address) of the associated interface, use IPMAC.BAT instead.

The syntax for using IPMAC.BAT is:

call ipmac ip mac

where:

ip  is the call directed environment variable that will contain the IP Address.

mac is the call directed environment variable that will contain the MAC address of the interface with the ip IP Address.
NOTES:
  If ip is n.o.p.2, you forgot to provide the mac parameter.
  If ip is n.o.n.e, the ping failed.
  if mac is No-MAC, the physical address was not found.
IPMAC.bat contains:
@echo off
if \{%2\}

\{\} goto noP2 if \{%1\}

\{\} goto noP1 setlocal for /f "Tokens=2 Delims=\[\]" %%i in ('ping -n 1 "%computername%"') do set IP=%%i if not defined IP set IP=n.o.n.e&set MAC=No-IP&goto :end for /f "Skip=10 Tokens=1* Delims=:" %%i in ('ipconfig /all') do set p1=#%%i&set p2=#%%j&call :parse if not defined MAC set MAC=No-MAC :end endlocal&set %1=%IP%&set %2=%MAC% goto :EOF :noP2 if \{%1\}==\{\} goto noP1 set %1=n.o.p.2 :noP1 @echo Syntax call IPMAC IP MAC goto :EOF :parse if "%p1%" EQU "#" goto :EOF set wrk=%p1:Physical Address=% if "%p1%" NEQ "%wrk%" set PA=%p2:~2%&goto :EOF set wrk=%p1:IP Address=% if "%p1%" EQU "%wrk%" goto :EOF set WIP=%p2:~2% if /i "%IP%" NEQ "%WIP%" goto :EOF set MAC=%PA%



Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish