Oct 22 2017

Check if there is a working internet connection when a firewall blocks ping requests

image_pdfimage_print

Sometimes you need to check if there is a working internet connection. You can use the
pingcommand for that. But this command does not work if ICMP echo request have disabled on the firewall. If this is the case we have to check the status of a website. If the status is 200 then there is a working internet connection.

In this example I will use both vbscript and powershell to check if there is a working internet connection.

The basics

  1. The command Invoke-WebRequest -Uri www.google.com -TimeoutSec 1 -UseBasicParsing gives a lot of internet about the webpage. Pay attention to the statuscode.

    Invoke-WebRequest -Uri www.google.com -TimeoutSec 1 -UseBasicParsing

  2. The command (Invoke-WebRequest -Uri www.google.com -TimeoutSec 1 -UseBasicParsing).StatusCode returns only the statuscode.

    (Invoke-WebRequest -Uri www.google.com -TimeoutSec 1 -UseBasicParsing).StatusCode

  3. There is an error message if there is no working internet connection.

    Invoke-WebRequest -Uri www.google.com -TimeoutSec 1 -UseBasicParsing
    With no working internet connection.

  4. If there is a working internet connection then the script returns 200.

    Try {(Invoke-WebRequest -Uri www.google.com -TimeoutSec 1 -UseBasicParsing).StatusCode} Catch {Write-Host “Error!!!”}
    Working internet connection.

  5. In this case there is no working internet connection, so an error message is shown.

    Try {(Invoke-WebRequest -Uri www.google.com -TimeoutSec 1 -UseBasicParsing).StatusCode} Catch {Write-Host “Error!!!”}
    No working internet connection.

Further testing with PowerShell

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<#
.NOTES
===========================================================================
Created with:     Windows PowerShell ISE
Created on:       21-October-2017
Created by:       Willem-Jan Vroom
Organization:     
Filename:         CheckIfWebsiteIsReachable.ps1
===========================================================================
.DESCRIPTION:
 
It checks if there is a working internet connection by reading the webpage.
This is very usefull in case the ping command does not work when ICMP echo
requests are disabled on the firewall.
 
#>
param ($URL)
Function Get-StatusCodeFromWebsite
{
  param($Website)
  Try
   {
    (Invoke-WebRequest -Uri $Website -TimeoutSec 1 -UseBasicParsing).StatusCode
   }
  Catch
   {
   }
}
 
$URL = "www.google.com"
$Result = Get-StatusCodeFromWebsite -Website $URL
if($Result -eq 200)
 {
  Write-Host "The website $URL is reachable."
 }
 else
 {
  Write-Host "The website $URL is not reachable."
 }

Result if there is a working internet connection.

Result if there is no working internet connection.

The next step: use vbscript and powershell to detect if there is a working internet connection

As far as I know vbscript has not the option to detect the status from a website. So we use the powershell function with vbscript to detect if there is a working internet connection.

The powershell script has changed a little bit:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<#
.NOTES
===========================================================================
Created with:     Windows PowerShell ISE
Created on:       21-October-2017
Created by:       Willem-Jan Vroom
Organization:     
Filename:         CheckIfWebsiteIsReachable_with_vbscript.ps1
===========================================================================
.DESCRIPTION:
 
It checks if there is a working internet connection by reading the webpage.
This is very usefull in case the ping command does not work when ICMP echo
requests are disabled on the firewall.
 
#>
Param ($URL)
Function Get-StatusCodeFromWebsite
{
  param($Website)
  Try
   {
    (Invoke-WebRequest -Uri $Website -TimeoutSec 1 -UseBasicParsing).StatusCode
   }
  Catch
   {
   }
}
 
$Result = Get-StatusCodeFromWebsite -Website $URL
if($Result -eq 200)
 {
  Exit 0
 }
 else
 {
  Exit 1
 }

It exits with an exit code:
0 = success
1 = failure

And we use vbscript to check:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
' ================================================================================================
' Check if a there is a working network connection. Do not use ping, as the ping command cannot
' bu used in case ICMP echo requests have been disabled on the firewall.
' Created by Willem-Jan Vroom
' Version history:
'
' 0.0.1
'  - Initial version
'
' 1.0.0
'  - Final version
' ================================================================================================
 
' ------------------------------------------------------------------------------------------------
' Declare the most variables. 
' ------------------------------------------------------------------------------------------------
 
  Option Explicit
 
  Dim objShell      : set objShell   = WScript.CreateObject("WScript.Shell")
  Dim strWebsite    : strWebsite     = "http://www.vroom.cc"
  Dim strCurrentDir : strCurrentDir  = Left(Wscript.ScriptFullname, InstrRev(Wscript.ScriptFullname, "\"))
  Dim strCommand    : strCommand     = "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy Bypass -WindowStyle Hidden -File " & chr(34) & strCurrentDir & "CheckIfWebsiteIsReachable_with_vbscript.ps1" & chr(34) & " -URL "& strWebsite
  Dim strText       : strText        = ""
  Dim valResult     : valResult      = 0
 
' ------------------------------------------------------------------------------------------------
' Check the website that is mentioned in the string strWebsite. 
' Use the exitcode from the powershell script:
'  0 = Success
'  1 = Failure
' ------------------------------------------------------------------------------------------------
 
  valResult = objShell.Run(strCommand, 6, True)
  if valResult = 0 Then 
     strText = "The website '" & strWebSite & "' could be reached"
       else
     strText = "The website '" & strWebSite & "' could not be reached"
  end if
 
' ------------------------------------------------------------------------------------------------
' Display a message box with the result. 
' ------------------------------------------------------------------------------------------------
 
  msgbox strText,0,"The result."

In this example we try the webpage http://www.vroom.cc/.

Result if there is a working internet connection.

Result if there is no working internet connection.

You can download the scripts in a ZIP file.

Permanent link to this article: https://www.vroom.cc/wordpress/check-if-there-is-a-working-internet-connection-when-a-firewall-blocks-ping-requests/