Jump to content

reporting a ping from dos


Guest ghetek
 Share

Recommended Posts

Here's an example of using the return of a DOS command. This method only works with RunWait (and not Run) but it will return the exit code of the program that was run. In the case of ping, a 0 means the host was up and 1 means it was down.

$result = RunWait(@ComSpec & " /c ping www.google.com", "", @SW_HIDE)
MsgBox(0, "Trial 1", "Result of google ping: " & $result)
$result = RunWait(@ComSpec & " /c ping www.goble-de-gook.com", "", @SW_HIDE)
MsgBox(0, "Trial 2", "Result of garbage ping: " & $result)

Edited to make more sense

Edited by pekster

[font="Optima"]"Standing in the rain, twisted and insane, we are holding onto nothing.Feeling every breath, holding no regrets, we're still looking out for something."[/font]Note: my projects are off-line until I can spend more time to make them compatable with syntax changes.

Link to comment
Share on other sites

Try the following code. It displays the errorlevel from ping command:

$sCmd = @ComSpec & " /c ping.exe"

; displays "0"
$sCmdParm = "www.yahoo.com"
$iRetCode = RunWait( $sCmd & " " & $sCmdParm)
MsgBox( 0, "Ping Return Code", $iRetCode )

; displays "1"
$sCmdParm = "test.yahoo.com"
$iRetCode = RunWait( $sCmd & " " & $sCmdParm)
MsgBox( 0, "Ping Return Code", $iRetCode )
Edited by midiaxe
Link to comment
Share on other sites

  • 2 weeks later...

aaaaaaaaaaaaaah :huh2:

:postal:

I need help, this is driving me crazy :D

When i ping a "unknown host" your script works fine (it send back result 1), but if i ping a server that is not responding, it still give me a 0 as its working.

Would it be possible to:

Ping a server:

Result = 1 (unknow host) write to ini "fail"

Look for the word "Request timed out" and write to ini "fail"

Result = 0 (Reply) write to ini "Succed"

argh, why is it giving me a 0 if there is no reply :)

Were ever i lay my script is my home...

Link to comment
Share on other sites

...

When i ping a "unknown host" your script works fine (it send back result 1), but if i ping a server that is not responding, it still give me a 0 as its working.

...

argh, why is it giving me a 0 if there is no reply  :D

Because the server was reached and the ping did it's job successful.

When the server is not answering to the ping, it's not the fault of ping.exe

I don't have a server here wich is configured not to answer to a ping, so that I could test it. But I think it must be some kind of that reason.

arctor

Link to comment
Share on other sites

I found this in the other thread:

var = FileRead ("mypath\my.txtr", FileGetSize ("mypath\my.txt ))

if stringinstr ($var,"mystring") then

msgbox(4096,"Hurray","Only one line of code and I don't have to read line by line")

endif

And think i will use that, if i can get the ping result into a txt file.

And one know how i can pipe the result?

ping.exe www.autoitscript.com > test.txt

Will that work?

yea, it works :D

Sorry for posting before testing, but i'm getting used to get errors all the time :huh2:

Edited by Doxie

Were ever i lay my script is my home...

Link to comment
Share on other sites

I found this in the other thread:

var = FileRead ("mypath\my.txtr", FileGetSize ("mypath\my.txt ))

if stringinstr ($var,"mystring") then

msgbox(4096,"Hurray","Only one line of code and I don't have to read line by line")

endif

And think i will use that, if i can get the ping result into a txt file.

And one know how i can pipe the result?

ping.exe www.autoitscript.com > test.txt

Will that work?

yea, it works :D

Sorry for posting before testing, but i'm getting used to get errors all the time :huh2:

hehe I just posted that

you may need http: but ya that's what I've seen people do

don't forget to clear the var when you're done

$var=""

Rick

Only $2.00 with Resale Rights How to Block Better for Martial Artists and NonMartial Artistshttp://kirkhamsebooks.com/MartialArts/Bloc...tterEbook_m.htm

Link to comment
Share on other sites

Hurray!!!

Its all working :D

ping.exe -n 1 127.0.0.1 > test.txt ;This is making a reply

ping.exe -n 1 192.168.0.1 > test.txt ;This is not making a reply

$var = FileRead ("test.txt", FileGetSize ("test.txt" ))

if stringinstr ($var,"Reply") then

msgbox(4096,"Hurray","Reply")

Else

msgbox(4096,"Doh","No Reply")

endif

I need this line because i'm making a ADSL Status tool for my users.

And this "function" is need to test if they are logged in to their ISP.

If they can ping 10.0.0.6, they got contact with their ISP.

If they can ping www.microsoft.com, they are logged in.

If 10.0.0.6 succed, but microsoft fails, it will open their login page. :huh2:

It also check some other settings, and now it all works perfectly, this was the last problem i had.

Were ever i lay my script is my home...

Link to comment
Share on other sites

don't forget to clear the var when you're done

$var=""

Rick

Everytime i run the ping it rewrite the txt file, so i get a fresh txt file each time.

What exactlly could happend if i dont clear the $var? :D

Were ever i lay my script is my home...

Link to comment
Share on other sites

...

ping.exe www.autoitscript.com > test.txt

...

When you are already on the cmdline, why do not do the rest of it also there.

You don't need to redirect to a textfile, then load it with autoit and then find the right string in autoit.

Just use a one liner. Pipe the output to find. The /i makes it caseunsensitiv.

$res = RunWait(@ComSpec & ' /c ping www.yahoo.com | find /i "string_you_want_to_find"')
MsgBox(0, "Output", "PingResult: " & $res)

When string is found you will get errorlevel 0, if not, errorlevel 1.

You can also use a ping.exe -n 1 for just ping one time instead of three.

One Note: To get back the errorlevel from a commandline-prog as a result in AutoIt don't seems to work with Win98.

arctor

Edit: Forgot a space. -n 1

@Tutor2000: You don't need HTTP for a ping

Edited by Arctor
Link to comment
Share on other sites

Sweet dude...

One less file to think about :huh2:

Even tough this is taking a bit longer (if no reply) then it does with > .txt

Wonder why is that? :D

But i prefer your last suggestion, very nice and handy.

Thanks, ill mention your name in the "About" hehe

Were ever i lay my script is my home...

Link to comment
Share on other sites

Sweet dude...

One less file to think about :huh2:

Even tough this is taking a bit longer (if no reply) then it does with > .txt

Wonder why is that?  :D

But i prefer your last suggestion, very nice and handy.

Thanks, ill mention your name in the "About"  hehe

for me it is better to find the string "TTL" , because ,for example, in Windows Xp Italian "Reply" is translated "risposta".

bye

Link to comment
Share on other sites

When you are already on the cmdline, why do not do the rest of it also there.

You don't need to redirect to a textfile, then load it with autoit and then find the right string in autoit.

Just use a one liner. Pipe the output to find. The /i makes it caseunsensitiv.

$res = RunWait(@ComSpec & ' /c ping www.yahoo.com | find /i "string_you_want_to_find"')
MsgBox(0, "Output", "PingResult: " & $res)

When string is found you will get errorlevel 0, if not, errorlevel 1.

You can also use a ping.exe -n 1 for just ping one time instead of three.

One Note: To get back the errorlevel from a commandline-prog as a result in AutoIt don't seems to work with Win98.

arctor

Edit: Forgot a space. -n 1

@Tutor2000: You don't need HTTP for a ping

I don't like find

I've found it worked differently with different OS's. Could be my inexptitude (and spelling ld)

Thanks for the http info

Rick

Only $2.00 with Resale Rights How to Block Better for Martial Artists and NonMartial Artistshttp://kirkhamsebooks.com/MartialArts/Bloc...tterEbook_m.htm

Link to comment
Share on other sites

$res = RunWait(@ComSpec & ' /c ping www.yahoo.com | find /i "string_you_want_to_find"')

Why does not @SW_HIDE work with above code?

I had the same problem with the PowerArchiver commandline tool.

(Using your command as an example) I used this:

$res = RunWait(@ComSpec & ' /k ping www.yahoo.com | find /i "string_you_want_to_find && EXIT"', '', @SW_HIDE)
Edited by SlimShady
Link to comment
Share on other sites

  • Developers

Works for me, could it be you forgot the parameter for the startup Path before the @SW_HIDE ?

Try this one:

$res = RunWait(@ComSpec & ' /c ping www.yahoo.com | find /i "string_you_want_to_find"','',@SW_HIDE)
msgbox(0,'debug',$res)

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...