Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.
July 20, 2004
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 <a href="https://www.itprotoday.com/article/jsifaq/jsi-tip-5535-how-can-i-cause-a-called-batch-file-to-return-a-call-directed-environment-variable-.aspx">call directed environment variable</a> 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 offif {%2}
{} @echo Syntax IPName IP Name [/f]&goto :EOFset %2=NFif not {%3}
{} if /i {%3} NEQ {/F} @echo Syntax IPName IP Name [/f]&goto :EOFfor /f "Tokens=2" %%n in ('ping -a -n 1 %1^|findstr /L /C:"[" /C:"]"') do ( set %2=%%n)if /i {%3} EQU {/F} goto :EOFfor /f "Tokens=1 Delims=. " %%n in ('@echo %%%2%%') do ( set %2=%%n)
You May Also Like