Skip to content

Instantly share code, notes, and snippets.

@Gergling
Created October 13, 2017 13:00
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Gergling/57ad8a385510d7e0aae3fdc24f680e7f to your computer and use it in GitHub Desktop.
Save Gergling/57ad8a385510d7e0aae3fdc24f680e7f to your computer and use it in GitHub Desktop.
Bash scripts for restarting wifi when it fails.
#!/bin/bash
recent_restart=1
while true; do
printf $(date +"%Y-%m-%d")
printf " "
printf $(date +"%H:%M:%S")
printf " "
if [[ $(ping -c 1 google.com) ]] > /dev/null 2>&1 ; then
if [ $recent_restart -eq 0 ] ;
then
printf "\n"
fi
recent_restart=1
printf "Internet last up"
else
if [ $recent_restart -eq 1 ]
then
printf "\n"
bash restart.sh
printf "\n"
# echo "Would have restarted wifi"
recent_restart=0
fi
printf "Internet last down"
fi
printf "\r"
sleep 1
done
#!/bin/bash
recent_restart=1
while true; do
printf $(date +"%Y-%m-%d")
printf " "
printf $(date +"%H:%M:%S")
printf " "
if [[ $(ping -c 1 google.com) ]] > /dev/null 2>&1 ; then
if [ $recent_restart -eq 0 ] ;
then
printf "\n"
fi
recent_restart=1
printf "Internet last up"
else
if [ $recent_restart -eq 1 ]
then
printf "\n"
bash restart.sh
printf "\n"
# echo "Would have restarted wifi"
recent_restart=0
fi
printf "Internet last down"
fi
printf "\r"
sleep 1
done
#!/bin/bash
# Check for internet connection.
if [[ $(ping -c 1 google.com) = "ping: cannot resolve google.com: Unknown host" ]]
then
echo "Internet is down"
else
echo "Internet is up"
fi
#!/bin/bash
# get current wifi device
CURRENT_DEVICE=$(networksetup -listallhardwareports | awk '$3=="Wi-Fi" {getline; print $2}')
echo "Current Wi-Fi Device = '$CURRENT_DEVICE'"
# turn off wifi
networksetup -setairportpower $CURRENT_DEVICE off
#!/bin/bash
# get current wifi device
CURRENT_DEVICE=$(networksetup -listallhardwareports | awk '$3=="Wi-Fi" {getline; print $2}')
echo "Current Wi-Fi Device = '$CURRENT_DEVICE'"
# turn on wifi
networksetup -setairportpower $CURRENT_DEVICE on
#!/bin/bash
# get current wifi device
CURRENT_DEVICE=$(networksetup -listallhardwareports | awk '$3=="Wi-Fi" {getline; print $2}')
echo "Current Wi-Fi Device = '$CURRENT_DEVICE'"
echo "Switching off wifi."
# turn off wifi
networksetup -setairportpower $CURRENT_DEVICE off
echo "Switching on wifi."
# turn on wifi
networksetup -setairportpower $CURRENT_DEVICE on
#!/bin/bash
# Check for internet connection.
if [[ $(ping -c 1 google.com) = "ping: cannot resolve google.com: Unknown host" ]]
then
echo "Internet is down"
else
echo "Internet is up"
fi
#!/bin/bash
# get current wifi device
CURRENT_DEVICE=$(networksetup -listallhardwareports | awk '$3=="Wi-Fi" {getline; print $2}')
echo "Current Wi-Fi Device = '$CURRENT_DEVICE'"
# turn off wifi
networksetup -setairportpower $CURRENT_DEVICE off
#!/bin/bash
# get current wifi device
CURRENT_DEVICE=$(networksetup -listallhardwareports | awk '$3=="Wi-Fi" {getline; print $2}')
echo "Current Wi-Fi Device = '$CURRENT_DEVICE'"
# turn on wifi
networksetup -setairportpower $CURRENT_DEVICE on
#!/bin/bash
# get current wifi device
CURRENT_DEVICE=$(networksetup -listallhardwareports | awk '$3=="Wi-Fi" {getline; print $2}')
echo "Current Wi-Fi Device = '$CURRENT_DEVICE'"
echo "Switching off wifi."
# turn off wifi
networksetup -setairportpower $CURRENT_DEVICE off
echo "Switching on wifi."
# turn on wifi
networksetup -setairportpower $CURRENT_DEVICE on
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment