Getting an ip address's hostname into a variable, using PING

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
©opy[it]®ight
Posts: 60
Joined: 17 Mar 2012 09:59

Getting an ip address's hostname into a variable, using PING

#1 Post by ©opy[it]®ight » 15 Aug 2012 06:37

I did a search before posting this message so here goes :)

I know that there are more ways to do this but since i'm far from an advanced scripter
i'm asking you guys (are there any females around? :mrgreen: ) which code is most efficient/clean
to achieve the goal i want (i don't want to work with temporary files so i guess a FOR loop is necessary).

I use the following command:

ping -a -n 1 -w 0 %IP%


The default output has way too much info i don't want but i don't know how to skip/ignore or strip them from the result:

C:\Users\admin>ping -a -n 1 -w 0 74.125.79.94

Pingen naar ey-in-f94.1e100.net [74.125.79.94] met 32 bytes aan gegevens:
PING: verzenden mislukt. Algemene fout.

Ping-statistieken voor 74.125.79.94:
Pakketten: verzonden = 1, ontvangen = 0, verloren = 1
(100% verlies).

©opy[it]®ight
Posts: 60
Joined: 17 Mar 2012 09:59

Re: Getting an ip address's hostname into a variable, using

#2 Post by ©opy[it]®ight » 15 Aug 2012 07:00

Hey Ed Dyreen, alles goed hoor :D

Thanks for the script, except that i want to get an external ip address's hostname, not a local one ;)

edit: hah, you deleted your post already :lol:

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: Getting an ip address's hostname into a variable, using

#3 Post by Squashman » 15 Aug 2012 07:05

Very similar to the other code I posted to get the IP address from the hostname in your other thread.
Tokens will be 3 for you. Since I use english for my version of Windows the tokens is 2.

Code: Select all

for /f "tokens=3 delims= " %%G in ('ping -a -n 1 -w 0 74.125.79.94 ^| find "["') do set hostname=%%G

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: Getting an ip address's hostname into a variable, using

#4 Post by Ed Dyreen » 15 Aug 2012 07:13

'
Hey ©opy[it]®ight, alles goe :wink:

Code: Select all

@echo off

set "$hostnameOrIP=74.125.79.94"
set    "$.hostname="
set          "$.IP="

for /f "tokens=3,4delims=[] " %%a in (

       'ping "%$hostnameOrIP%" -n 1'

) do   if not defined $.hostname (

       set "$.hostname=%%~a"
       set       "$.ip=%%~b"
)

set "$"

pause
exit
I tried using an ip, but this doesn't returns the DNS for me.
Is that DNS in your hostfile or is that default behavior on your OS win7 ?

©opy[it]®ight
Posts: 60
Joined: 17 Mar 2012 09:59

Re: Getting an ip address's hostname into a variable, using

#5 Post by ©opy[it]®ight » 15 Aug 2012 07:19

Squashman wrote:Very similar to the other code I posted to get the IP address from the hostname in your other thread.
Tokens will be 3 for you. Since I use english for my version of Windows the tokens is 2.

Code: Select all

for /f "tokens=3 delims= " %%G in ('ping -a -n 1 -w 0 74.125.79.94 ^| find "["') do set hostname=%%G


Thanks! :) Is there a way to make the script universal i.e. that it can be used regardless of the Windows version someone uses?
(i have yet to learn what tokens are and what they do ;-) )

found it: http://www.robvanderwoude.com/ntfortokens.php

Ed Dyreen wrote:Hey ©opy[it]®ight, alles goe :wink:

I tried using an ip, but this doesn't returns the DNS for me.
Is that DNS in your hostfile or is that default behavior on your OS win7 ?


No i just looked for an ip (used one from google) and typed the ping command.
My HOSTS file only has the default content/entries ;)
Last edited by ©opy[it]®ight on 15 Aug 2012 07:32, edited 4 times in total.

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: Getting an ip address's hostname into a variable, using

#6 Post by Squashman » 15 Aug 2012 07:20

Ed Dyreen wrote:I tried using an ip, but this doesn't returns the DNS for me.

That's what the -a switch is for when using an IP.

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: Getting an ip address's hostname into a variable, using

#7 Post by Squashman » 15 Aug 2012 07:31

©opy[it]®ight wrote:Thanks! :) Is there a way to make the script universal i.e. that it can be used regardless of the Windows version someone uses?
(i have yet to learn what tokens are and what they do ;-) )

I am not good with the language stuff. I leave that up to Ed and Liviu.

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: Getting an ip address's hostname into a variable, using

#8 Post by Squashman » 15 Aug 2012 07:34

I do like ED's solution better though as you can give the hostname or IP and it will give you back both. Just need to add the -a to the ping command on his code.

©opy[it]®ight
Posts: 60
Joined: 17 Mar 2012 09:59

Re: Getting an ip address's hostname into a variable, using

