Skip to content

Instantly share code, notes, and snippets.

@infostreams
Created November 8, 2016 09:21
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save infostreams/20eda7a267a3cb526b94c9f5ef719db4 to your computer and use it in GitHub Desktop.
Save infostreams/20eda7a267a3cb526b94c9f5ef719db4 to your computer and use it in GitHub Desktop.
Collect details about internet / wifi availability (macOS)
#!/bin/sh
SCRIPTPATH=$(cd "$(dirname "$0")"; pwd -P)
while [ true ]
do
${SCRIPTPATH}/status.sh >> ${SCRIPTPATH}/log.txt
sleep 10
done
#!/bin/sh
ssid=$(/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport -I | awk '/ SSID/ {print substr($0, index($0, $2))}')
services=$(networksetup -listnetworkserviceorder | grep 'Hardware Port')
while read line; do
sname=$(echo $line | awk -F "(, )|(: )|[)]" '{print $2}')
sdev=$(echo $line | awk -F "(, )|(: )|[)]" '{print $4}')
#echo "Current service: $sname, $sdev, $currentservice"
if [ -n "$sdev" ]; then
ifconfig $sdev 2>/dev/null | grep 'status: active' > /dev/null 2>&1
rc="$?"
if [ "$rc" -eq 0 ]; then
currentservice="$sname"
fi
fi
done <<< "$(echo "$services")"
ping -c 1 www.google.com >/dev/null 2>&1
code=${?}
if [ "${code}" -eq 0 ]; then
have_internet=1
else
have_internet=0
fi
time=$(date +"%Y-%m-%d %H:%M:%S")
echo "${time} | Have internet: ${have_internet} | Connected through: ${currentservice} | SSID: ${ssid}"
@infostreams
Copy link
Author

infostreams commented Nov 8, 2016

This collection of scripts prints (status.sh) or logs (record.sh) whether or not you have internet, how you are connected to the internet, and what the wifi SSID is. This is useful for logging unreliable internet connections.

To determine if you have a functioning internet connection it pings www.google.com, but for some reason this sometimes gives unreliable results (false positives): it can claim that you don't have a functioning internet connection when you actually do have one. If you find out why these false positives occur, please comment or (even better) provide adjusted code.

Only works on macOS for now, but that's only because it relies on an obscure Apple tool to figure out which SSID you are connected to. Should be relatively straight forward to make it work on both Linux and Mac, but don't have the time now. Let me know if you manage to get it to work on both!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment