Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

What software is there that will play an alert (PC speaker) when there isn't any internet connectivity for 5 minutes?

My switch/router seems to disconnect every few days, and I want to reset it when it happens.

PC          -- TP-Link switch/router -- FO
192.168.x.1 -- 192.168.x.2 / x.y.z.a -- a.b.c.d
share|improve this question
3  
Not to point out the obvious, but you're taking a very roundabout solution to the problem of a faulty switch/router –  Michael Mrozek Jun 9 at 2:08
add comment

2 Answers 2

up vote 6 down vote accepted

You can use a modified version of this script to do what you want:

#!/bin/bash

downTime=0
lastAccessTime=$(date +"%s")
while [ true ]; do
if ! ping -c1 google.com >& /dev/null; then
    downTime=$(( $(date +"%s") - $lastAccessTime ))
else
    downTime=0
    lastAccessTime=$(date +"%s")
fi

sleep 15

if [ $downTime -ge 300 ]; then
   echo "alert"
fi
done

We're "CONNECTED" Example

With debugging turned on so you can see what the script's doing.

set -x

Running with a valid hostname to demonstrate the "connection is up" state.

$ ./watcher.bash
+ downTime=0
++ date +%s
+ lastAccessTime=1402276955
+ '[' true ']'

The above initializes a couple of variables and determines the last time we went through the loop, $lastAccessTime. We now try to ping Google.

+ ping -c1 google.com
+ downTime=0
++ date +%s
+ lastAccessTime=1402276955

We now calculate any down time, $downTime, if ping fails, otherwise, we reset $downTime to zero, and recalculate $lastAccessTime.

+ sleep 15

Now we wait 15 seconds.

+ '[' 0 -ge 300 ']'

Now we check if we've been down for > 5 minutes (300 seconds). Then we repeat going through the while loop.

+ '[' true ']'
+ ping -c1 google.com
+ downTime=0
++ date +%s
+ lastAccessTime=1402276970
+ sleep 15
....

As long as we're up, nothing will happen other than we check with the ping command every 15 seconds.

We're "DISCONNECTED" Example

Now to simulate a "connection is down" state, we'll swap out the hostname we're pinging and use a fake one, google1234567890.com. Repeating a run of our script with debugging enabled we now see some actual down time getting calculated.

$ ./watcher.bash
+ downTime=0
++ date +%s
+ lastAccessTime=1402277506
+ '[' true ']'
+ ping -c1 google1234567890.com
++ date +%s
+ downTime=0
+ sleep 15
+ '[' 0 -ge 300 ']'
+ '[' true ']'
+ ping -c1 google1234567890.com
++ date +%s
+ downTime=15
+ sleep 15
...

Notice above that $downTime is equal to 15 seconds so far. If we wait a while longer we'll see this:

+ '[' true ']'
+ ping -c1 google1234567890.com
++ date +%s
+ downTime=300
+ sleep 15

We've accrued 300 seconds of down time. So now when we check, we print the message, alert.

+ '[' 300 -ge 300 ']'
+ echo alert
alert
+ '[' true ']'
+ ping -c1 google1234567890.com
++ date +%s
+ downTime=315
+ sleep 15

This state will continue until the connection is restored and the ping is once again successful.

So what about a sound?

That's easy. You can use a variety of tools to do this. I would use something like sox or mplayer to play an audio file such as an .mp3 or .wav file with an appropriate sound you want to hear every 15 seconds, while the connection is down.

mplayer someaudio.wav

Simply replace the alert message above with this line to get audio feedback that the connection is down.

Timing out issues with ping

If you use ping in the manner above you'll likely encounter a slow lag time where it takes ping literally 10-20 seconds for it to fail when the connection is down. See my answer to this U&L Q&A titled: How to redirect the output of any command? for an example using the command line tool fing instead. This tool will fail more quickly than the traditional ping.

share|improve this answer
    
Btw I'm using beep command, is it safe to chown /dev/tty0 so I could use it without sudo? –  Kokizzu Jun 9 at 2:45
1  
@Kokizzu - no you shouldn't be changing the ownership on /dev/tty0. –  slm Jun 9 at 11:21
1  
Hooray, sox! @Kokizzu - slm's right about leaving the permissions on the /dev/tty0 device alone, but you could get a pseudo-terminal /dev/pts/[num] device - the same kind of character device that xterm uses for each window, for example - with something like screen or tmux or even ssh. These you can more safely control as they're only slave devices to the master /dev/ptmx device anyway and won't cause the same kind of problems as, say, a user-owned /bin/login process might - which is what could happen in the other case. –  mikeserv Jun 9 at 23:46
add comment

if you are using Linux you can use ping within a script to google.com for example:

counter=0
while [ true ]
do
ping -c 1 google.com > /dev/null 2>&1
if [ $? -ne 0 ] 
then
    let "counter +=1"
else
    let "counter = 0"
fi
if [ $counter -eq 300 ] # we assume that one ping need one second (300 is 5 minutes)
then
   echo "alert"
fi
done
share|improve this answer
2  
This will work, but you might want to consider using fing instead of ping. The ping cmd. can take a bit to fail when it isn't successful. The fing command on the otherhand will fail within 1 second. See my A to this Q&A for more: How to redirect the output of any command?. I show both using ping and fing. –  slm Jun 9 at 0:47
    
@slm, Thanks for your editing, I was confused how to make ping last only for one second –  Networker Jun 9 at 5:57
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.