Bash Script: Check Connectivity

#!/bin/bash -x
while [ $limiter -lt 10 ];do
chek=$(ping -c 1 | awk '/data/ {print $7}')
if [ "$chek" = "data." ]; then
play /home/your-pc/hack/snd/Alarm.wav
exit 0
play /home/your-pc/hack/snd/gitar.wav
let limiter+=1

This simple bash script was made to check your current connectivity. It ping and play sounds until it meet a few conditions to stop.

  • First condition is when the string  $chek contains “data.” , I’ll explain it later.
  • Second condition is when the $limiter reach 9

First, we assign 0 to a variable called limiter .Then..

while [ $limiter -lt 10]; do

In this line, we set a loop using with a condition. the “-lt” means lesser than or < . So when the $limiter value is 10, it then stop the looping.

chek=$(ping -c 1 | awk '/data/ {print $7}')

This is how you assign a variable’s value from output of commands. ping -c 1 will do a ping to one time. The output of ping will be used in awk ‘/data/ {print $7}‘  AWK is a programming language that is designed for processing text-based data.

For example: if the connectivity is available the ping output will be.

PING ( 56(84) bytes of data.

and if the connectivity is unavailable, in some relatively-normal-case it just show this.

PING: unknown host

The awk line is awk ‘/data/ {print $7}’. It means it will find the line containing word “data”, and then print the 7th column of the line. If connectivity is available the seventh line is “data.” (notes: the first column is “PING”).  And that is why in the next line of the script it will do comparison to check wether the string $chek is “data.”. This means, if connectivity is unavalaible the ping output would be as shown above then $chek does not contain “data.” or empty ” “.

if [ "$chek" = "data." ]; then
play /home/your-pc/hack/snd/Alarm.wav
exit 0
play /home/your-pc/hack/snd/gitar.wav
let limiter+=1

We are ready to check wether we should hear Alarm sounds or the gitar sounds.🙂
I used the play tool to play the sounds. My friend said that it only a python script that runs some other scripts or library to play sounds. Most linux distributions has it pre-installed (He guess). So if you use ubuntu 9.10 as same as I use, this will be okay. Let’s proceed! (wait, what? windows? go search for batch tutorial)

As you can see, in line 01 the script does the comparison wether the $chek is contain “data.” or empty ” “.  If it is contain “data.” then it will start the play commands, and start to play the sounds found in /home/your-pc/hack/snd/Alarm.wav .  After it done play the sound, then it will simply stop the loop and finish the script.

But, when $chek does not contain “data.” or empty ” ” this means you have no connectivity, and it will play the sounds found in /home/your-pc/hack/snd/gitar.wav .It’s not over yet, after done play the sounds,

in line 06 it will increment the $limiter values to 1–>

then finish the if  –> check wether the $limiter is now less than 10 (wich means 9)–> oh no it is not yet 9! –>

do the ping again –> assign the new values for $chek –> do the rest code all over again. –>

UNTIL, the $limiter finally reach nine and then finish the loop and stop the script. Why? Because, maybe in some case it is not about losing connectivity temporary. Without the while looping the script will play the sounds until it finally connected (wich we might sure,that it would be for a long-long time). And that can be iritating, but yes you can do the Ctrl-C to terminate the script if you run this directly from the terminal.

(what? you run this script from terminal?? I don’t want to see those scary blinking cursor with black and white text around it!)

okay, I’ll documenting once again how you can run this script simply from your top panel bar and run it with just a click.🙂

So you can do other things more useful than hitting reload button all over again :p, while listening to smooth sound of looping guitar pattern.

Oh! This, maybe you interested to another bash script implementation for the good use of linux. My other friend share it here.

AND IF YOU THINK YOU ARE HARDCORE PROGRAMMER, WICH APPARENTLY LAUGHING AT MY SCRIPT>>> Please..pleasee..pleaseeee… give me another algorithm that probably more simple than my stupid script…please…

2 thoughts on “Bash Script: Check Connectivity

  1. Can you help me make a simple script that I can click on and have run in Ubuntu?
    All I need it to do is ” v4l2-ctl -i 2 ” I need this to run in Terminal. This sets the input of the tv card in my ubuntu computer. Right now I have to manually open Terminal, type in this command, and it sets the tv card. (I have to do it every time, which is a pain.) Then I launch the tv player to watch tv on Ubuntu. All of the things I see online are for making a command to launch or execute a program, I just need it to run the above command. Thanks!

    1. NT, you can make a launcher for that. Just right click on your desktop and click create launcher. You’ll see the “create launcher” window, put your command there and DON’T FORGET TO CHANGE THE TYPE to “RUN IN TERMINAL”. You can then run the command by clicking the launcher on your desktop. You can get it in the launcher panel too. (see here: ).

      Oh and if you’re still using old ubuntu, perhaps this can help you
      I made that post two years ago.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s