Last active
January 17, 2024 15:51
-
-
Save megadix/97477a42bb8c150f1e9ba7b4f4955600 to your computer and use it in GitHub Desktop.
VPN Heartbeat
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <# | |
| .SYNOPSIS | |
| Keep a VPN connection alive by periodically issuing a GET | |
| .DESCRIPTION | |
| Keep a VPN connection alive by periodically issuing a GET on a specified URL and log status code. | |
| .PARAMETER url | |
| URL to GET. | |
| .PARAMETER sleep | |
| Interval (in seconds) between GET invocations. | |
| .EXAMPLE | |
| Run script on default url | |
| vpn-heartbeat.ps1 | |
| .EXAMPLE | |
| Run script on custom url and sleep interval | |
| vpn-heartbeat.ps1 -url https://www.megadix.it/ -sleep 120 | |
| #> | |
| param( | |
| # url | |
| [String]$url = "https://www.megadix.it/", | |
| # sleep interval | |
| [Int32]$sleep = 60 | |
| ) | |
| Write-Output "Heartbeat script" | |
| Write-Output "Url : $url" | |
| Write-Output "Sleep : $sleep s`n" | |
| while ($true) { | |
| Write-Output "Heartbeat: $url" | |
| try { | |
| $result = Invoke-WebRequest -Uri $url | |
| if ($result.StatusCode -eq 200) { | |
| Write-Output "...received: HTTP $($result.StatusCode)" | |
| } | |
| else { | |
| # Log status code | |
| Write-Error "...received: HTTP $($result.StatusCode)" | |
| } | |
| } | |
| catch { | |
| # Log error and continue | |
| Write-Error "...an error occurred: $($_.Exception.Message)" | |
| Write-Error "retrying in ${sleep} seconds..." | |
| } | |
| Start-Sleep $sleep | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment