Jump to content

If ping return > 0 does not mean connected, then what does ?


Recommended Posts

I use

While 1
    $ping = ping ("google.com")
    If $ping > 0 then
        msgbox(0,'','connected YAY !')
        Run ("firefox.exe")
        ExitLoop
    Else
        Continueloop
    EndIf
Wend

Problem is that it sometimes does think its connected yet its not.

My computer has static IP address and everytime i restart computer it takes a while for network to connect to internet.

a while is like few seconds and thats why i wrote my self simple script that would run firefox only if google responds to ping.

Is there any better way to check if internet connection is active and therefore act accordingly ? Caz it seems like ping > 0 is not good enough or am i doing it wrong ?

Thanks in advance guys

Link to comment
Share on other sites

ok buts it relies on the exact same response which is "0"

I dont think that anything other or greater then 0 means connected, because once > 0 is achieved then it starts firefox while no internet connection is made or my network has not connected to modem or what ever

Is there a _WinAPI of some sort to figure this out ? or maybe some sort of DLL call function ?

Here is the whole thing i got.

#NoTrayIcon
#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_icon=icon.ico
#AutoIt3Wrapper_outfile=ConTest.exe
#AutoIt3Wrapper_Compression=4
#AutoIt3Wrapper_UseX64=n
#AutoIt3Wrapper_Res_Comment=N/A
#AutoIt3Wrapper_Res_Description=Tests connection to given address and notifies user for connectivity by WooHoo !
#AutoIt3Wrapper_Res_Fileversion=1.0.0.0
#AutoIt3Wrapper_Res_LegalCopyright=N/A
#AutoIt3Wrapper_Res_requestedExecutionLevel=requireAdministrator
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <Constants.au3>
#Include <File.au3>
#include <array.au3>
FileInstall ('wohoo.wav', @TempDir & '/wohoo.wav')
Opt("TrayOnEventMode",1)
Opt("TrayMenuMode",1)
TrayCreateItem("Show Settings")
TrayItemSetOnEvent(-1,"GUI") ;About is the function name
TrayCreateItem("Exit")
TrayItemSetOnEvent(-1,"ExitFunction") ;About is the function name
TraySetState()

$host = IniRead (@ScriptDir & "\Settings.ini","Settings","Host","")
testloop()

Func testloop()
    While 1
        $Ping = Ping ($host,1)
        If $Ping > "0" Then
            MsgBox(0,'','error '& @error)
            TrayTip ("Status",'You are connected to internet',1)
            SoundPlay (@TempDir & '/wohoo.wav',1)
Dim $szDrive1, $szDir1, $szFName1, $szExt1
            $GetPath1 = _PathSplit(IniRead (@ScriptDir & "\Settings.ini","Components","Path1",""), $szDrive1, $szDir1, $szFName1, $szExt1)
            $ReturnPath1 = _ArrayToString ($GetPath1,"",1,2)
            Run ('"' & IniRead (@ScriptDir & "\Settings.ini","Components","Path1","") & '"',$ReturnPath1)
Dim $szDrive2, $szDir2, $szFName2, $szExt2
            $GetPath2 = _PathSplit(IniRead (@ScriptDir & "\Settings.ini","Components","Path2",""), $szDrive2, $szDir2, $szFName2, $szExt2)
            $ReturnPath2 = _ArrayToString ($GetPath2,"",1,2)
            Run ('"' & IniRead (@ScriptDir & "\Settings.ini","Components","Path2","") & '"',$ReturnPath2)
Dim $szDrive3, $szDir3, $szFName3, $szExt3
            $GetPath3 = _PathSplit(IniRead (@ScriptDir & "\Settings.ini","Components","Path3",""), $szDrive3, $szDir3, $szFName3, $szExt3)
            $ReturnPath3 = _ArrayToString ($GetPath3,"",1,2)
            Run ('"' & IniRead (@ScriptDir & "\Settings.ini","Components","Path3","") & '"',$ReturnPath3)
Dim $szDrive4, $szDir4, $szFName4, $szExt4
            $GetPath4 = _PathSplit(IniRead (@ScriptDir & "\Settings.ini","Components","Path4",""), $szDrive4, $szDir4, $szFName4, $szExt4)
            $ReturnPath4 = _ArrayToString ($GetPath4,"",1,2)
            Run ('"' & IniRead (@ScriptDir & "\Settings.ini","Components","Path4","") & '"',$ReturnPath4)
Dim $szDrive5, $szDir5, $szFName5, $szExt5
            $GetPath5 = _PathSplit(IniRead (@ScriptDir & "\Settings.ini","Components","Path5",""), $szDrive5, $szDir5, $szFName5, $szExt5)
            $ReturnPath5 = _ArrayToString ($GetPath5,"",1,2)
            Run ('"' & IniRead (@ScriptDir & "\Settings.ini","Components","Path5","") & '"',$ReturnPath5)
Dim $szDrive6, $szDir6, $szFName6, $szExt6
            $GetPath6 = _PathSplit(IniRead (@ScriptDir & "\Settings.ini","Components","Path6",""), $szDrive6, $szDir6, $szFName6, $szExt6)
            $ReturnPath6 = _ArrayToString ($GetPath6,"",1,2)
            Run ('"' & IniRead (@ScriptDir & "\Settings.ini","Components","Path6","") & '"',$ReturnPath6)
Dim $szDrive7, $szDir7, $szFName7, $szExt7
            $GetPath7 = _PathSplit(IniRead (@ScriptDir & "\Settings.ini","Components","Path7",""), $szDrive7, $szDir7, $szFName7, $szExt7)
            $ReturnPath7 = _ArrayToString ($GetPath7,"",1,2)
            Run ('"' & IniRead (@ScriptDir & "\Settings.ini","Components","Path7","") & '"',$ReturnPath7)
Dim $szDrive8, $szDir8, $szFName8, $szExt8
            $GetPath8 = _PathSplit(IniRead (@ScriptDir & "\Settings.ini","Components","Pat8h",""), $szDrive8, $szDir8, $szFName8, $szExt8)
            $ReturnPath8 = _ArrayToString ($GetPath8,"",1,2)
            Run ('"' & IniRead (@ScriptDir & "\Settings.ini","Components","Path8","") & '"',$ReturnPath8)
Dim $szDrive9, $szDir9, $szFName9, $szExt9
            $GetPath9 = _PathSplit(IniRead (@ScriptDir & "\Settings.ini","Components","Path9",""), $szDrive9, $szDir9, $szFName9, $szExt9)
            $ReturnPath9 = _ArrayToString ($GetPath9,"",1,2)
            Run ('"' & IniRead (@ScriptDir & "\Settings.ini","Components","Path9","") & '"',$ReturnPath9)
Dim $szDrive10, $szDir10, $szFName10, $szExt10
            $GetPath10 = _PathSplit(IniRead (@ScriptDir & "\Settings.ini","Components","Path10",""), $szDrive10, $szDir10, $szFName10, $szExt10)
            $ReturnPath10 = _ArrayToString ($GetPath10,"",1,2)
            Run ('"' & IniRead (@ScriptDir & "\Settings.ini","Components","Path10","") & '"',$ReturnPath10)
            Exit
        Else
            TrayTip ("Status",'Checking for internet connection.',1)
        EndIf
        Sleep (1000) ;1 second dellay to keep CPU from overloading
    WEnd
EndFunc

Func GUI()
    $Form1 = GUICreate("Change Address",200,400)
    GUICtrlCreateGroup ("Ping who ?",5,5,190,70)
    $Input = GUICtrlCreateInput ("www.google.com",10,20,180,20)
    $Change = GUICtrlCreateButton ("Change",10,45,180)

    GUICtrlCreateGroup ("Options",5,80,190,40)
    GUICtrlCreateLabel ("Run at Startup ?",10,95,90,20)
    $Yes = GUICtrlCreateRadio("Yes",100,90)
    If RegRead ("HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run","ConTest") > "" Then GUICtrlSetState ($Yes,$GUI_CHECKED)
    $No = GUICtrlCreateRadio("No",140,90)
    If RegRead ("HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run","ConTest") = "" Then GUICtrlSetState ($No,$GUI_CHECKED)
#Region StartUp Components
    GUICtrlCreateGroup ("Startup Components",5,120,190,270)
    $FindApp1 = GUICtrlCreateButton ("Browse",130,140,60,20)
    $Path1 = GUICtrlCreateInput (IniRead (@ScriptDir & "\Settings.ini","Components","Path1",""),10,140,115,20)
    $FindApp2 = GUICtrlCreateButton ("Browse",130,160,60,20)
    $Path2 = GUICtrlCreateInput (IniRead (@ScriptDir & "\Settings.ini","Components","Path2",""),10,160,115,20)
    $FindApp3 = GUICtrlCreateButton ("Browse",130,180,60,20)
    $Path3 = GUICtrlCreateInput (IniRead (@ScriptDir & "\Settings.ini","Components","Path3",""),10,180,115,20)
    $FindApp4 = GUICtrlCreateButton ("Browse",130,200,60,20)
    $Path4 = GUICtrlCreateInput (IniRead (@ScriptDir & "\Settings.ini","Components","Path4",""),10,200,115,20)
    $FindApp5 = GUICtrlCreateButton ("Browse",130,220,60,20)
    $Path5 = GUICtrlCreateInput (IniRead (@ScriptDir & "\Settings.ini","Components","Path5",""),10,220,115,20)
    $FindApp6 = GUICtrlCreateButton ("Browse",130,240,60,20)
    $Path6 = GUICtrlCreateInput (IniRead (@ScriptDir & "\Settings.ini","Components","Path6",""),10,240,115,20)
    $FindApp7 = GUICtrlCreateButton ("Browse",130,260,60,20)
    $Path7 = GUICtrlCreateInput (IniRead (@ScriptDir & "\Settings.ini","Components","Path7",""),10,260,115,20)
    $FindApp8 = GUICtrlCreateButton ("Browse",130,280,60,20)
    $Path8 = GUICtrlCreateInput (IniRead (@ScriptDir & "\Settings.ini","Components","Path8",""),10,280,115,20)
    $FindApp9 = GUICtrlCreateButton ("Browse",130,300,60,20)
    $Path9 = GUICtrlCreateInput (IniRead (@ScriptDir & "\Settings.ini","Components","Path9",""),10,300,115,20)
    $FindApp10 = GUICtrlCreateButton ("Browse",130,320,60,20)
    $Path10 = GUICtrlCreateInput (IniRead (@ScriptDir & "\Settings.ini","Components","Path10",""),10,320,115,20)
    $ClearStartup = GUICtrlCreateButton ("Clear All Components",10,340,180,17)
    $SaveStartup = GUICtrlCreateButton ("Save Components",10,360,180)
#EndRegion StartUp Components
    GUISetState(@SW_SHOW,$Form1)
    While 1
        $nMsg = GUIGetMsg()
        Switch $nMsg
        Case $GUI_EVENT_CLOSE
            GUIDelete ($Form1)
            Exit
        Case $Change
            IniWrite (@ScriptDir & "\Settings.ini","Settings","Host",GUICtrlRead ($Input))
            GUIDelete ($Form1)
            testloop()
        Case $Yes
            RegWrite ("HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run","ConTest","REG_SZ",@ScriptFullPath)
        Case $No
            RegDelete ("HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run","ConTest")
        Case $FindApp1
            IniWrite (@ScriptDir & "\Settings.ini","Components","Path1",FileOpenDialog ("Locate executable","","Executable (*.bat;*.com;*.exe;*.cmd)",1+2,"",$Form1))
            GUICtrlSetData ($Path1,IniRead (@ScriptDir & "\Settings.ini","Components","Path1",""))
        Case $FindApp2
            IniWrite (@ScriptDir & "\Settings.ini","Components","Path2",FileOpenDialog ("Locate executable","","Executable (*.bat;*.com;*.exe;*.cmd)",1+2,"",$Form1))
            GUICtrlSetData ($Path2,IniRead (@ScriptDir & "\Settings.ini","Components","Path2",""))
        Case $FindApp3
            IniWrite (@ScriptDir & "\Settings.ini","Components","Path3",FileOpenDialog ("Locate executable","","Executable (*.bat;*.com;*.exe;*.cmd)",1+2,"",$Form1))
            GUICtrlSetData ($Path3,IniRead (@ScriptDir & "\Settings.ini","Components","Path3",""))
        Case $FindApp4
            IniWrite (@ScriptDir & "\Settings.ini","Components","Path4",FileOpenDialog ("Locate executable","","Executable (*.bat;*.com;*.exe;*.cmd)",1+2,"",$Form1))
            GUICtrlSetData ($Path4,IniRead (@ScriptDir & "\Settings.ini","Components","Path4",""))
        Case $FindApp5
            IniWrite (@ScriptDir & "\Settings.ini","Components","Path5",FileOpenDialog ("Locate executable","","Executable (*.bat;*.com;*.exe;*.cmd)",1+2,"",$Form1))
            GUICtrlSetData ($Path5,IniRead (@ScriptDir & "\Settings.ini","Components","Path5",""))
        Case $FindApp6
            IniWrite (@ScriptDir & "\Settings.ini","Components","Path6",FileOpenDialog ("Locate executable","","Executable (*.bat;*.com;*.exe;*.cmd)",1+2,"",$Form1))
            GUICtrlSetData ($Path6,IniRead (@ScriptDir & "\Settings.ini","Components","Path6",""))
        Case $FindApp7
            IniWrite (@ScriptDir & "\Settings.ini","Components","Path7",FileOpenDialog ("Locate executable","","Executable (*.bat;*.com;*.exe;*.cmd)",1+2,"",$Form1))
            GUICtrlSetData ($Path7,IniRead (@ScriptDir & "\Settings.ini","Components","Path7",""))
        Case $FindApp8
            IniWrite (@ScriptDir & "\Settings.ini","Components","Path8",FileOpenDialog ("Locate executable","","Executable (*.bat;*.com;*.exe;*.cmd)",1+2,"",$Form1))
            GUICtrlSetData ($Path8,IniRead (@ScriptDir & "\Settings.ini","Components","Path8",""))
        Case $FindApp9
            IniWrite (@ScriptDir & "\Settings.ini","Components","Path9",FileOpenDialog ("Locate executable","","Executable (*.bat;*.com;*.exe;*.cmd)",1+2,"",$Form1))
            GUICtrlSetData ($Path9,IniRead (@ScriptDir & "\Settings.ini","Components","Path9",""))
        Case $FindApp10
            IniWrite (@ScriptDir & "\Settings.ini","Components","Path10",FileOpenDialog ("Locate executable","","Executable (*.bat;*.com;*.exe;*.cmd)",1+2,"",$Form1))
            GUICtrlSetData ($Path10,IniRead (@ScriptDir & "\Settings.ini","Components","Path10",""))
        Case $ClearStartup
            GUICtrlSetData ($Path1,"")
            GUICtrlSetData ($Path2,"")
            GUICtrlSetData ($Path3,"")
            GUICtrlSetData ($Path4,"")
            GUICtrlSetData ($Path5,"")
            GUICtrlSetData ($Path6,"")
            GUICtrlSetData ($Path7,"")
            GUICtrlSetData ($Path8,"")
            GUICtrlSetData ($Path9,"")
            GUICtrlSetData ($Path10,"")
            MsgBox(64,"Information","Cleared","",$Form1)
        Case $SaveStartup ;this reads all 10 inputs and saves then to INI in case if some were blanked out or erased by user
            IniWrite (@ScriptDir & "\Settings.ini","Components","Path1",GUICtrlRead ($Path1))
            IniWrite (@ScriptDir & "\Settings.ini","Components","Path2",GUICtrlRead ($Path2))
            IniWrite (@ScriptDir & "\Settings.ini","Components","Path3",GUICtrlRead ($Path3))
            IniWrite (@ScriptDir & "\Settings.ini","Components","Path4",GUICtrlRead ($Path4))
            IniWrite (@ScriptDir & "\Settings.ini","Components","Path5",GUICtrlRead ($Path5))
            IniWrite (@ScriptDir & "\Settings.ini","Components","Path6",GUICtrlRead ($Path6))
            IniWrite (@ScriptDir & "\Settings.ini","Components","Path7",GUICtrlRead ($Path7))
            IniWrite (@ScriptDir & "\Settings.ini","Components","Path8",GUICtrlRead ($Path8))
            IniWrite (@ScriptDir & "\Settings.ini","Components","Path9",GUICtrlRead ($Path9))
            IniWrite (@ScriptDir & "\Settings.ini","Components","Path10",GUICtrlRead ($Path10))
            MsgBox(64,"Information","Saved !","",$Form1)
        EndSwitch
    WEnd
EndFunc
Func ExitFunction()
    Exit
EndFunc
Link to comment
Share on other sites

hmm

Maybe because of my static IP connection to the network dellay there is a GAP or some sort where connection is being made for a second and then disconnect after which it connects few seconds later again ?

Maybe because of that GAP ping returns > 0 ?

I guess i can add another ping into this loop to double make sure its there ;)

Thanks you guys for your help, ill go with double or nothing :)

Link to comment
Share on other sites

it just got worse

ping returns "1" regardless of connection status.

I was not connected and it gave me 1 so i went

$Ping = Ping ($host)
        If $Ping > "0" Then
            If $Ping = "1" Then ;then it means no connection
                TrayTip ("Ping return =" & $Ping & ' No connection','Checking for internet connection.',1)
            Else
                ;hurray !
            endif
        endif

I am now connected but it still returns 1

WTF is wrong with this function, it never worked for me way i expected even with earlier version of autoit.

And a minimum number of successful pings sound like a good idea, thats for that but still it stuck at 1

Link to comment
Share on other sites

can u give me example how ?

i dont know how to use @error

The gives as good an answer as anyone can I think and it's quicker. (Well it would have been quicker.)

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

  • Developers

am sorry is that an example of any sort of a sentence that i cant comprehend ?

Thanks

Have you read all posted answers because there is one that shows the use of @error?

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

Since Ping() is returning the number of milliseconds it takes to get a reply to a sent ping message, if you are getting a result >0 then either 1-You are in fact connected to the internet (else you could not be receiving a reply to the ping message), or 2-There is something wrong with your PC or your local network, that you would somehow get a ping reply without being connected (perhaps a firewall/proxy problem).

We then move off the topic of AutoIt. What indication are you getting from Firefox that leads you to believe you are not connected?

To understand how a ping works, you may want to read the following:

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

http://sqlblogcasts.com/blogs/simons/archive/2006/07/13/Ping-and-Tracert-explained-for-dummies.aspx

Link to comment
Share on other sites

Sorry

Problem was: there was no URL to connect to.

i forgot to consider if url = "" then iniwrite google.com so it was looping and giving me return "1" all the time. look at my script above.

gosh how could i miss that ;)

Edited by madasraka
Link to comment
Share on other sites

Have you read all posted answers because there is one that shows the use of @error?

Hah, totally missed that.

OK here's how I check for @error Madaraska. I use a switch statement.

; call your function
_FogRollsIn("On tiny cats feet.")

; and then check for @error:
; look into the helpfile to get the particular @error values that are returned by the function that you call
Switch @error
    Case 0
        ; Success!

    Case 1
        ; Fail

    ; Or I do an 'else' too
    Case Else
        ; Fail
EndSwitch
Edited by jaberwocky6669
Link to comment
Share on other sites

To do it your way it would have to be

If @error = 1 OR @error = 2 OR @error = 3 Then

and that would end up doing the same thing no mattter which of those errors was returned. In most cases you can just get away with

If @Error Then

In the other cases jaberwocky6669s example is the better way to go.

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

Since Ping() is returning the number of milliseconds it takes to get a reply to a sent ping message, if you are getting a result >0 then either 1-You are in fact connected to the internet (else you could not be receiving a reply to the ping message), or 2-There is something wrong with your PC or your local network, that you would somehow get a ping reply without being connected (perhaps a firewall/proxy problem).

We then move off the topic of AutoIt. What indication are you getting from Firefox that leads you to believe you are not connected?

To understand how a ping works, you may want to read the following:

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

http://sqlblogcasts.com/blogs/simons/archive/2006/07/13/Ping-and-Tracert-explained-for-dummies.aspx

Hi,

I am new to AutoIT but I get the same result using Ping - always 1.

Regardless if the network cable is plugged in or out.

Regards

Kallewirsch.

Link to comment
Share on other sites

If you try the Example provided for Ping in the help file, it will error out about 30% of the time. At least that is what I find on my system as of today. It is most likey a problem at my ISPs end which is not uncommon and timeout is something that you do have to allow for.

If I change the timeout delay to 400 ms, it works just fine. It probably also works fine with a number from 300 to 400 but I didn't test it to see what I was getting.

You do have to understand how Ping works as well as understand that ISPs live in a very unstable environment. Here is the modified example code I used, try it and see what you get.

Opt ('TrayIconDebug',1)

$var = Ping("www.AutoItScript.com",400)
;If $var Then; also possible:  If @error = 0 Then
If NOT @error Then
    Msgbox(0,"Status","Online, roundtrip was: " & $var & "ms")
Else
    Msgbox(0,"Error","An error occured with number: " & @error)
EndIf

I'll have some time in a few minutes to work up another example for you to try.

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

Regardless if the network cable is plugged in or out.

If you are getting non-zero returns from Ping() when your cable is unplugged, then there is most likely a problem with your PC.

- First, make sure your script is not using a url that is locally resolvable. If it is, your PC is answering its own ping.

- Check to see if your PC is connecting to a wireless router somewhere (may not necessarily be yours).

- Are you running a personal firewall application? If so, make sure that it is not answering your pings.

- Are you running an internet speed enhancer? These are usually glorified DNS caches, and may be causing your problem. They are seldom worth using.

- If none of these, take your PC in to a professional to have it checked out.

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...