#9 Post by ©opy[it]®ight » 15 Aug 2012 07:34

Hmm, true.. but if its not too much of a hassle to modify the script so that it can be used by more people around the world, i'm all ears :)

Ed? Ik ben benieuwd of je het voor elkaar weet te krijgen. In ieder geval bedankt voor de al gegeven oplossing! :)
(sorry if its not allowed to speak in another language than english ;) hint: google translate :mrgreen: )
Last edited by ©opy[it]®ight on 15 Aug 2012 10:44, edited 2 times in total.

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: Getting an ip address's hostname into a variable, using

#10 Post by Ed Dyreen » 15 Aug 2012 08:00

'
This can be used regardless the windows language or version.

Code: Select all

@echo off

call :hostName.resolve "ey-in-f94.1e100.net"
call :hostName.resolve "74.125.79.94"
call :hostName.resolve "0.0.0.1"
call :hostName.resolve ""

pause
exit

:hostName.resolve ( %1 )
:: (
       set "$.hostname="
             set "$.IP="

       for /f "tokens=1,2 delims=[]" %%a in (

              'ping /a "%~1" -n 1'

       ) do   if +%%~b neq + (

              for %%? in ( %%~a ) do set "$.hostname=%%~?"
              set "$.IP=%%~b"
       )
       echo. &set "$"
:: )
exit /b 0

Code: Select all


$.hostname=ey-in-f94.1e100.net
$.IP=74.125.78.94

$.hostname=ey-in-f94.1e100.net
$.IP=74.125.79.94

Omgevingsvariabele $ is niet gedefinieerd

$.hostname=ED-SERV-0
$.IP=192.168.1.10
Druk op een toets om door te gaan. . .

©opy[it]®ight
Posts: 60
Joined: 17 Mar 2012 09:59

Re: Getting an ip address's hostname into a variable, using

#11 Post by ©opy[it]®ight » 15 Aug 2012 10:41

I believe that piece of code does it all :shock:
I barely understand what it does but it looks amazing and... it works! :D

(heb je er lang over gedaan om dit te leren?)

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: Getting an ip address's hostname into a variable, using

#12 Post by Squashman » 15 Aug 2012 11:08

I have come to appreciate Ed's style of code writing. He really likes to indent and space things out for readability.

©opy[it]®ight
Posts: 60
Joined: 17 Mar 2012 09:59

Re: Getting an ip address's hostname into a variable, using

#13 Post by ©opy[it]®ight » 15 Aug 2012 14:39

Sure do(es)! :)

I've been playing with the code and it works a charm.
As i menioned before i'm not the only person that will use the script.

I host a webserver (sort of) that does one thing only, showing the connected client their external ip address/hostname.

Now, this works great for others, but when running this on my own computer/webserver, the script takes around 4 seconds to complete
(this only occurs when extracting the hostname from 192.168.0.1, as you see below):

Code: Select all

SET IP=192.168.0.1

for /f "tokens=1,2 delims=[]" %%a in ('ping -a -n 1 -w 0 %IP%') do if +%%~b neq + (for %%? in ( %%~a ) do set "HOSTNAME=%%~?")

The output i get:
ECHO is off (uit).


Whereas using 0.0.0.0 or 192.168.0.191 as IP address, it finishes almost instantly.

My question: Why does this happen/occur with one local IP, and not with others?

- ©opy[it]®ight

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: Getting an ip address's hostname into a variable, using

#14 Post by Ed Dyreen » 15 Aug 2012 18:31

'
Not sure, but I think ECHO is off (uit). :)

Code: Select all

@echo off
echo /?
set "HOSTNAME="
echo %HOSTNAME%
pause
Let me rephrase that, you are trying to echo a variable that isn't defined.
Apart from '@echo off', look for any 'echo ' statements in your batch and change them to 'echo.' or even better echo(.
Or just open your batch with notepad, press CTRL+H enter 'echo ' as the search string and 'echo.' as replacement string and select 'Alles vervangen'.

©opy[it]®ight
Posts: 60
Joined: 17 Mar 2012 09:59

Re: Getting an ip address's hostname into a variable, using

#15 Post by ©opy[it]®ight » 16 Aug 2012 02:55

Ed Dyreen,

I couldn't find any empty echo statements. The ping command is simply not able to resolve a hostname for my router's IP 192.168.0.1, this is why the result(ing value) is empty/null.

Here are the results i got when pinging other local ip addresses:
C:\Users\admin>ping -a -n 1 -w 1 192.168.0.191

Pingen naar i7-2600K.lan_dn [192.168.0.191] met 32 bytes aan gegevens:

C:\Users\admin>ping -a -n 1 -w 1 192.168.0.121

Pingen naar Prutske [192.168.0.121] met 32 bytes aan gegevens:
Last edited by ©opy[it]®ight on 16 Aug 2012 04:41, edited 1 time in total.

Post Reply