Skip to content

Instantly share code, notes, and snippets.

@gtfunes
Created March 14, 2019 18:26
Show Gist options
  • Save gtfunes/58c1de6eca94f3038b6cba207a0b95bf to your computer and use it in GitHub Desktop.
Save gtfunes/58c1de6eca94f3038b6cba207a0b95bf to your computer and use it in GitHub Desktop.
Bash script to switch between wifi networks when they lose connection (works on macOS & Linux)
#!/bin/bash
# - WiFi switching script -
# Reads from wifis.txt file that has one network per line name=password
echo "Loading known networks list..."
MAX_NETWORKS=0
while IFS='=' read -r var value || [[ -n "$line" ]]; do
WIFI_NETWORK_NAMES[MAX_NETWORKS]="${var}"
WIFI_PASSWORDS[MAX_NETWORKS]="${value}"
((MAX_NETWORKS++))
done < wifis.txt
printf "Loaded!\n\n"
CURRENT_NETWORK=0
MAX_SECS=5
SECS=$[$MAX_SECS]
CURRENT_OS=3
get_current_os() {
if (( "${OSTYPE:0:6}" == "darwin" )); then
CURRENT_OS=0
elif (( "${OSTYPE:0:5}" == "linux" )); then
CURRENT_OS=1
elif (( "${OSTYPE:0:4}" == "msys" )); then
CURRENT_OS=2
else
CURRENT_OS=3
fi
}
get_current_os
if [ $CURRENT_OS -gt 1 ]; then
echo "$OSTYPE is not supported, bye!"
exit 0;
else
case $CURRENT_OS in
0)
echo "Running on MacOS"
;;
1)
echo "Running on Linux"
;;
esac
fi
AIRPORT="wlan0";
get_current_adapter() {
case $CURRENT_OS in
0)
AIRPORT="$( networksetup -listnetworkserviceorder | grep 'Hardware Port: Wi-Fi' | sed 's/.* //' | sed 's/.$//' )";
;;
1)
AIRPORT="$( nmcli d | grep -i -a 'wifi' | sed 's/ .*//' )";
;;
esac
}
get_current_adapter
echo "Running on $AIRPORT adapter"
connect_to_current_network() {
case $CURRENT_OS in
0)
networksetup -setairportnetwork $AIRPORT "${WIFI_NETWORK_NAMES[$CURRENT_NETWORK]}" "${WIFI_PASSWORDS[$CURRENT_NETWORK]}" | grep -i -a "Failed"
;;
1)
iwconfig $AIRPORT essid "${WIFI_NETWORK_NAMES[$CURRENT_NETWORK]}" key s:"${WIFI_PASSWORDS[$CURRENT_NETWORK]}" | grep -i -a "Failed"
;;
esac
sleep 5
}
get_current_network() {
case $CURRENT_OS in
0)
networksetup -getairportnetwork $AIRPORT
;;
1)
nmcli d | grep -i -a "wifi"
;;
esac
}
reset_network_adapter() {
case $CURRENT_OS in
0)
networksetup -setairportpower $AIRPORT off
networksetup -setairportpower $AIRPORT on
;;
1)
nmcli networking off
nmcli networking on
;;
esac
}
is_network_alive() {
ping -q -c 1 -W 1 8.8.8.8 >/dev/null
}
echo "Interval is set to $MAX_SECS"
echo "Total networks is $MAX_NETWORKS"
echo "Connecting to network \"${WIFI_NETWORK_NAMES[$CURRENT_NETWORK]}\""
connect_to_current_network
printf "Connected!\n\n"
while true; do
if (( $SECS == 0 )); then
SECS=$[$MAX_SECS]
if is_network_alive; then
echo "Connection is still up on \"${WIFI_NETWORK_NAMES[$CURRENT_NETWORK]}\"!"
else
echo "Connection on \"${WIFI_NETWORK_NAMES[$CURRENT_NETWORK]}\" is down!"
echo "Switching networks..."
CURRENT_NETWORK++
if (( $CURRENT_NETWORK >= $MAX_NETWORKS )); then
CURRENT_NETWORK=0;
fi
if connect_to_current_network; then
printf "Failed connecting to \"${WIFI_NETWORK_NAMES[$CURRENT_NETWORK]}\", restarting adapter...\n\n";
reset_network_adapter
sleep 1
else
get_current_network
fi
fi
fi
sleep 1
SECS=$[$SECS-1]
done
exit 0;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment