Created
March 4, 2017 08:27
-
-
Save grayatrox/430696af6df3aafd514bf57ef0dd0a08 to your computer and use it in GitHub Desktop.
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
#Persistent | |
#SingleInstance force | |
localIP := "192.168.0.1" | |
remoteIP := "8.8.8.8" | |
refresh := 60*5 ; Seconds | |
icon_connected := 102 | |
icon_disconnected := 101 | |
icon_loading := 100 | |
TrayIcon(icon_loading,prevIcon) | |
menu, Tray, Tip, Loading... | |
menu, tray, NoStandard | |
menu, Tray, add, Ping Local Server (%localIP%), MenuHandler | |
menu, tray, add, Ping Remote Server (%RemoteIP%), MenuHandler | |
menu, tray, add, Reload | |
menu, Tray, add, Exit | |
SetTimer, BeginTesting, % 1000*refresh | |
goto BeginTesting | |
return | |
MenuHandler: | |
if (InStr(A_ThisMenuItem, "Ping Local Server (" localIP ")")){ | |
local := simplePing(localip) | |
Msgbox % local.all | |
} | |
if (InStr(A_ThisMenuItem, "Ping Remote Server (" remoteIP ")")){ | |
remote := simplePing(remoteip) | |
Msgbox % remote.all | |
} | |
Return | |
Exit: | |
ExitApp | |
return | |
Reload: | |
Reload | |
return | |
BeginTesting: | |
localReceived:=0 | |
localLoss:=0 | |
remoteLoss:=0 | |
remoteReceived:=0 | |
remoteResponse := 0 | |
LocalResponse := 0 | |
remote := SimplePing(remoteIp) | |
local := SimplePing(localIP) | |
tip := "IP:" remoteIp ": " | |
if (remote.trip.Average){ | |
Tip .= remote.statistics.all "`n" | |
} | |
else { | |
Tip .= remote.reply[1] "`n" | |
} | |
tip .= "IP:" LocalIP ": " | |
if (local.trip.Average){ | |
Tip .= local.statistics.all | |
} | |
else { | |
Tip .= local.reply[1] | |
} | |
Menu,Tray,Tip, %Tip% | |
tip := "" | |
if (InStr(remote.statistics.Lost,"100% loss")) | |
TrayIcon(icon_disconnected,prevIcon) | |
else | |
TrayIcon(icon_connected,prevIcon) | |
Return | |
TrayIcon(icon, ByRef prevIcon){ | |
if (icon != prevIcon){ | |
Menu, Tray, Icon, C:\Windows\System32\imageres.dll, %icon% | |
prevIcon := icon | |
} | |
return | |
} | |
Return | |
SimplePing(Url,n=4,TimeoutMS=3000) { | |
LogFile := A_Temp "/Ping_" A_Now ".log" | |
Runwait, %comspec% /c ping -w %TimeoutMS% -n %n% %Url%>%LogFile%, , hide | |
FileRead, Contents, %LogFile% | |
reply := object() | |
statistics := {} | |
trip := {} | |
pingResults := {} | |
pingResults.All := {} | |
Loop, Read, %LogFile% | |
{ | |
pingResults.all .= A_LoopReadLine "`n" | |
if (Instr(A_LoopReadLine, "Reply") || Instr(A_LoopReadLine, "failed") || Instr(A_LoopReadLine, "Request timed out.")) | |
reply.Push(A_LoopReadLine) | |
else if (InStr(A_LoopReadLine,"Ping statistics for")) | |
statisticsLine:=true | |
else if (statisticsLine){ | |
tmp := strSplit(A_LoopReadLine, "=") | |
statistics.all := Substr(A_LoopReadLine,1,-1) | |
statistics.Sent := StrSplit(tmp[2],",")[1] | |
statistics.Received := StrSplit(tmp[3],",")[1] | |
statistics.Lost := StrSplit(tmp[4],",")[1] | |
statisticsLine := false | |
}else if (Instr(A_LoopReadLine,"Approximate round trip times in milli-seconds:")) | |
roundtripLine := true | |
else if (roundtripLine){ | |
tmp := strSplit(A_LoopReadLine, "=") | |
trip.all := A_LoopReadLine | |
trip.Minimum := SubStr(StrSplit(tmp[2],",")[1],1,-2) | |
trip.Maximum := SubStr(StrSplit(tmp[3],",")[1],1,-2) | |
trip.Average := SubStr(StrSplit(tmp[4],",")[1],1,-2) | |
roundtripLine := false | |
} | |
} | |
pingResults.reply := reply | |
pingResults.statistics := statistics | |
pingResults.trip := trip | |
FileDelete, %LogFile% | |
return pingResults | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment