Skip to content

Instantly share code, notes, and snippets.

@DavidPesticcio
Last active June 18, 2018 17:27
Show Gist options
  • Save DavidPesticcio/b0a48b99fd12c8b47f4e463ed0626130 to your computer and use it in GitHub Desktop.
Save DavidPesticcio/b0a48b99fd12c8b47f4e463ed0626130 to your computer and use it in GitHub Desktop.
Does your wifi drop because of dodgy infrastructure or hardware?
# Put the function below in your .profile or .bashrc or other suitable file
# Name the function whatever you fancy - I chose wifi
# Change remote & delay accordingly
# Call it with no parameters, to watch and recover the existing wifi connection with feedback
# Call it with any parameter(s) to check and/or recover the existing wifi connection with feedback
# Capture some stats to a logfile as evidence of your crappy infrastructure. :-)
function wifi () {
# TODO: Use GNU date to calculate seconds instead of SECONDS
# TODO: Persist daily stats.
# TODAY = Today's date
# DELAY = number of seconds to wait befor retrying re-connect
# REMOTE = Remote IP address for pinger function
# RECONNECTS = Number of reconnections today.
# DURATION_MAX = Used to calculate MAX connection time.
# DURATION_TOTAL = Used to calculate AVG connection time.
# SECONDS = Duration of last connection / re-connect.
# SSID = The SSID of the wireless network
# LOGFILE = Where to log output
local oneShot=${1:-}
local TODAY=$(date +%F)
local DELAY=2
local REMOTE=8.8.8.8
local RECONNECTS=1
local DURATION_MAX=0
local DURATION_TOTAL=0
local SSID=$(nmcli device | awk '$2 == "wifi" {print $NF; exit}')
local WIFI=$(nmcli device | awk '$2 == "wifi" {print $1; exit}')
local LOGFILE=/tmp/wifi_status_${SSID}
SECONDS=0
show_banner() {
echo "${TODAY}: Watching ${SSID} connection on ${WIFI}..."
echo " - [Reconnects] (Duration) {Average}"
}
show_duration() {
printf "% 5i" ${SECONDS}
}
show_duration_avg() {
printf "% 5i" $((${DURATION_TOTAL} / ${RECONNECTS}))
}
show_reconnects() {
printf "% 4i" ${RECONNECTS}
}
show_stats() {
case $1 in
reconnect) echo -n "No connection at $(date +%T) [$(show_reconnects)] ($(show_duration)) {$(show_duration_avg)} "
;;
daily|*) echo "${TODAY}: Reconnects: [$(show_reconnects)] / Duration Max: (${DURATION_MAX}) / Duration Avg: {$(show_duration_avg)}"
;;
esac
}
set_duration_max() {
if [ $SECONDS -gt $DURATION_MAX ]
then
DURATION_MAX=$SECONDS
fi
}
set_duration_total() {
DURATION_TOTAL=$((${DURATION_TOTAL} + ${SECONDS}))
}
pinger() {
ping -c 1 -W 1 $REMOTE > /dev/null 2>&1
}
wifi_restart() {
nmcli r wifi off
nmcli r wifi on
}
trap show_stats 1 2 3 4
if [ -z "$oneShot" ]
then
show_banner
fi
while true
do
# If today is a new day, output some stats and reset some counters.
if [ "${TODAY}" != "$(date +%F)" ]
then
show_stats daily
TODAY=$(date +%F)
show_banner
RECONNECTS=1
fi
local reconnected=0
if ! pinger
then
set_duration_total
set_duration_max
show_stats reconnect
wifi_restart
SECONDS=0
while ! pinger
do
if [ $((${SECONDS} % ${DELAY})) -eq 0 ]
then
echo -n ">"
nmcli r wifi on
nmcli device wifi rescan ssid ${SSID} > /dev/null 2>&1
fi
sleep ${DELAY}
done
echo " Re-Connected in $SECONDS seconds."
reconnected=1
RECONNECTS=$((${RECONNECTS} + 1))
SECONDS=0
fi
if [[ -n "$oneShot" && $reconnected -eq 0 ]]
then
echo "Already cnnected."
break
else
if [[ -n "$oneShot" ]]
then
break
fi
fi
sleep ${DELAY}
done | tee -a ${LOGFILE}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment