Okay.
1) I am not sure how to paste code so this may not work or look pretty.
2) This used to run an a computer using TigerOS and was written in Applescript 2-something I think, so I don't know how it will handle Sierra or whatever.
3) I am not a scripter. I reverse engineer and hack together. Technical suggestions welcome otherwise keep quiet in the peanut gallery. 🙂
(*
Applescript to test quality of an Internet connection by pinging a server a number of times, repeating this at intervals until the percentage packet loss drops below a certain threshold for two consecutive ping sets, whereupon it beeps, plays a notification and displays a message.
Basic script structure taken from http://macscripter.net/viewtopic.php?id=17519
AWK command modified from http://stackoverflow.com/questions/16618371/using-awk-to-grab-only-numbers-from- a-string
Settings entry section from https://discussions.apple.com/thread/2038077?answerId=9617054022#9617054022
There's probably something from a Dougscript in here too. :-)
MAKE SURE YOU ENTER THE IP OF THE SERVER YOU WISH TO PING WHERE IT ASKS FOR IT JUST BELOW
*)
-- DEFAULTS which can be adjusted
log "Starting script"
property serverURL : "xxx.xx.xxx.xxx" -- use the URL (or an IP address) of your server, or one that you know is ALWAYS up
-- DEFAULTS which can also be temporarily configured when running the application:
set pingCount to 20 as integer -- Set number of pings used in any one iteration to test connection.
set PckLssThr to 15 as integer -- % Packet Loss Threshold. Loss less than this will result in notification.
set cycleTime to 180 as integer -- Delay in seconds until next series of pings. Don't make this too small relative to the time it will take to process your total number of pings.
-- CONSTANTS
property opt1 : "Configure"
property opt2 : "Continue..."
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to "," -- need to put this here so passing of the DefaultSettings array to configure menu ends up including commas separating items
set DefaultSettings to {pingCount, PckLssThr, cycleTime} as string -- need to generate second style of default settings list in string format for configuration part to work
set TstSttng to {}
set ifInfo to 100 -- Don't change this. It sets packet failure report to 100 to enter repeat loop
-- MAIN SCRIPT
try
-- Dialog window allowing user to change some configurations during script execution
set choice to button returned of (display dialog "Monitor network quality with default settings of " & pingCount & " pings, " & PckLssThr & "% packet loss threshold, and " & cycleTime & " second cycle time between tests?" buttons {"Cancel", opt1, opt2} default button 3 with title "Network Quality Monitor")
if choice is opt1 then -- enter configure mode
set TstSttng to text returned of (display dialog "Please enter
1) Number of pings used per test
2) % packet loss value for good connection
3) Time (in seconds) between each test,
separating them with a comma:" default answer DefaultSettings)
set {pingCount, PckLssThr, cycleTime} to {(TstSttng's text item 1), (TstSttng's text item 2), (TstSttng's text item 3)} --
set AppleScript's text item delimiters to astid
end if
set ExitChoice to button returned of (display dialog "Select what to do when script ends successfully." buttons {"Cancel", "Notification", "Start RT Stream logging"} default button 2 with title "Exit strategy")
display alert "Starting network connection quality test." message "Good connection threshold set to ≤" & PckLssThr & "% packet loss for " & pingCount & " pings. Test repeats every " & cycleTime & " seconds." giving up after 7
log "progressing to actual ping script" --Should echo values to Event Log at bottom of window, for testing.
end try
-- Loop runs series of pings, extracts packet loss number from results then compares two consecutive sets of these to a threshold value which determines if a timer is engaged to delay until next loop or loop is exited.
repeat
set oldIfInfo to ifInfo
log "start of ping " & (current date)
set shellCmd to "ping -c " & pingCount & " " & serverURL & " | grep \"packet loss\" | awk -F'[^0-9]*' '{print $3}'"
set ifInfo to do shell script shellCmd
log "end of ping " & (current date)
log "start of test: " & oldIfInfo & " " & ifInfo & " vs " & PckLssThr
if ifInfo ≤ PckLssThr then
log "passed test 1"
if oldIfInfo ≤ PckLssThr then exit repeat
end if
my DisplayUpdate(ifInfo, oldIfInfo, pingCount)
log "passing through more than one cycle." & (current date)
log "ifInfo " & ifInfo
log "old ifInfo " & oldIfInfo
delay cycleTime
end repeat
-- Exit testing phase and report all clear.
if ExitChoice = "Notification" then
log "ifInfo " & ifInfo
log "old ifInfo " & oldIfInfo
delay 2
say "Network connection is clear. "
delay 2
try
set msg to "Average " & (round (oldIfInfo + ifInfo) / 2 rounding to nearest) & "% packet loss over last two sets of " & pingCount & " pings. Connection clear."
display dialog msg buttons {"Quit"} default button 1 with icon note giving up after 300
-- on error number -1712
end try
end if
if ExitChoice = "Start RT Stream logging" then
display dialog "Would normally run notification scripts"
end if
log "Script exiting"
on DisplayUpdate(ifInfo, oldIfInfo, pingCount)
try
display alert ("Last cycle " & ifInfo & "% packet loss
Previous cycle " & oldIfInfo & "% packet loss
Script continuing using " & pingCount & " pings.
" & (current date)) giving up after 30
end try
end DisplayUpdate