Last active
November 30, 2016 04:00
-
-
Save tcaddy/bad6db94c36429c9026599dbb201a32a to your computer and use it in GitHub Desktop.
PowerShell Script to ping the gateway constantly at intervals below 1000 milliseconds so that flaky wireless connections stay alive.
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
# This script will let you ping in intervals less than 1 second | |
$ip4_gateway = (Get-WmiObject -Class Win32_IP4RouteTable | where { $_.destination -eq '0.0.0.0' -and $_.mask -eq '0.0.0.0'} | Sort-Object metric1 | select nexthop).nexthop | |
$target = $ip4_gateway # target host | |
$interval = 200 # in milliseconds | |
$ping=New-Object System.Net.NetworkInformation.ping | |
function Ping-It() { | |
$success = 0 # initialize as zero | |
$failure = 0 # initialize as zero | |
$cumaltive_time = 0 # initialize as zero | |
$iterations = 0 # initialize as zero | |
$times = @() # initialize empty array | |
try { | |
Write-Host "Press CTRL+C to exit" | |
while(($iterations -eq 0) -Or ($r.Status -eq "Success")) { | |
$r = $ping.send($target) | |
Write-Host "Reply from "$target": bytes="($r.Buffer.Length)" time="($r.RoundTripTime)"ms TTL="($r.Options.Ttl) | |
if ($r.Status -eq "Success") { | |
$success += 1 | |
$cumaltive_time += $r.RoundTripTime | |
$times += $r.RoundTripTime | |
} else { | |
$failure += 1 | |
} | |
$iterations += 1 | |
sleep -Milliseconds $interval | |
} | |
} finally { | |
if ($r.Status -ne "Success") { | |
$r | |
} | |
$measure = $times | measure -Maximum -Minimum -Average | |
$avg = "{0:N3}" -f $measure.Average | |
$max = $measure.Maximum | |
$min = $measure.Minimum | |
Write-Host "Ping statistics for "$target":" | |
Write-Host " Packets: Sent ="$iterations", Received ="$success", Lost ="$failure" ("("{0:N1}" -f (($failure * 100.0) / $iterations))"% loss)," | |
Write-Host "Approximate round trip times in milli-seconds:" | |
Write-Host " Minimum = "$max"ms, Maximum = "$min"ms, Average = "$avg"ms" | |
Write-Host "----------------" | |
} | |
} | |
# Infinite loop, press CTRL+C to exit | |
while(1 -eq 1) { | |
Ping-It | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment