Skip to content

Instantly share code, notes, and snippets.

@bws0013
Created December 20, 2020 01:17
Show Gist options
  • Save bws0013/9f31fe2978542fc7f2b9c08745c8bb32 to your computer and use it in GitHub Desktop.
Save bws0013/9f31fe2978542fc7f2b9c08745c8bb32 to your computer and use it in GitHub Desktop.
My macbook has been encountering sporadic wifi issues in its old age. To cope with this I made a script designed to reset my wifi should this connection drop occur.
#!/bin/bash
# Steps to run, either add execution and run, or run the following
# bash <path-to-script>/bandaid.sh &
# To kill script find the pid with ps and kill or run the following
# kill -9 $(pgrep -f bandaid.sh)
# My System: macOS High Sierra, Version 10.13.6
# Bash version: GNU bash, version 3.2.57
# My setup
# 2 Aliases in my bash profile for running and killing
# alias bon='bash <path-to-script>/bandaid.sh &'
# alias boff='kill -9 $(pgrep -f bandaid.sh)'
# Get exit code for if screen is active
# Based on https://stackoverflow.com/a/39546692
check_for_active_screen() {
ioreg -c AppleBacklightDisplay | grep brightness | grep "dsyp\"={\"min\"=0,\"max\"=2,\"value\"=2}" > /dev/null
}
# Get exit code of two attempts at pinging google's public dns server
check_for_connection() {
ping -c2 -W2 '8.8.8.8' > /dev/null 2>&1
}
# Reset my wifi connection
# Based off of https://osxdaily.com/2011/05/31/enable-disable-airport-wireless-connections-command-line/
reset_wifi() {
networksetup -setairportpower "Wi-Fi" off > /dev/null 2>&1
sleep 1
networksetup -setairportpower "Wi-Fi" on > /dev/null 2>&1
}
# If I want to record instances of restarts
# output_file='<path-to-output>/output.out'
while :; do
check_for_active_screen
# Verify that we are not in sleep mode
if [ $? == 0 ]; then
check_for_connection
# Verify that our connection is down
if [ $? != 0 ]; then
# Reset wifi
reset_wifi
# If I want to record instances of restarts
# date >> $output_file
# echo 'Wifi Restarted' >> $output_file
# echo '---------------' >> $output_file
fi
fi
sleep 7
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment