Setting the ping address with Variable?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
CJDrew
Posts: 4
Joined: 15 May 2012 19:58

Setting the ping address with Variable?

#1 Post by CJDrew » 15 May 2012 20:10

Ok, so i'm making a little program that lets you put in any adress you want and it automatically pings it.

Here's my code so far

Code: Select all

@echo off
:START
cls
echo What website would you like to ping?:
set /p address="Domain Name=> "
set /p yesno="you would like to ping %address%?=> "
if %yesno%==yes goto YES
goto START

:YES
(magical ping code stuff goes here)



Does anyone know how i can set the ping address to my variable?

Fawers
Posts: 187
Joined: 08 Apr 2012 17:11
Contact:

Re: Setting the ping address with Variable?

#2 Post by Fawers » 15 May 2012 21:14

Try this:

Code: Select all

for /f "tokens=1,2 delims=[]" %%h in ('ping -n 1 %address%') do (
  if not [%%i] == [] set "IP=%%i"
)

CJDrew
Posts: 4
Joined: 15 May 2012 19:58

Re: Setting the ping address with Variable?

#3 Post by CJDrew » 15 May 2012 21:56

It didn't work... are you saying i should just put that whole thing under YES or am i missing something? (I'm sorry, the code you posted just makes no sense to my newbie mind...)

Fawers
Posts: 187
Joined: 08 Apr 2012 17:11
Contact:

Re: Setting the ping address with Variable?

#4 Post by Fawers » 15 May 2012 22:02

CJDrew wrote:It didn't work... are you saying i should just put that whole thing under YES or am i missing something? (I'm sorry, the code you posted just makes no sense to my newbie mind...)

Well, %address% should be a website address and not the ip itself (since you actually want the ip). On my command line, when I ping a website, the ip is echoed within [brackets] - that's why those are the delimiters in my code.

Oh, yes: the code I posted should be in the :YES section.

But only for the record, could you run ping -n 1 localhost and paste its output here?

CJDrew
Posts: 4
Joined: 15 May 2012 19:58

Re: Setting the ping address with Variable?

#5 Post by CJDrew » 16 May 2012 16:25

Still not working... would you mind posting the working code?
Also, here is the screencapture...

Image

Fawers
Posts: 187
Joined: 08 Apr 2012 17:11
Contact:

Re: Setting the ping address with Variable?

#6 Post by Fawers » 16 May 2012 17:32

CJDrew wrote:Still not working... would you mind posting the working code?
Also, here is the screencapture...

Image

There's no IP address on your image.
My code works when there's and actual IP address in the output.

Here's what I get:
C:\Documents and Settings\Fawers\Desktop>ping -n 1 localhost

Disparando contra FWerneck [127.0.0.1] com 32 bytes de dados:

Resposta de 127.0.0.1: bytes=32 tempo<1ms TTL=64

Estatísticas do Ping para 127.0.0.1:
Pacotes: Enviados = 1, Recebidos = 1, Perdidos = 0 (0% de perda),
Aproximar um número redondo de vezes em milissegundos:
Mínimo = 0ms, Máximo = 0ms, Média = 0ms

(text in blue = command
text in red = ip output)
The code I wrote needs this kind of output to work.

CJDrew
Posts: 4
Joined: 15 May 2012 19:58

Re: Setting the ping address with Variable?

#7 Post by CJDrew » 16 May 2012 17:40

Any idea as to how i could fix that?
It seems to work with other sites...
Image

Fawers
Posts: 187
Joined: 08 Apr 2012 17:11
Contact:

Re: Setting the ping address with Variable?

#8 Post by Fawers » 16 May 2012 17:42

You made me curious. What exactly is "FrostedFlakes"?

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Setting the ping address with Variable?

#9 Post by foxidrive » 16 May 2012 19:07

It looks like an IP V6 address to me.

http://en.wikipedia.org/wiki/IPv6

taripo
Posts: 227
Joined: 01 Aug 2011 13:48

Re: Setting the ping address with Variable?

#10 Post by taripo » 16 May 2012 21:04

Just add the line:

Code: Select all

ping %address%


address is the variable you used to store the domain you want to ping.

Here's the code, i've added the line

Code: Select all

