Permalink
Branch: master
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
executable file 74 lines (64 sloc) 1.85 KB
#!/bin/bash
set -e -f -u -o pipefail
# Run this command to configure the network to keep trying if connecting to the
# network fails.
# $ dir="/System/Library/PrivateFrameworks/Apple80211.framework/Versions/A"
# $ sudo "${dir}/Resources/airport" \
# prefs joinmode=Preferred joinmodefallback=KeepLooking
# Run "airport prefs" to check current preferences.
readonly ATTEMPTS="${1:-10}" SLEEP_TIME="${2:-20}"
readonly DRYRUN="no"
# Figure out which network interface is wifi from the output of
# networksetup -listnetworkserviceorder
readonly WIFI_DEV="en1"
readonly SSID="burrow"
readonly INTERNAL_IP="192.168.0.1"
readonly LOG_FILE="${HOME}/tmp/reconnect-wifi.log"
PATH="${PATH}:/usr/sbin:/sbin"
log() {
local now
now="$(date)"
echo "${now} $*" >> "${LOG_FILE}"
}
check_wifi() {
log "Starting checks"
# Make separate attempts with delays between then to cope with some transient
# failures.
for i in $(seq 1 "${ATTEMPTS}"); do
if ping -c 5 -o -q "${INTERNAL_IP}" >& /dev/null; then
log "attempt ${i}: internal ping succeeded"
exit 0
fi
log "attempt ${i}: internal ping failed"
sleep "${SLEEP_TIME}"
done
}
reset_wifi() {
ping -c 5 "${INTERNAL_IP}"
echo "Reconnecting wifi"
# Sometimes disabling the wifi is necessary to make everything work again :/
networksetup -setairportpower "${WIFI_DEV}" off
sleep 10
networksetup -setairportpower "${WIFI_DEV}" on
sleep 10
networksetup -setairportnetwork "${WIFI_DEV}" "${SSID}" - \
< "${HOME}/Documents/${SSID}-password.txt"
sleep 30
ping -c 5 "${INTERNAL_IP}"
}
main() {
check_wifi
log "All ${ATTEMPTS} attempts failed :("
if [ "${DRYRUN}" == "no" ]; then
set -x
for i in 1 2 3; do
if reset_wifi; then
exit 0
fi
done
log "All reset_wifi attempts failed"
else
echo "Dryrun is true, not making changes."
fi
}
main "$@"