I deal with a number of remote workers who, for one reason or the other, don’t work in the company office. Often, they’re using a VPN tunnel to connect to a server back at the company.
Occasionally, we’ll see intermittent connectivity issues from the client. Perhaps it’s their ISP, perhaps it’s the VPN tunnel, perhaps it’s a piece of software triggering IDS on a managed firewall.
In any case, we can triangulate the problem by launching a script on the client’s side that pings endpoints of our choosing to check connectivity. But we also want to time stamp and capture the results of the pings to a text file we can review later.
This is where
tee
is your friend. As the man entry says, tee is a “pipe fitting”.
The tee utility copies standard input to standard output, making a copy in zero or more files.
So, here are our requirements:
- Script is user-initiated.
- Script gets out of the user’s way.
- Timestamps and logs the pings to a text file in a folder on the Desktop.
This Applescript, which makes a bunch of bash calls, does all of that.
# Simple ping monitor # A script that pings servers of your choice by IP or DNS name and logs the results to a text file in a folder on the Desktop. # # Written by AB @ Modest Industries (modestindustries.com) # # 2012-07-25 - AB: First draft. # 2014-07-25 - AB: Formatting cleanup. #Servers to ping. For each server you name here, you'll need to set up a ping statement below. set server1 to "google.com" set server2 to "8.8.8.8" set server3 to "yahoo.com property the_prefix : space property the_sep : "-" # Format a date to use as a datestamp. on myDate() set myYear to "" & year of (current date) set myMth to text -2 thru -1 of ("0" & (month of (current date)) * 1) set myDay to text -2 thru -1 of ("0" & day of (current date)) set myHours to hours of (current date) set myMinutes to minutes of (current date) return {myYear, myMth, myDay, myHours, myMinutes} end myDate # Check for a folder called Monitoring on the Desktop. If it doesn't exist, make one. tell application "Finder" set the directory to desktop if (exists folder "Monitoring") is false then make new folder at desktop with properties {name:"Monitoring"} end if set the_path to folder "Monitoring" of desktop set the_name to (item 1 of my myDate()) set the_name to (the_name & the_sep & item 2 of my myDate()) set the_name to (the_name & the_sep & item 3 of my myDate()) set the_timestamp to item 4 of my myDate() & item 5 of my myDate() -- set the directory to "Monitoring" if (exists folder the_name of folder "Monitoring" of desktop) is false then make new folder at the_path with properties {name:the_name} end if set the_path to folder the_name of folder "Monitoring" of desktop as alias set posixPath to POSIX path of the_path end tell # Ping servers of your choice. You'll need one statement for each server named above. tell application "Terminal" to do script "ping " & server1 & " | while read pong; do echo \"$(date): $pong\"; done | tee " & quoted form of posixPath & the_name & the_sep & the_timestamp & the_sep & server1 & ".txt" tell application "Terminal" to do script "ping " & server2 & " | while read pong; do echo \"$(date): $pong\"; done | tee " & quoted form of posixPath & the_name & the_sep & the_timestamp & the_sep & server2 & ".txt" tell application "Terminal" to do script "ping " & server3 & " | while read pong; do echo \"$(date): $pong\"; done | tee " & quoted form of posixPath & the_name & the_sep & the_timestamp & the_sep & server3 & ".txt" # Hide all the windows. tell application "System Events" to set visible of process "Terminal" to false # Tell the user it's running. display dialog "Ping monitor is running!" buttons {"OK"} default button 1 # Switch back to the Finder. tell application "Finder" to activate
You might want to tweak the dialogue to tell the user to leave the Terminal app running.
Should this be a bash script? Probably. But this works and can be launched by the user and hides most of the gubbins so that the user can get on with their business.