rss logo

PowerShell script to monitor internet connection failover

PowerShell logo

In an infrastructure with two internet links I was asked to monitor the times when the connection switched from one link to the other.

In a full Windows environment I developed a PowerShell script to do that.

Let's see, in details, how it works…

Get the current public ip address with PowerShell

The goal here is to get the current public ip. To do so we will use external service as http://ifconfig.me.

Mozilla Firefox | http://ifconfig.me/ip address to get current ip

Great, a simple text with ip address : easy!

  • So, to get it from PowerShell we simply need to enter this command :
PS C:\ > (New-Object System.Net.WebClient).DownloadString('http://ifconfig.me/ip') PowerShell | Get public ip address
  • A little more complicated, but not that much, we could also use json format with https://api.ipify.org website :
PS C:\ > ((New-Object System.Net.WebClient).DownloadString('https://api.ipify.org?format=json') | ConvertFrom-Json).ip PowerShell | Get public ip address

PowerShell Script

v1.0

Replace default_ip variable with your current IP.

########################### # author : shebangthedolphins.net # version : 1.0 # date : 2021.06.26 # role : monitor wan link, send email in case of failover # other : Tested on Windows 2019 Server # updates : # - 1.0 (2021/06) : First Version #Enable TLS1.2 : [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 #we set our default wan ip $default_ip = '94.198.4.X' #Function to send emails Function Mail { param ([string]$emailbody, [string]$subject) $encoding = [System.Text.Encoding]::UTF8 Send-MailMessage -Encoding $encoding -To check_wan@shebangthedolphins.net -Subject $subject -From check_wan@shebangthedolphins.net -smtpserver mx.shebangthedolphins.net -Body $emailbody #send email } #We create a checkip_state.txt file whith 1 value. 1 value when wan ip is equal to $default_ip, 0 when wan ip is different from $default_ip if (!(Test-Path C:\Windows\Temp\checkip_state.txt -PathType Leaf)) { #if C:\Windows\Temp\checkip_state.txt has not been created Set-Content C:\Windows\Temp\checkip_state.txt '1' #then we create C:\Windows\Temp\checkip_state.txt with default value } #get public ip address $currentIP = (New-Object System.Net.WebClient).DownloadString('http://ifconfig.me/ip') #$currentIP = ((New-Object System.Net.WebClient).DownloadString('https://api.ipify.org?format=json') | ConvertFrom-Json).ip #If public ip is Null (in case of http://ifconfig.me/ip fails) or is equal to default public ip If(($currentIP -eq $default_ip) -or ($currentIP -eq $Null)) { Write-Host "currently on default link" if ($(get-content C:\Windows\Temp\checkip_state.txt) -eq '0') { #if C:\Windows\Temp\checkip_state.txt value is equal to 0 it means we were previously on alternative link, this is a link change so we send a mail Mail "We are currently using default link with public IP : $currentIP" "Check Failover : link has changed" } Set-Content C:\Windows\Temp\checkip_state.txt '1' #we set checkip_state.txt to 1 (which means default link) } else { Write-Host "currently on alternative link" if ($(get-content C:\Windows\Temp\checkip_state.txt) -eq '1') { #if C:\Windows\Temp\checkip_state.txt value is equal to 1 it means we were previously on default link, this is a link change so we send a mail Mail "We are currently using alternative link with public IP : $currentIP" "Check Failover : link has changed" Set-Content C:\Windows\Temp\checkip_state.txt '0' #we set checkip_state.txt to 0 (which means alternative link) } }

v1.1

########################### # author : shebangthedolphins.net # version : 1.1 # date : 2022.09.13 # role : monitor wan link, send email in case of failover # other : Tested on Windows 2019 Server # updates : # - 1.0 (2021/06) : First Version # - 1.1 (2022/09) : Send an email if the script has run 12 times on the secondary link #Enable TLS1.2 : [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 #we set our default wan ip $default_ip = '94.198.4.X' #Function to send emails Function Mail { param ([string]$emailbody, [string]$subject) $encoding = [System.Text.Encoding]::UTF8 Send-MailMessage -Encoding $encoding -To check_wan@shebangthedolphins.net -Subject $subject -From check_wan@shebangthedolphins.net -smtpserver mx.shebangthedolphins.net -Body $emailbody #send email } #We create a checkip_state.txt file whith 1 value. 1 value when wan ip is equal to $default_ip, 0 when wan ip is different from $default_ip if (!(Test-Path C:\Windows\Temp\checkip_state.txt -PathType Leaf)) { #if C:\Windows\Temp\checkip_state.txt has not been created Set-Content C:\Windows\Temp\checkip_state.txt '1' #then we create C:\Windows\Temp\checkip_state.txt with default value } #We create a checkip_count.txt to count to count the number of times the script has run on the secondary link if (!(Test-Path C:\Windows\Temp\checkip_count.txt -PathType Leaf)) { Set-Content C:\Windows\Temp\checkip_count.txt '0' } #get public ip address $currentIP = (New-Object System.Net.WebClient).DownloadString('http://ifconfig.me/ip') #If public ip is Null (in case of http://ifconfig.me/ip fails) or is equal to default public ip If(($currentIP -eq $default_ip) -or ($currentIP -eq $Null)) { Write-Host "currently on default link" if ($(get-content C:\Windows\Temp\checkip_state.txt) -eq '0') { #if C:\Windows\Temp\checkip_state.txt value is equal to 0 it means we were previously on alternative link, this is a link change so we send a mail Mail "We are currently using default link with public IP : $currentIP" "Check Failover : link has changed" } Set-Content C:\Windows\Temp\checkip_state.txt '1' #we set checkip_state.txt to 1 (which means default link) Set-Content C:\Windows\Temp\checkip_count.txt '0' #reinit checkip count when we go back to default link } else { Write-Host "currently on alternative link" Set-Content C:\Windows\Temp\checkip_count.txt $([int](get-content C:\Windows\Temp\checkip_count.txt)+1) #increments by 1 the value contained in checkip_count.txt file if ($(get-content C:\Windows\Temp\checkip_state.txt) -eq '1') { #if C:\Windows\Temp\checkip_state.txt value is equal to 1 it means we were previously on default link, this is a link change so we send a mail Mail "We are currently using alternative link with public IP : $currentIP" "Check Failover : link has changed" } if ($(get-content C:\Windows\Temp\checkip_count.txt) -ge '13') { #if the script has been executed 12 times without going back to the main link Set-Content C:\Windows\Temp\checkip_count.txt '0' #reinit checkip count Mail "We are currently using alternative link with public IP : $currentIP" "Check Failover : still on backup link" #send email } Set-Content C:\Windows\Temp\checkip_state.txt '0' #we set checkip_state.txt to 0 (which means alternative link) }

Now follow this link to create a Windows task in order to run the script periodically.

Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

Contact :

contact mail address