LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
 
Search this Thread
Old 07-07-2004, 08:38 PM   #1
default
LQ Newbie
 
Registered: Jul 2004
Location: Sydney Australia
Distribution: Fedora Core
Posts: 4

Rep: Reputation: 0
Bash script help for dummy. need to monitor and restore my adsl connection


Hi

Can someone please help me here, I administer a couple of small firewalls that are on an adsl connection but they are about 100kms away from me.
When the connection goes down I have no way of bringing it back up.

I assume it will only need a simple bash type script to do the following things.

check the connection is up
if connection is up, is it active (not timed out or frozen) (perhaps ping to another server)
if active then exit
if not active then clear existing connection and start new connection

I could then put this in a cron job about every 5 or 10 mins

Please forgive me as I have no programming Knowledge.

If anyone has the time to help out it would be greatly appreciated.

Dave
 
Old 07-07-2004, 09:48 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,020
Blog Entries: 11

Rep: Reputation: 907Reputation: 907Reputation: 907Reputation: 907Reputation: 907Reputation: 907Reputation: 907Reputation: 907
Code:
#!/bin/bash
dump=`ping -c 1 www.your_favourite`
if [ ! $? == 0 ]; then 
    echo "Ooops - hang up and reconnect"
else
    echo "All is well"
fi
Tackle it from there :)

Just replace the echo with the Ooops with whatever
script you run to connect the modem to the ISP.



Cheers,
Tink
 
Old 07-07-2004, 09:54 PM   #3
default
LQ Newbie
 
Registered: Jul 2004
Location: Sydney Australia
Distribution: Fedora Core
Posts: 4

Original Poster
Rep: Reputation: 0
Thanks Tinkster,

I shall Give it a "bash"

I didn't realise it would be that simple.
 
Old 07-07-2004, 09:57 PM   #4
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,020
Blog Entries: 11

Rep: Reputation: 907Reputation: 907Reputation: 907Reputation: 907Reputation: 907Reputation: 907Reputation: 907Reputation: 907
Pleasure mate, and regards to the Ruh's :}


Cheers,
Tink
 
Old 07-07-2004, 11:14 PM   #5
default
LQ Newbie
 
Registered: Jul 2004
Location: Sydney Australia
Distribution: Fedora Core
Posts: 4

Original Poster
Rep: Reputation: 0
Just one more thing if you happen to read this thread again,

Can you quickly explain what these actually do:

!
$
?

The rest of it speaks pretty clearly and is easy to understand.

I have started reading up on bash scripting but needed this script much sooner than I could have figured it out.

Thanks again for your promt reply

Dave
 
Old 07-08-2004, 02:10 AM   #6
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,020
Blog Entries: 11

Rep: Reputation: 907Reputation: 907Reputation: 907Reputation: 907Reputation: 907Reputation: 907Reputation: 907Reputation: 907
The unary ! negates the following statement.
Read NOT( $? == 0 )
$? is a bash-variable, and contains the exit
status of the previous command, in our case
of the ping. If the status of a command is
success $? will contain 0, otherwise a non-zero
value.

In human-readable form:

if the previous command was unsuccessful, say ooops ,
otherwise all is well.


http://tldp.org/LDP/abs/abs-guide.html.tar.gz is a pretty good
reference for bash-programmers,

This one http://tldp.org/LDP/Bash-Beginners-G...de.html.tar.gz
I haven't read, but I like her Linux-noob guide :) and assume
that she's done a similarly good job on this one as well.



Cheers,
Tink
 
Old 07-08-2004, 06:40 PM   #7
default
LQ Newbie
 
Registered: Jul 2004
Location: Sydney Australia
Distribution: Fedora Core
Posts: 4

Original Poster
Rep: Reputation: 0
Thanks Again Tinkster,

Your help and explanation is a great inspiration.
I will indeed check those links from your last post.
I put that script in place last night and tested it.
All went well, But I just have to add the command to bring the vpn back up as I realized this morning that it dies with the connection. That is quite simple though.

The thing that had me stumped in the begining was gathering the results of the ping and using the answer to decide whether to exit or execute the restart.

Now that I have a working script I can study up my bash scripting and make it a bit more fancy later on.When I do I
will post the results up for others to use.

Kindest Regards,

Dave
 
Old 07-08-2004, 07:32 PM   #8
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,020
Blog Entries: 11

Rep: Reputation: 907Reputation: 907Reputation: 907Reputation: 907Reputation: 907Reputation: 907Reputation: 907Reputation: 907
Quote:
Originally posted by default
Thanks Again Tinkster,
Again, my pleasure :)


Quote:
The thing that had me stumped in the begining
was gathering the results of the ping and using
the answer to decide whether to exit or execute the restart.
Note that
Code:
dump=`ping -c 1 www.your_favourite`
isn't meant to gather anything, it's to ping-probe, and the
assignment to dump only prevents ping-output, it's a
beautifying measure, so to speak :} ... the $? is the key
to analysing the result.

Quote:
Now that I have a working script I can study up my
bash scripting and make it a bit more fancy later on.
When I do I will post the results up for others to use.

Kindest Regards,
Dave
All I can say is that I'm glad I got you started, and that
I'm very happy to see you enthusiastic about scripting.

Can't wait to see the result! :}


Cheers,
Tink
 
Old 07-09-2004, 10:59 AM   #9
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,540

Rep: Reputation: 370Reputation: 370Reputation: 370Reputation: 370
My variant:

Code:
#!/bin/bash

if ping -c 1 www.your_favourite >/dev/null
then 
    echo "All is well"
else
    echo "Ooops - hang up and reconnect"
fi
It saves you the questions about ! $ ? ....
 
Old 07-09-2004, 03:41 PM   #10
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,020
Blog Entries: 11

Rep: Reputation: 907Reputation: 907Reputation: 907Reputation: 907Reputation: 907Reputation: 907Reputation: 907Reputation: 907
Thanks for pointing that variant out :)

I guess it's a matter of taste, I find my variant
more self-documenting...


Cheers,
Tink
 
Old 07-09-2004, 04:10 PM   #11
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,540

Rep: Reputation: 370Reputation: 370Reputation: 370Reputation: 370
Well, with no offense, I disagree and think yours is slightly obfuscated.

First, you affect a variable you never use while the real goal is to hide output:
Code:
>/dev/null
is the standard way for that goal.

Second, you use again a variable ($?) you could have avoided too by using the command itself in site like I do.

Third, your test has one superfluous level of complexity:

Code:
if [ ! $? == 0 ]
can be simplified to:
Code:
if [ $? != 0 ]
or even:
Code:
if [ $? = 0 ]
and swapping the clauses.

That said, your code was anyway perfectly doing its job, I'm just discussing cosmetics
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
perl/bash script to monitor all processes running in my machine pudhiyavan Linux - Security 4 07-19-2005 02:09 PM
Samba and Windows XP restore connection problem SirJohnWackness Linux - Networking 1 12-17-2004 07:16 AM
send automatic input to a script called by another script in bash programming jorgecab Programming 2 04-01-2004 12:20 AM
How to focus / restore / min / max windows with bash ? peb Linux - Newbie 2 02-29-2004 01:21 AM
How to restore a lost connection??? grobbekuiken Linux - Newbie 0 03-31-2003 01:31 AM


All times are GMT -5. The time now is 11:37 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration