Command to get computere name

  • Thread starter Thread starter Gerald
  • Start date Start date
Is there a does command to get a computer name using its
IP address?


I just scripted IPName.bat for you.

The syntax for using IPName is:
[call] IPName IPAddress Name [/f]

where IPAddress is the IP Address.
Name is a the environment variable name you want returned.
/f is optional and causes the fully qualified name to be returned.



IPName.bat contains:

@echo off
if {%2}=={} @echo Syntax IPName IP Name [/f]&goto :EOF
setlocal
set ip=%1
set Type=C
set Name=NF
if /i {%3}=={/F} set Type=D
for /f "Tokens=2" %%n in ('ping -a -n 1 %ip%^|findstr /i /c:"Pinging"') do (
set Name=%%n
)
if "%Type%" NEQ "C" goto finish
for /f "Tokens=1 Delims=. " %%n in ('@echo %Name%') do (
set Name=%%n
)
:finish
endlocal&set %2=%Name%

Jerold Schulman
Windows: General MVP
JSI, Inc.
http://www.jsiinc.com
 
Thanks, I will give it a shot
-----Original Message-----
Is there a does command to get a computer name using its
IP address?


I just scripted IPName.bat for you.

The syntax for using IPName is:
[call] IPName IPAddress Name [/f]

where IPAddress is the IP Address.
Name is a the environment variable name you want returned.
/f is optional and causes the fully qualified name to be returned.



IPName.bat contains:

@echo off
if {%2}=={} @echo Syntax IPName IP Name [/f]&goto :EOF
setlocal
set ip=%1
set Type=C
set Name=NF
if /i {%3}=={/F} set Type=D
for /f "Tokens=2" %%n in ('ping -a -n 1 %ip%
^|findstr /i /c:"Pinging"') do (
 
Jerold said:
Is there a does command to get a computer name using its
IP address?


I just scripted IPName.bat for you.

The syntax for using IPName is:
[call] IPName IPAddress Name [/f]

where IPAddress is the IP Address.
Name is a the environment variable name you want returned.
/f is optional and causes the fully qualified name to be returned.



IPName.bat contains:

@echo off
if {%2}=={} @echo Syntax IPName IP Name [/f]&goto :EOF
setlocal
set ip=%1
set Type=C
set Name=NF
if /i {%3}=={/F} set Type=D
for /f "Tokens=2" %%n in ('ping -a -n 1 %ip%^|findstr /i /c:"Pinging"') do (
set Name=%%n
)
if "%Type%" NEQ "C" goto finish
for /f "Tokens=1 Delims=. " %%n in ('@echo %Name%') do (
set Name=%%n
)
:finish
endlocal&set %2=%Name%
Hello Jerold,
to be more locale independent I suggest a small modification. In my
locale it's only "Ping". But the ip name is followed by the ip number in
square brackets so :

for /f "Tokens=2" %%n in ('ping -a -n 1 %ip%^|findstr "\[%~1\]"') do (

should be more universal. Since fndstr is by default in regexp mode the
brackets have to be escaped, the dots from the ip number will be
interpreted as any char but that won't harm.
 
Thank you.

The following also works:
for /f "Tokens=2" %%n in ('ping -a -n 1 %ip%^|findstr /i /L /C:"[%1]"') do (


Jerold said:
Is there a does command to get a computer name using its
IP address?


I just scripted IPName.bat for you.

The syntax for using IPName is:
[call] IPName IPAddress Name [/f]

where IPAddress is the IP Address.
Name is a the environment variable name you want returned.
/f is optional and causes the fully qualified name to be returned.



IPName.bat contains:

@echo off
if {%2}=={} @echo Syntax IPName IP Name [/f]&goto :EOF
setlocal
set ip=%1
set Type=C
set Name=NF
if /i {%3}=={/F} set Type=D
for /f "Tokens=2" %%n in ('ping -a -n 1 %ip%^|findstr /i /c:"Pinging"') do (
set Name=%%n
)
if "%Type%" NEQ "C" goto finish
for /f "Tokens=1 Delims=. " %%n in ('@echo %Name%') do (
set Name=%%n
)
:finish
endlocal&set %2=%Name%
Hello Jerold,
to be more locale independent I suggest a small modification. In my
locale it's only "Ping". But the ip name is followed by the ip number in
square brackets so :

for /f "Tokens=2" %%n in ('ping -a -n 1 %ip%^|findstr "\[%~1\]"') do (

should be more universal. Since fndstr is by default in regexp mode the
brackets have to be escaped, the dots from the ip number will be
interpreted as any char but that won't harm.


Jerold Schulman
Windows: General MVP
JSI, Inc.
http://www.jsiinc.com
 
Thanks again guys.

-----Original Message-----

Thank you.

The following also works:
for /f "Tokens=2" %%n in ('ping -a -n 1 %ip%
^|findstr /i /L /C:"[%1]"') do (
Jerold said:
On Mon, 19 Jul 2004 09:08:19 -0700, "Gerald"

Is there a does command to get a computer name using its
IP address?


I just scripted IPName.bat for you.

The syntax for using IPName is:
[call] IPName IPAddress Name [/f]

where IPAddress is the IP Address.
Name is a the environment variable name you want returned.
/f is optional and causes the fully qualified name to be returned.



IPName.bat contains:

@echo off
if {%2}=={} @echo Syntax IPName IP Name [/f]&goto :EOF
setlocal
set ip=%1
set Type=C
set Name=NF
if /i {%3}=={/F} set Type=D
for /f "Tokens=2" %%n in ('ping -a -n 1 %ip% ^|findstr /i /c:"Pinging"') do (
set Name=%%n
)
if "%Type%" NEQ "C" goto finish
for /f "Tokens=1 Delims=. " %%n in ('@echo %Name%') do (
set Name=%%n
)
:finish
endlocal&set %2=%Name%
Hello Jerold,
to be more locale independent I suggest a small modification. In my
locale it's only "Ping". But the ip name is followed by the ip number in
square brackets so :

for /f "Tokens=2" %%n in ('ping -a -n 1 %ip% ^|findstr "\[%~1\]"') do (

should be more universal. Since fndstr is by default in regexp mode the
brackets have to be escaped, the dots from the ip number will be
interpreted as any char but that won't harm.


Jerold Schulman
Windows: General MVP
JSI, Inc.
http://www.jsiinc.com
.
 
Jerold said:
Thank you.

The following also works:
for /f "Tokens=2" %%n in ('ping -a -n 1 %ip%^|findstr /i /L /C:"[%1]"') do (
That's right, and may be shortened further %1 contains a number, so the
ignore case /I can be omitted as the /L switch which resembles the /C:,
in this case even the qoutes are obsolete.

for /f "Tokens=2" %%n in ('ping -a -n 1 %ip%^|findstr /C:[%1]') do (
 
Jerold said:
Thank you.

The following also works:
for /f "Tokens=2" %%n in ('ping -a -n 1 %ip%^|findstr /i /L /C:"[%1]"') do (
That's right, and may be shortened further %1 contains a number, so the
ignore case /I can be omitted as the /L switch which resembles the /C:,
in this case even the qoutes are obsolete.

for /f "Tokens=2" %%n in ('ping -a -n 1 %ip%^|findstr /C:[%1]') do (

Yes. My revised script:

@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
)

Jerold Schulman
Windows: General MVP
JSI, Inc.
http://www.jsiinc.com
 
Back
Top