Last active
July 28, 2021 16:56
-
-
Save amalakar/08aa0925ca53eb323314e323dbc92595 to your computer and use it in GitHub Desktop.
Checks current bandwidth and forgets/joins wifi if it is lower than min threshold
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /bin/bash | |
# Needs brew install speedtest-cli | |
network='en0' | |
wifi_name='XXXX-TODO' | |
wifi_password='YYYY-TODO' | |
min_bandwidth_mbps=125 | |
function download_speed() { | |
bps=$(speedtest-cli --no-upload --json | jq .download) | |
bps=${bps%.*} | |
mbps=$(($bps/(1024*1024))) | |
echo $mbps | |
} | |
mbps=$(download_speed) | |
echo "Current bandwidth is: $mbps Mbps" | |
if [ "$mbps" -gt "${min_bandwidth_mbps}" ]; then | |
echo "Won't reset wifi, as bandwidth is high ${mbps} Mbps > ${min_bandwidth_mbps} Mbps" | |
exit 0 | |
else | |
echo "Will reset wifi, as bandwidth is low ${mbps} Mbps < ${min_bandwidth_mbps} Mbps" | |
fi | |
echo "About to switch off WiFi" | |
networksetup -setairportpower "${network}" off | |
echo "About to forget WiFi $wifi_name" | |
sudo networksetup -removepreferredwirelessnetwork "${network}" "${wifi_name}" | |
echo "About to switch on WiFi" | |
networksetup -setairportpower "${network}" on | |
echo "About to sleep for 15 seconds" | |
sleep 15 | |
echo "About to join WiFi: ${wifi_name}" | |
networksetup -setairportnetwork "${network}" "${wifi_name}" "${wifi_password}" | |
mbps=$(download_speed) | |
echo "Current bandwidth is: $mbps Mbps" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment