Worklet: Check Internet Connection Speed Mac OS

  • 25 September 2023
  • 1 reply
  • 42 views

Userlevel 2
Badge +1

I wrote this macOS bash Worklet with the help of Otto AI to check the speed of an internet connection on macOS devices.

It’s my entry into the Just Script It challenge! 

Evaluation


# This Worklet runs a speed test on Mac OS devices using the networkQuality command.
# This command was included starting with Mac OS Monterrey.
#Exit non-compliant to run remediation script
exit 1

 

Remediation

# ======================
# Otto AI Generated Code
# ======================

#intro message:
clear
echo "Checking Network Quality......"

# s allows for sequential testing, which tends to yield more accurate results
# c is computer readable output
# networkQuality available starting with Monterrey

output=$(networkQuality -sc)

# Remove leading/trailing whitespace and curly braces
output=${output#*{}
output=${output%{}*}

# Extract the values using pattern matching
uplink_capacity=$(grep -o '"ul_throughput" : [0-9]*' <<< "$output" | awk '{print $NF}')
uplink_capacity=$(($uplink_capacity/1000000))
downlink_capacity=$(grep -o '"dl_throughput" : [0-9]*' <<< "$output" | awk '{print $NF}')
downlink_capacity=$(($downlink_capacity/1000000))
os_version=$(grep -o '"os_version" : "[^"]*"' <<< "$output" | cut -d'"' -f4)
interface=$(grep -o '"interface_name" : "[^"]*"' <<< "$output" | cut -d'"' -f4)

#Get info on whether connection is WiFi or Ethernet
network_type=$(networksetup -listnetworkserviceorder | grep -C1 $interface)
connection_type=$(echo "$network_type" | awk -F 'Hardware Port: ' '/Hardware Port:/{print $2}' | cut -d',' -f1)

clear

#Output the Info
echo "Download Speed: $downlink_capacity Mbps"
echo "Upload Speed: $uplink_capacity Mbps"
echo "OS Version: $os_version"
echo "Interface: $interface"
echo "Connection Type: $connection_type"

 


1 reply

Userlevel 5
Badge

Awesome work on this, Landon! Thanks for sharing :) 

Reply