Jump to content

Script to detect active internet connection (Working Now)


Draygoes
 Share

Recommended Posts

Please Note This Has Been Solved!

I am leaving this here for anyone with the same problem. Solution in this post.

 

Sorry for the vague title, but it was the best that I could come up with considering.

Suddenlink has been switching our connection on and off for the last few days as they make upgrades. In some of these cases, its been a few hours. So, I decided to modify one of the example scripts in an attempt to get the script to display a msgbox upon finding an internet connection. I leave the volume on full, this way I know exactly when the connection comes back. The problem with this is that when I glanced at the computer after 30 more mins, I found that we DID have a connection, yet the script had not displayed the Msg. I do not feel that I need to tell you guys how out of touch with Autoit I am, so this is a problem that I cannot even fathom the issue of. In my mind, the script looks perfect. Anyway, I will defer to the wonderful genus that is this community to see what the issue is.

I will upload the code as is. If you could present the fixed code, along with an explanation of what I did wrong I would be very grateful.

Fix or not, I just thank you for taking the time to read this.

 

#include <MsgBoxConstants.au3>
Example()
Func Example()
    Local $dData = InetRead("http://www.autoitscript.com/autoit3/files/beta/update.dat")

    Local $iBytesRead = @extended

    Local $sData = BinaryToString($dData)
If $sData = "" Then
    Sleep( 1000 )
    Example()
    EndIf
    MsgBox(0, "YAY!!!", "Internet Connection Back!")
EndFunc

As you can see, I was hoping that if it returned an empty string then the code would simply restart the function. But, should it show anything other than a blank string I figured that it would pass the EndIf and display the Msg. Somehow I got this wrong.

 

I noticed one possible issue in the above code, and that was in the variable. I accidentally read the wrong variable it would seem. However, fixing that still did not result in a working script. :(


EDITED CODE

#include <MsgBoxConstants.au3>
Example()
Func Example()
    Local $dData = InetRead("http://www.autoitscript.com/autoit3/files/beta/update.dat")

    Local $iBytesRead = @extended

    Local $sData = BinaryToString($dData)
If $dData = "" Then
    Sleep( 1000 )
    Example()
    EndIf
    MsgBox(0, "YAY!!!", "Internet Connection Back!")
EndFunc

 

Edited by Draygoes
Spoiler

 

"If a vegetarian eats vegetables,What the heck does a humanitarian eat?"

"I hear voices in my head, but I ignore them and continue on killing."

"You have forced me to raise the indifference warning to beige, it's a beige alert people. As with all beige alerts please prepare to think about the possibility of caring."

An optimist says that giving someone power DOESN'T immediately turn them into a sadist. A pessimist says that giving someone power doesn't IMMEDIATELY turn them into a sadist.

 

 
Link to comment
Share on other sites

Try this version, there's no recursion involved and it doesn't hammer the autoitscript.com site every second.

#include <MsgBoxConstants.au3>
#include <INet.au3>
Global $dData
While 1
    $dData = _GetIP()
    If $dData <> -1 Then ;  internet connection is working
        MsgBox($MB_SYSTEMMODAL + $MB_ICONINFORMATION, "YAY!!!", "Internet Connection Back!")
    EndIf
    Sleep(301000) ; sleep for 5 minutes (and one second)
WEnd

 

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

Try this version, there's no recursion involved and it doesn't hammer the autoitscript.com site every second.

#include <MsgBoxConstants.au3>
#include <INet.au3>
Global $dData
While 1
    $dData = _GetIP()
    If $dData <> -1 Then ;  internet connection is working
        MsgBox($MB_SYSTEMMODAL + $MB_ICONINFORMATION, "YAY!!!", "Internet Connection Back!")
    EndIf
    Sleep(301000) ; sleep for 5 minutes (and one second)
WEnd

 

Unfortunatlly, this version doesnt work either. At least not to the degree that I want it to. Also, the idea was not to hammer the site, it was to check for a file online, and keep looking until found then display a message and exit. I modified the script above to have it exit after the Msg display, then disabled the internet connection manually,

Despite there being no connection, it still displays the message.

Oh, and I also altered it to sleep for 3 seconds rather than 5 mins. I want to know pretty fast when the connection comes back.

 

Here is a rough draft of what I am trying to do.

Is there an active internet connection?

yes - display msg and exit

no- wait 3 seconds and check again

To be clear, I should mention that Suddenlink has stated that they could be cutting off the connection at any point in time for any amount of time for a while. I do not need to know when the connection goes out, I will notice that. I just want to execute the script when it does go out so that I wont have to constantly check for an active connection.

This should be easy, and a much younger version of myself would be laughing at me right now. I am sorry if this looks as noobish as it is, but again, I have not used Autoit in a while.

 

Anyway, modified yet still not working code based on your design.

#include <MsgBoxConstants.au3>
#include <INet.au3>
Global $dData
While 1
    $dData = _GetIP()
    If $dData <> -1 Then ;  internet connection is working
        MsgBox($MB_SYSTEMMODAL + $MB_ICONINFORMATION, "YAY!!!", "Internet Connection Back!")
    Exit
    EndIf
    Sleep(3000) 
    WEnd


Again, thank you for your time.

 

EDIT
I also considered using Ping(), but that did not work very well either. Still, if the option is on the table I would love to know.

Edited by Draygoes
Spoiler

 

"If a vegetarian eats vegetables,What the heck does a humanitarian eat?"

"I hear voices in my head, but I ignore them and continue on killing."

"You have forced me to raise the indifference warning to beige, it's a beige alert people. As with all beige alerts please prepare to think about the possibility of caring."

An optimist says that giving someone power DOESN'T immediately turn them into a sadist. A pessimist says that giving someone power doesn't IMMEDIATELY turn them into a sadist.

 

 
Link to comment
Share on other sites

Read the help file for _GetIP, it has a built in spam protection in that it won't let you check for your IP in less than 5 minutes, which is why I wrote the script the way I did.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

Read the help file for _GetIP, it has a built in spam protection in that it won't let you check for your IP in less than 5 minutes, which is why I wrote the script the way I did.

Ah ok. I should have checked on that.

Well, is there any way to get results a bit faster with a different function. Again, the idea is not to bomb a site, its to check every 3 seconds until site is found then display msg and exit. That should be possible right? I am finding it more and more difficult to pull this off, but I know that it must be possible...

Spoiler

 

"If a vegetarian eats vegetables,What the heck does a humanitarian eat?"

"I hear voices in my head, but I ignore them and continue on killing."

"You have forced me to raise the indifference warning to beige, it's a beige alert people. As with all beige alerts please prepare to think about the possibility of caring."

An optimist says that giving someone power DOESN'T immediately turn them into a sadist. A pessimist says that giving someone power doesn't IMMEDIATELY turn them into a sadist.

 

 
Link to comment
Share on other sites

After searching google, I finally found a solution that works. Slightly modified, it does exactly what I wanted. YAY! :)

Thank you for all of your time and effort BeerMan! Perhaps your solutions wil work in a slightly different situation.

 

Just in case anyone does get help from any of this, I am leaving this in place, and tagging it.

SOLUTION

While 1
$connect = _GetNetworkConnect()

If $connect Then
    MsgBox(64, "YES!!!", $connect)
Exit
Else
    Sleep( 3000 )
EndIf
WEnd


Func _GetNetworkConnect()
    Local Const $NETWORK_ALIVE_LAN = 0x1  ;net card connection
    Local Const $NETWORK_ALIVE_WAN = 0x2  ;RAS (internet) connection
    Local Const $NETWORK_ALIVE_AOL = 0x4  ;AOL

    Local $aRet, $iResult

    $aRet = DllCall("sensapi.dll", "int", "IsNetworkAlive", "int*", 0)

    If BitAND($aRet[1], $NETWORK_ALIVE_LAN) Then $iResult &= "LAN connected" & @LF
    If BitAND($aRet[1], $NETWORK_ALIVE_WAN) Then $iResult &= "WAN connected" & @LF
    If BitAND($aRet[1], $NETWORK_ALIVE_AOL) Then $iResult &= "AOL connected" & @LF

    Return $iResult
EndFunc

:EDIT:
I forgot to credit the creator. Most of this code was written by rasim, and the original solution can be found here.

 

:EDIT2:
If anyone feels up to posting a more simple solution to get the same task done, its still more than welcome. This feels like a bit much for what should be a very simple task.

Edited by Draygoes
Spoiler

 

"If a vegetarian eats vegetables,What the heck does a humanitarian eat?"

"I hear voices in my head, but I ignore them and continue on killing."

"You have forced me to raise the indifference warning to beige, it's a beige alert people. As with all beige alerts please prepare to think about the possibility of caring."

An optimist says that giving someone power DOESN'T immediately turn them into a sadist. A pessimist says that giving someone power doesn't IMMEDIATELY turn them into a sadist.

 

 
Link to comment
Share on other sites

Your last script will only tell you if your computer is connected to a network, it won't tell you if your network is connected to the internet.

An alternative to _GetIP might be the _WinAPI_IsInternetConnected function.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

Your last script will only tell you if your computer is connected to a network, it won't tell you if your network is connected to the internet.

An alternative to _GetIP might be the _WinAPI_IsInternetConnected function.

 

As always you are correct. Network went down for about 2.5 hours and that script couldnt tell the difference. I will look into the above function. Thanks for the advice friend. :)

Spoiler

 

"If a vegetarian eats vegetables,What the heck does a humanitarian eat?"

"I hear voices in my head, but I ignore them and continue on killing."

"You have forced me to raise the indifference warning to beige, it's a beige alert people. As with all beige alerts please prepare to think about the possibility of caring."

An optimist says that giving someone power DOESN'T immediately turn them into a sadist. A pessimist says that giving someone power doesn't IMMEDIATELY turn them into a sadist.

 

 
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

×
×
  • Create New...