#!/bin/bash . /etc/init.d/functions.sh MAX_TRIES=25 PREV_PWD=${PWD} start_over() { eend 1 continue } cleanup() { cd ${PREV_PWD} rm -f net.log } trap cleanup INT for ((i=1; i<=MAX_TRIES; i++)); do ebegin "Try $i" # Get networks here so we can change them between cycles ALLOWED_NETWORKS=$(. /etc/conf.d/net; echo "${preferred_aps[@]}") if sudo /etc/init.d/net.wlan0 restart 2>&1 | tee net.log; then if grep net.log \ -e "WARNING: net.wlan0 is already stopping"; then echo "Last network attempt was interrupted; zapping" >/dev/stderr sudo /etc/init.d/net.wlan0 zap start_over elif ! grep net.log \ -e 'received address'; then echo "Didn't receive an IP address" >/dev/stderr start_over elif grep net.log \ -e err \ -e 169.254; then echo "Failed to get a network without errors" >/dev/stderr start_over else NETWORK=$(grep "connected to SSID" net.log | grep -o \".*\") # Strip quotes NETWORK=${NETWORK#\"} NETWORK=${NETWORK%\"} FOUND=0 for AP in ${ALLOWED_NETWORKS}; do if [[ ${AP} = ${NETWORK} ]]; then echo "SSID ${NETWORK} in allowed networks: ${ALLOWED_NETWORKS}" >/dev/stderr FOUND=1 break fi done if [[ ${FOUND} -eq 0 ]]; then echo "Failed to find SSID ${NETWORK} in allowed networks: ${ALLOWED_NETWORKS}" >/dev/stderr start_over fi fi eend 0 break fi eend 1 done