LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-15-2008, 05:57 AM   #1
Springs
Member
 
Registered: Apr 2008
Posts: 59

Rep: Reputation: 0
bash scripting


hi all.. im totally new to Linux and im needing to learn it for my new job.. ive been given the task of writing a bash script that will ping a particular IP address and report if the ping was successfully or not and if not email an error message to a particular address.

ive been looking around at loads of bash tutorials and reading what they say.. ive been told the best way is to load it all into a grep command and do it via that.. only problem is that i totally suck atm at programming and dont really no where to start. i can get the script to ping for a count of 10 to an ip address and load that into a file etc or just print it on screen but thats about it

any ideas?
 
Old 04-15-2008, 06:17 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
You don't have to read tons of bash tutorials: just focus on one or two and start to exercise. Here is some hints:
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://rute.2038bug.com/index.html.gz
and when you are a little more experienced, you can go through the Advanced Bash Scripting Guide
http://www.tldp.org/LDP/abs/html.

Regarding your issue you can simply use ping in a test construct. Every command in linux has an exit status whose value is usually 0 if the command is successfull, 1 otherwise. Other values are specific to the application. So you can simply do something like this
Code:
#!/bin/bash
if ! ping -c 2 192.168.0.1 > /dev/null
then
    #
    #  commands to send e-mail notification here
    #
fi
The expression evaluated by the if/then construct in this case is the exit status of the ping command. If the IP address is pingable the exit code is 0 and the expression is true, otherwise the exit code is 1 and the expression is false. I added a negation operator (the exclamation mark before the ping command) since you want e-mail notification if ping does not succeed. I also redirect the ping output to /dev/null (think it as a black hole) since you're not really intersted in the ping statistics.
 
Old 04-15-2008, 06:34 AM   #3
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
welcome to LQ.

That seems like a strange assignment when starting a new job--are you sure it is not homework?

To detect if ping has succeeded, you can evaluate the exit code ($?) (Look at "man ping" for the possible codes.) You would then test the exit code and decide what to do next (eg with an "if" statement). Also, use ping with the -c option so it doesn't run forever.

To learn Bash, you will want to use a tutorial (eg Bash Guide for Beginners--free at tldp.org) and try things as you go.
 
Old 04-15-2008, 08:07 AM   #4
Springs
Member
 
Registered: Apr 2008
Posts: 59

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by pixellany View Post
welcome to LQ.
That seems like a strange assignment when starting a new job--are you sure it is not homework?
well its to do while one of the guys is away.. he asked me to do it he comes back tomorrow.. ive been reading the net for the last couple of days to understand the bash system
 
Old 04-15-2008, 09:02 AM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Quote:
Originally Posted by pixellany View Post
(No, they do exactly the same thing)
Not really. The back ticks can be used in the Bourne Shell too, so they improve portability. Also they permit nesting and if I'm not wrong they treat double backslash \\ in a different way. Anyway, for common usage they do the same thing.

@ Springs: have you tried some of the suggestions given? Feel free to post your script for advices.

PS: I have reported this thread for splitting, since answers to two different questions are crossing.
 
Old 04-17-2008, 04:29 AM   #6
Springs
Member
 
Registered: Apr 2008
Posts: 59

Original Poster
Rep: Reputation: 0
cool mines working now and ive got it to email when it doesnt respond. got another question for this. i was it to ping it at least 5 times over say a 30 mins period and if it doesnt respond after the 5th time then email it.
 
Old 04-17-2008, 06:10 AM   #7
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
You can go through other options of the command ping (see man ping for details). You can specify the number of ping requests and the interval between each request:
Code:
ping -c 5 -i 450 192.168.0.1
This will do the first ping immediately, then wait 450 seconds between successive pings. After 30 minutes it will send the last ping then stop. However, I don't know which will be the exit code if the first pings are lost and only the last ones are received. Also, with this approach your script will always wait 30 minutes before proceeding.

An alternative can be using the sleep command and do successive single ping requests until the desired count is reached:
Code:
#!/bin/bash
count=1
status=1
#
#  execute the following command until 5 ping requests have been sent
#  and while the ping has not been received
#
while [ $count -le 5 -a $status -ne 0 ]
do
    ping -c 1 192.168.0.1 > /dev/null
    status=$?
    #
    #  if exit status is 1 sleep, otherwise proceed
    #
    test $status -eq 1 && sleep 450
    count=$((count + 1))
done
 
Old 04-17-2008, 06:14 AM   #8
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,331
Blog Entries: 55

Rep: Reputation: 3530Reputation: 3530Reputation: 3530Reputation: 3530Reputation: 3530Reputation: 3530Reputation: 3530Reputation: 3530Reputation: 3530Reputation: 3530Reputation: 3530
// I think he should read the posted BaSH guides and be allowed to provide some pseudo script to show he at least understands the basic structures. Else if he's asked to modify the script later on he hasn't learned a thing.
 
  


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
bash scripting csdhiman Linux - Server 3 10-12-2007 01:15 AM
Bash scripting saje Linux - Newbie 4 08-22-2007 09:03 PM
bash scripting vadon Linux - Newbie 6 05-10-2005 05:07 AM
need help with bash scripting rich2oo1 Programming 2 12-17-2003 01:50 PM
About bash scripting pazvant Programming 3 10-20-2003 12:12 PM


All times are GMT -5. The time now is 09:11 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