Last active
January 24, 2018 05:03
-
-
Save pabloab/de6da7230a0c7d84674994b0007f5231 to your computer and use it in GitHub Desktop.
[Online tester] Test if online. A nice guitar pluck sound loud when comes back
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 | |
set -euo pipefail # unofficial bash strict mode: http://redsymbol.net/articles/unofficial-bash-strict-mode/ | |
IFS=$'\n\t' | |
# By Pablo Bianchi (pablo.bianchi@gmail.com) | |
# Infinit loop testing if we are onliene. When we are start beeping. | |
# (based on http://www.linuxscrew.com/2009/04/02/tiny-bash-scripts-check-internet-connection-availability/ ) | |
#~ USAGE: Just: ./testInternet.sh | |
#~ NOTES | |
#~ Consider http_proxy and https_proxy already setted up | |
hash play 2>&- || { echo >&2 "I require «play» but it's not installed. Try apt-get install sox. Aborting."; exit 1; } | |
#~ ToDo: | |
#~ LOG, example: /bin/echo $(/bin/date +%Y-%m-%d) $(/usr/bin/uptime) | |
#~ take a look to http://www.linuxito.com.ar/gnu-linux-2/nivel-medio/164-script-para-monitorear-un-enlace | |
#~ take a look to: wget con --load-cookies file | |
#~ use alternative nm-online o httping | |
#~ OPTIONS | |
WGET="`which wget`" | |
URL="https://www.google.com.ar/" | |
# URL="http://www.mercadolibre.com.ar/" | |
TIMEOUT=6 | |
if [ $# -eq 0 ]; then | |
delay=3; | |
else | |
delay="$1"; | |
fi | |
noConnectionMessage="No connection, trying again in $delay seconds..."; | |
connectionMessage="WE HAVE INTERNET! :) Trying again in $delay seconds..."; | |
echo "INTERNET TESTER: Type Ctrl+C to quit"; | |
echo | |
rm --force /tmp/index.site | |
function playSound { | |
time=1; # Time played | |
if [ `sox --version| grep -oP "v[0-9.]+" | sed "s/[v.]//g"` -lt 1431 ]; then #analize sox version, if greater than v14.3.1 can use pluck | |
play -q -n synth $time sine; | |
else | |
play -q -n synth $time pluck $1; | |
#for i in G4 G4 G4 E4;do play -n synth 0.1 pluck $i repeat 2;done # You can play with something like this also :) (first three notes from Beethoven 5th symphony) | |
fi | |
} | |
while [ 1 -eq 1 ]; do | |
# $WGET -q --tries=10 --timeout=2 $URL -O /tmp/index.site &> /dev/null || echo "Warning: wget return status code $?" | |
$WGET -q --tries=10 --timeout=$TIMEOUT $URL -O /tmp/index.site &> /dev/null || echo -e "wget status code $?." | |
if [ ! -s /tmp/index.site ];then | |
echo $noConnectionMessage; | |
#playSound E2 | |
sleep 1; | |
else | |
#~ zenity --warning --text "ADDRESS is back" # Also could use xmessage | |
# notify-send -i "notification-network-wireless-full" "Connection state:" "$connectionMessage"; | |
notify-send -u critical "Connection state:" "$connectionMessage"; | |
echo $connectionMessage; | |
playSound E3 | |
fi | |
sleep $delay; | |
done | |
exit 0; | |
#~ Possible not-obvious reasons for not having connection: | |
#~ - Invalid MTU: Try ping -s 1500 -c1 yahoo.com lowing from 1500 to what start working. http://swik.net/Ubuntu/Only+Ubuntu/How+to+Optimize+your+Internet+Connection+using+MTU+and+RWIN/cbnda | |
#~ - DNSs problems: Try flushing DNSs, using Google or OpenDNS servers | |
#~ Ubuntu 11.10 and above doesnt cache names. To investigate: dig -t a ns1.myhostingcompany.com @domain_registrar_dns_server | |
#~ If you wanted to refresh your settings you could disable and then enable networking or run: sudo service network-manager restart | |
#~ `which wget`-q --tries=10 --timeout=2 http://www.google.com -O /tmp/index.site &> /dev/null | |
#~ if [ ! -s /tmp/index.site ];then echo -e "$(date +%F\ %T)\tOFFLINE"; else echo -e "`date '+%Y%m%d'`\tONLINE"; fi | |
#~ echo "$(date +%F\ %T) - Failed check - $ERROR" &>> $LOG | |
#~ Resatarting service if offline | |
#~ Red Hat/Fedora/CentOS: service network restart | |
#~ Debian/Ubuntu: service networking restart | |
# de http://forums.opensuse.org/archives/sf-archives/archives-programming-scripting/345669-check-internet-connection-script.html | |
#if ping -c 3 some.router.at.isp.com | |
#then | |
# echo 'Have connectivity' | |
#fi | |
#~ Pinging default gateway: ping -q -w 1 -c 1 `ip r | grep default | cut -d ' ' -f 3` > /dev/null && echo ok || echo error | |
#~ SIMPLER | |
#~ #!/bin/bash | |
#~ wget -q --tries=10 --timeout=20 http://google.com | |
#~ if [[ $? -eq 0 ]]; then | |
#~ echo "Online" | |
#~ else | |
#~ echo "Offline" | |
#~ fi | |
This is the list of exit codes for wget: | |
0 No problems occurred | |
1 Generic error code | |
2 Parse error — for instance, when parsing command-line options, the .wgetrc or .netrc… | |
3 File I/O error | |
4 Network failure | |
5 SSL verification failure | |
6 Username/password authentication failure | |
7 Protocol errors | |
8 Server issued an error response |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment