Created
April 12, 2016 22:51
-
-
Save ericoporto/3f6c3c5dff0c4ead73845f269ab80c02 to your computer and use it in GitHub Desktop.
Let your computer tell you when your internet is back on. Bash script that requires cron and espeak.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Requires: espeak, crontab, mktemp. | |
# | |
# Script for checking if this computer is connected to internet. | |
# If you copy it to $installdir, give this script execution permission. | |
# And remember to set proper ownership (using chown). | |
# Use Example: | |
# | |
# sudo chmod +x checkinternet.sh | |
# sudo cp checkinternet.sh /usr/local/sbin/ | |
# sudo chown user:group /usr/local/sbin/checkinternet.sh | |
# checkinternet.sh --install | |
# | |
# Warning: this script was made using `man`, because I had no Internet. | |
# | |
testaddress="8.8.8.8" | |
thisfilename="checkinternet.sh" | |
installdir="/usr/local/sbin" | |
usage() { | |
if [ "$1" = -h ] | |
then | |
exec 1>&2 | |
echo -n U | |
else | |
echo -n u | |
fi | |
echo "sage: `basename $0` [ --install | --uninstall | --statefile name | --check ] [ -h, --help ]" | |
if [ "$1" = -h ] | |
then | |
cat <<EOF | |
Options: --install add cron entry for check internet each 5 min. | |
--uninstall remove cron entries from this script. | |
-h, --help print this help and exit | |
--statefile name give a file for check and save state. | |
--check check for internet connection now. | |
EOF | |
exit 0 | |
else | |
exit 1 | |
fi | |
} | |
if [ "$#" -eq 0 ] | |
then | |
usage | |
fi | |
while [ $# -ne 0 ] | |
do | |
case "$1" in | |
-v|--version) | |
echo "`basename $0` version 1.0" \ | |
"by Erico Porto <http://github.com/ericoporto>" | |
exit 0 | |
;; | |
-h|--help) | |
usage -h | |
;; | |
--statefile) | |
test $# -gt 1 || usage | |
tempbool="$2" | |
shouldcheck=1 | |
shift | |
;; | |
--install) | |
install=1 | |
shouldcheck=0 | |
;; | |
--uninstall) | |
uninstall=1 | |
shouldcheck=0 | |
;; | |
--check) | |
shouldcheck=1 | |
;; | |
*) | |
usage | |
;; | |
esac | |
shift | |
done | |
if [[ $uninstall ]] ; then | |
crontab -l | while read -r; do | |
[[ $REPLY = *$thisfilename* ]] && continue | |
printf '%s\n' "$REPLY" | |
done | crontab - | |
echo " " | |
echo "removed entries from cron" | |
echo " " | |
exit | |
else | |
if [[ $install ]] ; then | |
tmpbool=`mktemp -t hasinternet.XXX` | |
echo "0" > $tmpbool | |
#let's make a new crontab | |
crontab -l | while read -r; do | |
[[ $REPLY = *$thisfilename* ]] && continue | |
printf '%s\n' "$REPLY" | |
done | crontab - | |
newcronfile=`mktemp -t new.cronfile.XXX` | |
crontab -l > $newcronfile | |
printf "\n...adding to cron...\n" | |
#echo new cron into cron file | |
echo "*/5 * * * * $installdir/$thisfilename --statefile $tmpbool" >> $newcronfile | |
#install new cron file | |
crontab $newcronfile | |
#remove new cron file | |
rm $newcronfile | |
#say success | |
printf "\nInstalled!\nkeep this computer on.\n\n" | |
exit | |
fi | |
fi | |
if [[ $shouldcheck -eq 1 ]]; | |
then | |
hasntpinged=`ping -c 3 -W 3 $testaddress | grep \ 0\ received` | |
if [[ $tempbool ]]; then | |
isbooltrue=`cat $tempbool | grep 1` | |
isboolfalse=`cat $tempbool | grep 0` | |
if [[ $hasntpinged ]]; then | |
if [[ $isbooltrue ]]; then | |
echo "didn't pinged" | |
espeak "internet connection lost." | |
echo "0" > $tempbool | |
fi | |
else | |
if [[ $isboolfalse ]]; then | |
echo "has pinged" | |
espeak "connected to internet!" | |
echo "1" > $tempbool | |
fi | |
fi | |
else | |
if [[ $hasntpinged ]]; then | |
echo "didn't pinged" | |
espeak "no internet here." | |
else | |
echo "has pinged" | |
espeak "we have internet!" | |
fi | |
fi | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment