Skip to content

Instantly share code, notes, and snippets.

@cnavarroestrella
Last active February 7, 2020 19:40
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save cnavarroestrella/52b5cb0d092ceed933fa to your computer and use it in GitHub Desktop.
#!/bin/bash
# PINGLOOP with OS X wifi bugfix
# Will ping google.com or domain provided via parameter.
# Keep this running in background to monitor connection and fix automatically.
# By Carlos Navarro, with thanks to Kevin Ritchie for the wifi bugfix code.
# Bugfix code provided here: http://osxdaily.com/2015/10/16/fix-wi-fi-problems-mac-os-x-el-capitan/
# NOTE: This is not a proper fix. This simply refreshes wifi configuration when connection failure is
# detected, equivalent to turning wifi off/on but faster. There could be, at worst, up to a
# second of no connectivity, which may break some active connections.
# Permanent fix remains up to Apple.
# Contact via:
# * Twitter: twitter.com/Nosgoroth
# * Email: nosgoroth@gmail.com
# Pingloop output legend
# * Green "O" -> Ping was OK
# * Red "X" -> Ping failed
# * Yellow "!" -> Wifi bugfix was applied
#################
# Configuration #
#################
# Set to zero to apply fix immediately after offline condition detected.
# A nonzero value prevents notifications for momentary ping errors.
min_seconds_before_offline=0
# Set to false to disable toast notifications.
notify=true
# Set to false to disable OS X wifi bug fix.
wifi_bugfix=true
# Set to true to enable some extra output.
debug=false
# Change default address to ping here.
# You can also specify a domain via parameters (pingloop apple.com)
defaultaddr=google.com
#############
# Functions #
#############
not(){
osascript -e "display notification \"${*/\"/\\\"}\" with title \"Alert\""
}
do_ping(){
res=$(ping -q -c 1 -t 1 $address &> /dev/null)
return $?
}
apply_wifi_bugfix(){
echo "add State:/Network/Interface/en0/RefreshConfiguration temporary" | scutil
}
########
# Init #
########
tput clear
if [[ -z "$1" ]]; then
address=$defaultaddr
addrname=Network
else
address=$1
addrname=Domain\ $1
fi
_user=$(whoami)
if [ "$_user" == "root" ]; then
_root=true
else
_root=false
fi
##############
# Info block #
##############
if $debug; then
echo "$(tput bold)$(tput setaf 3)DEBUG MODE$(tput sgr 0)"
echo
fi
if $wifi_bugfix; then
if $_root; then
echo "WiFi bufgix: $(tput setaf 2)enabled$(tput sgr 0)"
else
echo "WiFi bufgix: $(tput bold)$(tput setaf 3)enabled but cannot apply$(tput sgr 0) - run with $(tput bold)sudo$(tput sgr 0)"
fi
else
echo "WiFi bufgix: $(tput setaf 1)disabled$(tput sgr 0)"
fi
if $notify; then
echo "Toast notifications: $(tput setaf 2)enabled$(tput sgr 0)"
else
echo "Toast notifications: $(tput setaf 1)disabled$(tput sgr 0)"
fi
if (( $min_seconds_before_offline > 0 )); then
echo "Offline condition will be treated after it persists for $(tput bold)$(tput setaf 2)$min_seconds_before_offline$(tput sgr 0) seconds"
else
echo "Offline condition will be treated $(tput bold)$(tput setaf 3)immediately$(tput sgr 0)"
fi
echo
echo "Pinging $(tput bold)$address$(tput sgr 0) in loop. Press $(tput bold)Ctrl+C$(tput sgr 0) to exit."
#############
# Main loop #
#############
trap 'echo; echo Exiting...; exit' INT
s="0"
ctrbuf=-1
minbuf=$min_seconds_before_offline
while true; do
do_ping &> /dev/null
r=$?
if [ "$r" == "0" ]; then
echo -n "$(tput setaf 2)O$(tput sgr 0)"
else
echo -n "$(tput setaf 1)X$(tput sgr 0)"
fi
if [ "$s" != "$r" ] && (( $ctrbuf > $minbuf )); then
ctrbuf=0
else
ctrbuf=$(( ctrbuf + 1 ))
fi
$debug && echo -n ".$r$s."
if [ "$r" == "0" ] && [ "$s" != "0" ]; then
$debug && echo -n "~"
$notify && not "$addrname online"
s="$r"
elif [ "$r" != "0" ] && [ "$s" == "0" ] && (( $ctrbuf + 1 > $minbuf )); then
$debug && echo -n "~"
if $wifi_bugfix && $_root; then
apply_wifi_bugfix
echo -n "$(tput bold)$(tput setaf 3)!$(tput sgr 0)"
$notify && not "Applied wifi bugfix"
else
$notify && not "$addrname offline"
fi
s="$r"
fi
if [ "$r" == "0" ]; then
sleep 1
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment