Skip to content

Instantly share code, notes, and snippets.

@JayBrown
Last active May 13, 2020 03:55
Show Gist options
  • Save JayBrown/851c337222f3d5521901dae1c88d648f to your computer and use it in GitHub Desktop.
Save JayBrown/851c337222f3d5521901dae1c88d648f to your computer and use it in GitHub Desktop.
#!/bin/zsh
# shellcheck shell=bash
# wifi-auto-switch.zsh for macOS
# v0.2
emulate -LR bash
export LANG=en_US.UTF-8
# switch WiFi network function
_switchwifi () {
# get WiFi device identifier (often 'en0')
wifidevice=$(networksetup -listnetworkserviceorder | grep "^(Hardware Port: Wi-Fi," | rev | awk '{print $1}' | rev | sed 's/)$//')
if ! [[ $wifidevice ]] ; then
echo "Error: WiFi device not found!" >&2
else # switch network
error=false
if [[ $1 == 24 ]] ; then # switch to 2.4 GHz network
echo "Switching to 2.4 GHz network..."
if ! networksetup -setairportnetwork "$wifidevice" "$ssid_base$suffix_24ghz" "$pw_24ghz" ; then
error=true
fi
elif [[ $1 == 50 ]] ; then # switch to 5.0 GHz network
echo "Switching to 5.0 GHz network..."
if ! networksetup -setairportnetwork "$wifidevice" "$ssid_base$suffix_50ghz" "$pw_50ghz" ; then
error=true
fi
fi
if $error ; then
echo "Error switching WiFi network!" >&2
fi
fi
}
##################################
######### USER VARIABLES #########
##################################
ssid_base="MyNetwork" # basename of the WiFi's SSID (without suffixes)
suffix_50ghz=".5" # suffix for 5.0 GHz SSID
suffix_24ghz=".2.4" # suffix for 2.4 GHz SSID
bssid_5p0ghz="00:00:00:00:00:00" # 5.0 GHz BSSID
bssid_2p4ghz="00:00:00:00:00:00" # 2.4 GHz BSSID
rssi_threshold_50="20" # signal strength difference threshold above which to switch to 2.4 GHz
rssi_threshold_24="15" # signal strength difference threshold below which to switch back to 5.0 GHz
# NOTE: threshold values can be identical!
pw_50ghz="myPassword1" # 5.0 GHz network passphrase
pw_24ghz="myPassword2" # 2.4 GHz network passphrase
# NOTE: escape '$' (string/dollar) characters with '\' (backslash) in passphrases!
##################################
# macOS airport CLI location
airport="/System/Library/PrivateFrameworks/Apple80211.framework/Versions/A/Resources/airport"
# current WiFi status
bssid_current=$("$airport" -I | awk '/BSSID:/{print $2}')
if [[ $bssid_current == "$bssid_5p0ghz" ]] ; then
echo "Current BSSID: $bssid_5p0ghz [5 GHz]"
net50=true
net24=false
elif [[ $bssid_current == "$bssid_2p4ghz" ]] ; then
echo "Current BSSID: $bssid_2p4ghz [2.4 GHz]"
net50=false
net24=true
else
echo "Not connected to home WiFi! Exiting..."
exit
fi
# WiFi environment scan
while true
do
scan_all=$("$airport" -s 2>/dev/null)
scan_ssid=$(echo "$scan_all" | grep "$ssid_base")
if ! [[ $scan_ssid ]] ; then
echo "Home WiFi network '$ssid_base' not detected! Exiting..."
exit
else
if [[ $(echo "$scan_ssid" | wc -l) == 1 ]] ; then
echo "Only one home WiFi network detected! Exiting..."
exit
else
break
fi
fi
done
# RSSI values (signal strength)
rssi50=$(echo "$scan_ssid" | grep "$bssid_5p0ghz" | awk -F"$bssid_5p0ghz" '{print substr($0, index($0,$2))}' | awk '{print $1}' | tr -d -c 0-9)
rssi24=$(echo "$scan_ssid" | grep "$bssid_2p4ghz" | awk -F"$bssid_2p4ghz" '{print substr($0, index($0,$2))}' | awk '{print $1}' | tr -d -c 0-9)
echo -e "Current 5 GHz RSSI: $rssi50\nCurrent 2.4 GHz RSSI: $rssi24"
# RSSI difference between 5.0 & 2.4 GHz # always subtract 5.0 from 2.4 because 2.4 GHz is usually stronger
rssi_diff=$(echo "$rssi24 - $rssi50" | bc | tr -d -c 0-9)
echo "RSSI difference: $rssi_diff"
# check for network switching
if $net50 ; then
rssi_threshold="$rssi_threshold_50"
echo "Threshold: $rssi_threshold"
if [[ $rssi_diff -lt "$rssi_threshold" ]] ; then
echo "Value is below threshold: exiting..."
exit
else # on 5.0 GHz the value needs to be *above* threshold, i.e. decreased strength of 5.0 GHz
echo "Value is above threshold: switching WiFi network..."
_switchwifi 24
fi
elif $net24 ; then
rssi_threshold="$rssi_threshold_24"
echo "Threshold: $rssi_threshold"
if [[ $rssi_diff -gt "$rssi_threshold" ]] ; then
echo "Value is above threshold: exiting..."
exit
else # on 2.4 GHz the value needs to be *below* threshold, i.e. increased strength of 5.0 GHz
echo "Value is below threshold: switching WiFi network..."
_switchwifi 50
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment