Skip navigation

JSI Tip 8275. How do I retrieve a computer name if I know the IP address on my network?

Using ping -a, which specifies reverse name resolution be performed on the IP address, I have scripted IPName.bat to return the computer name for an IP address on your LAN.

The syntax for using IPName.bat is:

\[call\] IPName IPAddress Name \[/F\]

Where:

IPAddress is the IP address you wish to resolve to a computer name.

Name      is a call directed environment variable that will contain the computer name.

/F        is an optional parameter that will return the fully qualified computer name, like JSI001.JSIINC.COM, instead of JSI001.
NOTE: If the IP address does NOT respond, Name will contain NF.

IPName.bat contains:

@echo off
if \{%2\}

\{\} @echo Syntax IPName IP Name \[/f\]&goto :EOF set %2=NF if not \{%3\}

\{\} if /i \{%3\} NEQ \{/F\} @echo Syntax IPName IP Name \[/f\]&goto :EOF for /f "Tokens=2" %%n in ('ping -a -n 1 %1^|findstr /L /C:"\[" /C:"\]"') do ( set %2=%%n ) if /i \{%3\} EQU \{/F\} goto :EOF for /f "Tokens=1 Delims=. " %%n in ('@echo %%%2%%') do ( set %2=%%n )



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