Ping script using Redhat and BAsh


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Ping script using Redhat and BAsh
# 1  
Old 06-12-2003
Ping script using Redhat and BAsh

Alright, I have being checking out various posting here trying to hack together something for a friend

Needed:

A script that can run in cron doing:
ping of several hosts and notifying via email when they are unavailable.

I am not going to post my non working messes (I am a basic scripter), but I have tried several approaches and just dont have the strong knowledge base to push variables around in a script.

Suggestions Please..

using: bash on redhat linux box

actually here is the latest script:
#!/bin/bsh
#################################
for file in `cat iplist.txt`
do
ping -c 1 -A $file | grep errors | wc -l
read tom
echo $tom
if [ "$tom" != 0 ]
then
mail -s"Test" exx@xx.com
else
mail -s"Worked" exx@xx.com
fi
done

OH how the heck do I get the mail program to just fire out the email? I know in solaris mailx will nicely shoot out the email...

Also my variations of this produce nice frustrating results...like the if then aint working right. Figure read is seen it as a string? At least that is my guess...

Last edited by edkung; 06-12-2003 at 12:34 PM..
# 2  
Old 06-13-2003
Re: Ping script using Redhat and BAsh

Quote:
Originally posted by edkung
Needed:

A script that can run in cron doing:
ping of several hosts and notifying via email when they are unavailable.

using: bash on redhat linux box

actually here is the latest script:
#!/bin/bsh
#################################
for file in `cat iplist.txt`
do
ping -c 1 -A $file | grep errors | wc -l
read tom
echo $tom
if [ "$tom" != 0 ]
then
mail -s"Test" exx@xx.com
else
mail -s"Worked" exx@xx.com
fi
done
For starters, make sure that iplist.txt is in the correct directory. I assume it will always be in the same directory as the script? If not, add a path to the file name. Second, for the mail portion, try using sendmail as this is commonly installed on RedHat. To check the status of the ping command, use a variable to capture the status. Not sure what the return value is of that ping command, adjust the IF statement to match.

for file in `cat iplist.txt`
do
PING_TEST=0
PING_TEST=`ping -c 1 -A $file | grep errors | wc -l`
#read tom
#echo $tom
if [ $PING_TEST -ne 0 ]
#Use -ne (not equal) for integer comparison
then
sendmail -s"Test" exx@xx.com
else
sendmail -s"Worked" exx@xx.com
fi
done

Last edited by google; 06-13-2003 at 08:08 AM..
# 3  
Old 06-13-2003
Thanks, that worked much better. Getting both the positive and negative results I originally wanted. I have to work on the sendmail part (get the right switches).

Now here is another question

Lets say in my iplist.txt file I want to include a name for the ip address
ex: 192.168.60.5,router1

and I want to have the email subject to say

Router1 is down!

I am thinking that any of the text editors will help with this...(sed awk or what have you)
# 4  
Old 06-13-2003
Using Oombera's suggestion on another thread....
echo "1024MB" | awk '{print substr($1,0,4)}' # print first four numbers / chars
echo "1024MB" | sed 's/[A-Za-z]*//g' # removes all letters
x=${x%%[A-Za-z]*} # remove everything after (and including) any chars - Note this is specific to Korn Shell

setup your variables in you iplist.txt so that they are in a consistent format. Then, instead of using $FILE, use one of the above suggestions to get either the IP address or the actual name. You will of course have to make some modifications. The code above will remove the characters from the variables. You will need to remove numbers and dots to reverse this.

Do a google search on SendMail Flags to get correct formatting.

Last edited by google; 06-13-2003 at 10:38 AM..
# 5  
Old 06-13-2003
Or you could split the variable up right away and store both parts in separate variables:

for file in `cat iplist.txt`
do
ipAdd=`echo $file | awk -F, '{print $1}'
failMsg=`echo $file | awk -F, '{print $2}'
PING_TEST=0
PING_TEST=`ping -c 1 -A $ipAdd | grep errors | wc -l`
#read tom
#echo $tom
if [ $PING_TEST -ne 0 ]
#Use -ne (not equal) for integer comparison
then
sendmail -s"$failMsg is down!" exx@xx.com
else
sendmail -s"Worked" exx@xx.com
fi
done
# 6  
Old 06-13-2003
doesnt work...the ipAdd variable isnt properly being populated.
I have tried checking the ' and " and no go...
# 7  
Old 06-13-2003
Oh, shoot, I forgot to close the two lines with another `

ipAdd=`echo $file | awk -F, '{print $1}'`
failMsg=`echo $file | awk -F, '{print $2}'`
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to block first bash script until second bash script script launches web server/site?

I'm new to utilities like socat and netcat and I'm not clear if they will do what I need. I have a "compileDeployStartWebServer.sh" script and a "StartBrowser.sh" script that are started by emacs/elisp at the same time in two different processes. I'm using Cygwin bash on Windows 10. My... (3 Replies)
Discussion started by: siegfried
3 Replies

2. Programming

Ping test sends mail when ping fails

help with bash script! im am working on this script to make sure my server will stay online, so i made this script.. HOSTS="192.168.138.155" COUNT=4 pingtest(){ for myhost in "$@" do ping -c "$COUNT" "$myhost" &&return 1 done return 0 } if pingtest $HOSTS #100% failed... (4 Replies)
Discussion started by: mort3924
4 Replies

3. Shell Programming and Scripting

ping script

hello fellows, I need help with a script, I'm using this one HOSTS="192.168.10.9 192.168.10.15 " SUBJECT="Attention - Gateway San Martin DOWN " COUNT=1 EMAILID="lmail@mydomain.com" for myHost in $HOSTS do count=$(ping -c $COUNT $myHost | grep 'received' | awk -F',' '{ print $2... (2 Replies)
Discussion started by: lucas.fradusco
2 Replies

4. Shell Programming and Scripting

ping with timeout bash

Hi experts: I want to do below thing with bash if ping 192.168.0.1 sucussful then do somthing without delay, if ping failed within 20s, then kill ping and exit not sure this can use "alarm" to do this. any code will be welcome Thanks (1 Reply)
Discussion started by: yanglei_fage
1 Replies

5. Shell Programming and Scripting

Need bash script to ping the servers and rename the output file each time the script is ran

HI, I have a file serverlist in that all host names are placed. i have written a small script #./testping #! /bin/bash for i in `cat serverlist` do ping $i >> output.txt done so now it creates a file output.txt till here fine.. now each time i run this script the output file... (4 Replies)
Discussion started by: madhudeva
4 Replies

6. Shell Programming and Scripting

Using ping in script

Hi. I have a server with multiple network ports that need to be tested to a list of destinations. I'm trying to write a scripts to automate this but can't seem to get past an error and could use some help. I have two test files one contains the ip addresses of the onboard NICs and the other... (10 Replies)
Discussion started by: mikez104
10 Replies

7. Shell Programming and Scripting

Animation Ping on Solaris Like Cisco Ping

Hi, I develop simple animation ping script on Solaris Platform. It is like Cisco ping. Examples and source code are below. bash-3.00$ gokcell 152.155.180.8 30 Sending 30 Ping Packets to 152.155.180.8 !!!!!!!!!!!!!.!!!!!!!!!!!!!!!. % 93.33 success... % 6.66 packet loss...... (1 Reply)
Discussion started by: gokcell
1 Replies

8. UNIX for Dummies Questions & Answers

Ping bash script to file

Hello: I have this script: #!/bin/bash #for loop for ip in `cat ips` do ping $ip | grep "is alive">>pingtestlog done And its working properly with this input: ericadm@amxcruas1> cat ips 10.196.60.4 10.196.61.210 10.196.62.73 10.196.61.152 (5 Replies)
Discussion started by: asenav1
5 Replies

9. Shell Programming and Scripting

Bash script for ping in your own subnet

I have a question for one who wants to help me. I want to create a bash script to ping IP-adresses. It must first ask me the beginnen IP, then the ending IP like: 192.168.100.1 - 192.168.100.255. When nothing is filled in, then it must find my subnet and ping that, like when my ip is... (14 Replies)
Discussion started by: ugurgazi
14 Replies

10. Shell Programming and Scripting

Help With Ping Script

Hello all...I'm new to Unix and learning. What I'm trying to create is a script that will ping a known range of IP addresses, say 192.168.1.1 to 192.168.1.254. For each address that no reply is received, that address will be written to a log file that will be emailed to an administrator. My wife... (1 Reply)
Discussion started by: spmitchell
1 Replies
Login or Register to Ask a Question