Jump to content
Sign in to follow this  
BinaryBrother

_CheckInternet()

Recommended Posts

BinaryBrother

Terrible internet in this area forced me to create a logger.

; #FUNCTION# ====================================================================================================================
; Name ..........: _CheckConnection
; Description ...: Will tell if your internet down, or just DNS
; Syntax ........: _CheckConnection()
; Parameters ....: 
; Return values .: 0, 1, 2 INT
; 0 = Everything fine, internet/DNS up.
; 1 = DNS is down, but IP ping worked.
; 2 = Internet is down entirely.
; Author ........: BinaryBrother
; ===============================================================================================================================
Func _CheckConnection()
    Local $PingDNS1, $PingDNS2, $PingDNS3, $PingDNS4, $Ret
    TCPStartup()
    $PingDNS1 = Ping("google.com")
    $PingDNS2 = Ping("computerhope.com")
    $PingDNS3 = Ping("yahoo.com")
    $PingDNS4 = Ping("download.com")
    If $PingDNS1 <= 1 And $PingDNS2 <= 1 And $PingDNS3 <= 1 And $PingDNS4 <= 1 Then
        $Ret = 1
        ;DNS is possibly down, check internet next.
        $PingDNS1 = Ping("74.125.93.106")
        $PingDNS2 = Ping("69.72.169.241")
        $PingDNS3 = Ping("67.195.160.76")
        $PingDNS4 = Ping("64.30.224.118")
        If $PingDNS1 <= 1 And $PingDNS2 <= 1 And $PingDNS3 <= 1 And $PingDNS4 <= 1 Then
            $Ret = 2 ;Internet Down
        EndIf
        Return $Ret
    Else
        Return 0 ;DNS and Internet are up
    EndIf
EndFunc   ;==>_CheckConnection

Example of use... :mellow:

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Compression=4
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
#include <Date.au3>
While 1
    _Main()
    Sleep(5000)
WEnd

Func _Main()
    $ConnectionStatus = _CheckConnection()
    If $ConnectionStatus >= 1 Then
        _Beep()
        _Log("[INTERNET DOWN] " & @MON & "/" & @MDAY & "/" & @YEAR)
        _Log("Error Code: " & $ConnectionStatus)
        $DownAt = _NowTime()
        $Timer = TimerInit()
        _Log("Down: " & $DownAt)
        While _CheckConnection() >= 1 ;While internet down
            Sleep(5000)
            _Beep()
        WEnd
        $UpAt = _NowTime()
        $DownTime = int(TimerDiff($Timer)/1000/60)
        If $DownTime <= 0 Then
            $DownTime = "Less than a minute"
        Else
            $DownTime &= " minute[s]"
        EndIf
        _Log("Up: " & $UpAt)
        _Log("Downtime: " & $DownTime)
    EndIf
EndFunc

Func _Beep()
    Beep(4000, 200)
    Beep(4000, 200)
    Beep(4000, 200)
EndFunc


Func _CheckConnection()
    Local $PingDNS1, $PingDNS2, $PingDNS3, $PingDNS4, $Ret
    TCPStartup()
    $PingDNS1 = Ping("google.com")
    If $PingDNS1 >= 1 Then Return 0 ;We know the internet is fine.
    $PingDNS2 = Ping("computerhope.com")
    $PingDNS3 = Ping("yahoo.com")
    $PingDNS4 = Ping("download.com")
    If $PingDNS1 <= 1 And $PingDNS2 <= 1 And $PingDNS3 <= 1 And $PingDNS4 <= 1 Then
        $Ret = 1
        ;DNS is possibly down, check internet next.
        $PingDNS1 = Ping("74.125.93.106")
        $PingDNS2 = Ping("69.72.169.241")
        $PingDNS3 = Ping("67.195.160.76")
        $PingDNS4 = Ping("64.30.224.118")
        If $PingDNS1 <= 1 And $PingDNS2 <= 1 And $PingDNS3 <= 1 And $PingDNS4 <= 1 Then
            $Ret = 2
        EndIf
        TCPShutdown()
        Return $Ret
    Else
        TCPShutdown()
        Return 0 ;DNS and Internet are up
    EndIf
EndFunc   ;==>_CheckConnection

Func _Log($Data)
    FileWriteLine(@DesktopDir & "\InternetLog.txt", $Data)
EndFunc

I might suggest this to the guys down the road with a server warehouse... Those beeps would sure help find the server with the internet out... lol

Edited by BinaryBrother

Share this post


Link to post
Share on other sites
guinness

I use this, instead of pinging websites for Vista+ >>

ConsoleWrite("Internet Is Connected" & " = " & _IsInternetConnected() & @CRLF)

Func _IsInternetConnected()
    Local $aReturn = DllCall('connect.dll', 'long', 'IsInternetConnected')
    If @error Then
        Return SetError(1, 0, False)
    EndIf
    Return $aReturn[0] = 0
EndFunc   ;==>_IsInternetConnected
Edited by guinness
  • Like 1

UDF List:

 
_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_ArrayFilter/_ArrayReduce_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 22/04/2018

Share this post


Link to post
Share on other sites
BinaryBrother

Nice find, although [as you've already stated] it doesn't work for XP and might not tell weather or not your DNS is down... I'm not sure how the method works as I couldn't find more info on it, but if that's the same API that the system tray icon utilizes for connectivity, it will not fail if your DNS is down due to caching... :mellow:

Share this post


Link to post
Share on other sites
BrewManNH

I've never run into the problem of a DNS going down or not reachable, but you can always set your own DNS settings to any number of public ones, such as the Google ones (8.8.8.8 and 8.8.4.4) or any of the ones on this page so that you don't have to worry about a single point of failure. I don't even use the DNS settings from my ISP at home.

I administer the domain controller at the college I work for, and I have a DHCP server setup with the our DNS and also use Googles and the Verizon one at 4,2,2,2 as backups and unless my DHCP server goes down when a computer needs an IP address the computers never lose connection.


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

Share this post


Link to post
Share on other sites
BinaryBrother

I always run OpenDNS on my personal machine, I quite like the level of control and statistical visualization, not to mention the ability to refresh specific domain's DNS cache manually! I normally push OpenDNS on our main router as well. However, the boss has some sort of "dedicated" service through the ISP and believes that not utilizing the DNS provided would be wasting this "dedicated" service. You know how the higher ups can be... ;P

I've made this script so we can report outages to his ISP, weekly or bi-weekly [as I understand it]. We have a hard time with the ISP's DNS as well as general internet connectivity... So I wanted to report [specifically] when the DNS simply goes down or when the entire socket goes down, so we can narrow the probable cause of the outages. Not to mention when I tell the reps that the DNS is down, they're like "DN.. What? Go reboot your router..."... Posted Image

Edited by BinaryBrother

Share this post


Link to post
Share on other sites
storme

Terrible internet in this area forced me to create a logger.

G'day BinaryBrother

Oone thing you might like to add is a check to your "default gateway".

I've had lot of customers report "no internet access" but when I get there the router has been switched off, the cables unplugged, the LAN connection is disabled, etc

On the rough testes I've made up I check the defautl gateway first, if it doesn't work nothing else will.

Thanks for sharing the code!

John Morrison

Share this post


Link to post
Share on other sites
BinaryBrother

Good idea, should be simplistic enough to add. I had forgot about testing it [DG] entirely, even though it does just "lock-up" 2 or 3 times a week... >.<

Share this post


Link to post
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
Sign in to follow this  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.