@echo off
:START
cls
echo What website would you like to ping?:
set /p address="Domain Name=> "
set /p yesno="you would like to ping %address%?=> "
if %yesno%==yes goto YES
goto START

:YES
ping %address%

Code: Select all

What website would you like to ping?:
Domain Name=> www.google.com
you would like to ping www.google.com?=> yes

Pinging www.l.google.com [173.194.78.104] with 32 bytes of data:

Reply from 173.194.78.104: bytes=32 time=16ms TTL=50
Reply from 173.194.78.104: bytes=32 time=15ms TTL=50
Reply from 173.194.78.104: bytes=32 time=16ms TTL=50
Reply from 173.194.78.104: bytes=32 time=15ms TTL=50

Ping statistics for 173.194.78.104:
    Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
    Minimum = 15ms, Maximum = 16ms, Average = 15ms

C:\>

taripo
Posts: 227
Joined: 01 Aug 2011 13:48

Re: Setting the ping address with Variable?

#11 Post by taripo » 16 May 2012 21:16

cjdrew see post above.

and for Fawers,

Your code, if right, and it looks like it wasn't quite right - from my test anyway, but correcting it so it works, yes it would certainly pick up whatever is within the [] In this case he had an IPv6 address there, the IPv6 address for localhost. (I only found out recently that ::1 was the IPv6 address for localhost)

Fawers wrote:You made me curious. What exactly is "FrostedFlakes"?


That's the name of his computer.

If you do ping localhost, you will see the name of your computer

As to your code for grabbing the IP, did you try it?

It didn't quite work for me and I doubt it could've worked for you, as it was.

It has to be tokens=1-3 not tokens=1-2.

As an example -simplified- because it's to demonstrate. To grab the IP.

(I think ::1 is the equivalent of 127.0.0.1 in IPv6)

Code: Select all

C:\>for /f "tokens=1-3 delims=[]" %h in ("Pinging magnus [127.0.0.1] with 32 bytes of data:") do echo %i

C:\>echo 127.0.0.1
127.0.0.1


Code: Select all

C:\>for /f "tokens=1-3 delims=[]" %h in ("Pinging magnus [::1] with 32 bytes of data:") do echo %i

C:\>echo ::1
::1

Fawers
Posts: 187
Joined: 08 Apr 2012 17:11
Contact:

Re: Setting the ping address with Variable?

#12 Post by Fawers » 16 May 2012 21:25

taripo wrote:As to your code for grabbing the IP, did you try it?

It didn't quite work for me and I doubt it could've worked for you, as it was.

Yes, I did. It works pretty well.

taripo wrote:It has to be tokens=1-3 not tokens=1-2.

No, it doen't. We need the text between [brackets] - the 2nd token. We don't need anything after the brackets - the 3rd token.

taripo wrote:

Code: Select all

C:\>for /f "tokens=1-3 delims=[]" %h in ("Pinging magnus [127.0.0.1] with 32 bytes of data:") do echo %i

C:\>echo 127.0.0.1
127.0.0.1


Code: Select all

C:\>for /f "tokens=1-3 delims=[]" %h in ("Pinging magnus [::1] with 32 bytes of data:") do echo %i

C:\>echo ::1
::1

As you can see, you used only the 2nd token yourself. In nowhere is a 3rd token necessary.
By the way, your FOR loop has a string in its body -"Pinging magnus [::1] with 32 bytes of data:", and not the ping command. This string will ALWAYS output the same thing; the ping command with a custom address will not.

taripo
Posts: 227
Joined: 01 Aug 2011 13:48

Re: Setting the ping address with Variable?

#13 Post by taripo » 16 May 2012 21:32

ah you're right on both counts, that I don't need the third token of course, and doh as you point out, I wasn't using it anyway (as to the string, that was intentional, and as I said, as a demonstration, and it demonstrates that what you said about the second and third token is correct)
Last edited by taripo on 16 May 2012 22:21, edited 2 times in total.

taripo
Posts: 227
Joined: 01 Aug 2011 13:48

Re: Setting the ping address with Variable?

#14 Post by taripo » 16 May 2012 21:39

Your code would pick up his address.

It's not a "custom address", it's an IPv6 address. And your code does pick it up, as it is between the square brackets.

Post Reply