Page 1 of 1
Setting the ping address with Variable?
Posted: 15 May 2012 20:10
by CJDrew
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?
Re: Setting the ping address with Variable?
Posted: 15 May 2012 21:14
by Fawers
Try this:
Code: Select all
for /f "tokens=1,2 delims=[]" %%h in ('ping -n 1 %address%') do (
if not [%%i] == [] set "IP=%%i"
)
Re: Setting the ping address with Variable?
Posted: 15 May 2012 21:56
by CJDrew
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...)
Re: Setting the ping address with Variable?
Posted: 15 May 2012 22:02
by Fawers
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?
Re: Setting the ping address with Variable?
Posted: 16 May 2012 16:25
by CJDrew
Still not working... would you mind posting the working code?
Also, here is the screencapture...

Re: Setting the ping address with Variable?
Posted: 16 May 2012 17:32
by Fawers
CJDrew wrote:Still not working... would you mind posting the working code?
Also, here is the screencapture...

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.
Re: Setting the ping address with Variable?
Posted: 16 May 2012 17:40
by CJDrew
Any idea as to how i could fix that?
It seems to work with other sites...

Re: Setting the ping address with Variable?
Posted: 16 May 2012 17:42
by Fawers
You made me curious. What exactly is "FrostedFlakes"?
Re: Setting the ping address with Variable?
Posted: 16 May 2012 19:07
by foxidrive
It looks like an IP V6 address to me.
http://en.wikipedia.org/wiki/IPv6
Re: Setting the ping address with Variable?
Posted: 16 May 2012 21:04
by taripo
Just add the line:
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:\>
Re: Setting the ping address with Variable?
Posted: 16 May 2012 21:16
by taripo
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
Re: Setting the ping address with Variable?
Posted: 16 May 2012 21:25
by Fawers
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.
Re: Setting the ping address with Variable?
Posted: 16 May 2012 21:32
by taripo
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)
Re: Setting the ping address with Variable?
Posted: 16 May 2012 21:39
by taripo
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.