Last active
March 24, 2025 22:25
-
-
Save BobbyWibowo/e01cda9b01edbcc0ea6dfd1ed6f12a83 to your computer and use it in GitHub Desktop.
For pesky 3G/4G home router that often drops its mobile data connectivity due to inactivity
This file contains 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
#Requires -Version 7.0 | |
Param( | |
[Float] $Delay = 1, | |
[Float] $FailureDelay = 1, | |
[Int32] $BufferSize = 1 | |
) | |
[System.Threading.Thread]::CurrentThread.CurrentCulture = 'en-GB' | |
$Delay = [Math]::Round($Delay, 1) | |
$FailureDelay = [Math]::Round($FailureDelay, 1) | |
If ($Delay -lt 0.1) { | |
"Delay cannot be less than 0.1 seconds." | |
Exit 1 | |
} | |
if ($FailureDelay -lt 0.1) { | |
"Failure delay cannot be less than 0.1 seconds." | |
Exit 1 | |
} | |
If ($BufferSize -lt 1) { | |
"Buffer size cannot be less than 1 byte." | |
Exit 1 | |
} | |
# Targets do not have to be DNS IPs, but they are chosen due to simplicity | |
$Targets = @( | |
# Cloudflare DNS | |
"1.1.1.1", | |
"1.0.0.1", | |
# Cloudflare DNS (No Malware) | |
"1.1.1.2", | |
"1.0.0.2", | |
# Cloudflare DNS (No Malware/Adult) | |
"1.1.1.3", | |
"1.0.0.3", | |
# Google DNS | |
"8.8.8.8", | |
"8.8.4.4", | |
# OpenDNS | |
"208.67.222.222", | |
"208.67.220.220", | |
# OpenDNS Family | |
"208.67.222.123", | |
"208.67.220.123", | |
# OpenDNS Sandbox | |
"208.67.220.2", | |
"208.67.222.2" | |
) | |
"Test-Connection (Targets: {0:D}, Delay: {1:N1}s, Failure Delay: {2:N1}s Buffer Size: {3:D}B)" -F $Targets.Count, $Delay, $FailureDelay, $BufferSize | |
If ($Targets.Count -lt 1) { | |
Exit 1 | |
} | |
If ($Targets.Count -gt 1) { | |
$TargetsA = [System.Collections.ArrayList] $Targets | |
$TargetsB = [System.Collections.ArrayList] @() | |
} | |
While ($True) { | |
# Ensure it loops through all targets in random order before going through them all over again | |
If ($Targets.Count -gt 1) { | |
If ($TargetsA.Count -eq 0) { | |
$TargetsA = $TargetsB | |
$TargetsB = [System.Collections.ArrayList] @() | |
} | |
If ($TargetsA.Count -eq 1) { | |
$Index = 0 | |
} Else { | |
$Index = Get-Random -Minimum 0 -Maximum ($TargetsA.Count - 1) | |
} | |
$Target = $TargetsA[$Index] | |
$TargetsB.Add($Target) | Out-Null | |
$TargetsA.RemoveAt($Index) | |
} Else { | |
$Target = $Targets[0] | |
} | |
$DateNow = (Get-Date -Format "hh:mm:ss tt").ToUpper() | |
Write-Host -NoNewLine ("[{0}] {1} -> " -F $DateNow, $Target) | |
$TestResult = Test-Connection -TargetName $Target -Count 1 -BufferSize $BufferSize | |
If ($TestResult.Status -eq "Success") { | |
"{0} ({1}ms)" -F $TestResult.Status, $TestResult.Latency | |
Start-Sleep -Seconds $Delay | |
} Else { | |
$TestResult.Status | |
Start-Sleep -Seconds $FailureDelay | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment