3

I'm very new in bash scripting and unix so I will need some help on this.I have 7-10 hosts which I want to ping from one of the servers via cronjobs. What I want is when host is up to execute command on it. When is down do nothing.

I don't want logs or any messages. So far I have this and unfortunately don't have ability to try it right now. If you can just check it and point me.

#!/bin/bash
servers=( "1.1.1.1" "2.2.2.2" "3.3.3.3" "4.4.4.4" "5.5.5.5" "6.6.6.6" "7.7.7.7" )

for i in "${servers[@]}"
do
  ping -c 1 $i > /dev/null  
done

ping -c 1 $i > /dev/null
if [ $? -ne 0 ]; then

    if [ $STATUS >= 2 ]; then
        echo ""
    fi
else
    while [ $STATUS <= 1 ];
    do 
       # command should be here where is status 1 ( i.e. Alive )
       /usr/bin/snmptrap -v 2c -c public ...
    done
fi

I'm not sure if this is right or no. I've used this from one tutorial and there is some things that I'm not sure what they exactly do.

Am I on right way here or I'm totaly wrong?

4
  • 2
    Instead of pinging the hosts first (LBYL — Look Before You Leap), why don't you just run the command you intend to run (EAFP — Easier to Ask Forgiveness than Permission). If the hosts is up, the command will work and if the host is down, it simply won't work.
    – Celada
    Commented Jul 29, 2014 at 12:43
  • 2
    The point is that this will be somethik like alarm manager. I mean I have one web page where I have computer icons for every host. When I ping them if some icons turn red that means is down and when is green is up. Your comment make sense just I want to do it in this way. Thank's
    – S.I.
    Commented Jul 29, 2014 at 12:48
  • I rolled back your edit to a previous version of your question. Your edit completely re-wrote the question making the answers you have received irrelevant. If you have a new question, please post it separately. If you have a simple follow-up question, edit it into this one but don't remove the original version, just add to it.
    – terdon
    Commented Jul 30, 2014 at 15:16
  • @terdon, thank you. I will have this in my mind.
    – S.I.
    Commented Jul 30, 2014 at 17:50

2 Answers 2

4

I've made some comments in line to explain what different parts of the script are doing. I've then made a concise version of the script below.

#!/bin/bash
servers=( "1.1.1.1" "2.2.2.2" "3.3.3.3" "4.4.4.4" "5.5.5.5" "6.6.6.6" "7.7.7.7" )

# As is, this bit doesn't do anything.  It just pings each server one time 
# but doesn't save the output

for i in "${servers[@]}"
do
  ping -c 1 $i > /dev/null  
# done
# "done" marks the end of the for-loop.  You don't want it to end yet so I
# comment it out

# You've already done this above so I'm commenting it out
#ping -c 1 $i > /dev/null

    # $? is the exit status of the previous job (in this case, ping).  0 means
    # the ping was successful, 1 means not successful.
    # so this statement reads "If the exit status ($?) does not equal (-ne) zero
    if [ $? -ne 0 ]; then
        # I can't make sense of why this is here or what $STATUS is from
        # You say when the host is down you want it to do nothing so let's do
        # nothing
        #if [ $STATUS >= 2 ]; then
        #    echo ""
        #fi
        true
    else
        # I still don't know what $STATUS is
        #while [ $STATUS <= 1 ];
        #do 
           # command should be here where is status 1 ( i.e. Alive )
           /usr/bin/snmptrap -v 2c -c public ...
        #done
    fi

# Now we end the for-loop from the top
done

If you need a parameter for each server, create an array of parameters and an index variable in the for-loop. Access the parameter via the index:

#!/bin/bash
servers=( "1.1.1.1" "2.2.2.2" "3.3.3.3" "4.4.4.4" "5.5.5.5" "6.6.6.6" "7.7.7.7" )
params=(PARAM1 PARAM2 PARAM3 PARAM4 PARAM5 PARAM6 PARAM7)

n=0
for i in "${servers[@]}"; do
    ping -c 1 $i > /dev/null  

    if [ $? -eq 0 ]; then
       /usr/bin/snmptrap -v 2c -c public ${params[$n]} ...
    fi

    let $((n+=1)) # increment n by one

done
21
  • Thank you for the answer! I believe i've meaned $i where is $STATUS. Is just this 10 lines? With one cronjob I can run this script let's say every 1 hour and will check servers from the list and execute command if the server is alive? Also do I need something more or this is just enought to run command /usr/bin/snmptrap -v 2c -c public ...?
    – S.I.
    Commented Jul 29, 2014 at 13:06
  • I haven't tested it but this should be all you need. You can give it a shot and let me know if there is a problem. Whatever terminal commands you put between the if and fi will run for each server that's up.
    – drs
    Commented Jul 29, 2014 at 13:26
  • Note that in the for-loop, $i is the IP address of the server, not the status of the server. You will probably have to use $i in your snmptrap command.
    – drs
    Commented Jul 29, 2014 at 13:28
  • Ok, thank you again. I will test it a little later and will write if there is some problem.
    – S.I.
    Commented Jul 29, 2014 at 13:29
  • I think I don't need to use it in snmptrap since when see server alive will execute the command without matter what IP is.
    – S.I.
    Commented Jul 29, 2014 at 13:31
4

Even more concise

#!/bin/bash

servers=( "1.1.1.1" "2.2.2.2" "3.3.3.3" "4.4.4.4" "5.5.5.5" "6.6.6.6" "7.7.7.7" )

for i in "${servers[@]}"; do
    ping -c 1 $i > /dev/null && /usr/bin/snmptrap -v 2c -c public ...
done

NOTES: The "&&" after ping means "IF TRUE THEN", and in the case of ping, TRUE means ping did not fail (i.e. the server successfully responded to ping)

1
  • 1
    This work perfect. Tested on my local on VM. I'll try tomorrow on the real server and will write and accept. Thank you
    – S.I.
    Commented Jul 29, 2014 at 17:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .