0

I need a script that will ping two hosts and execute a command depending on their status.

i.e

#!/bin/bash

HOSTS="1.1.1.1 2.2.2.2"

for myHost in $HOSTS
do

fping $myHost > /tmp/ping.log

if  [ $(grep -c "1.1.1.1 is unreachable" "/tmp/echo.log") -eq 1 ]; then
echo "1.1.1.1 is down"

else
if [ $(grep -c "2.2.2.2 is alive" "/tmp/echo.log") -eq 1 ]; then
echo "2.2.2.2 is alive"

fi
fi
done

then i need to have if 1.1.1.1 doesn't respond and 2.2.2.2 does then do command and the other way round i.e if 1.1.1.1 responds and 2.2.2.2 doesn't then do command.

Lurch
  • 819
  • 3
  • 18
  • 30

1 Answers1

1
HOSTS="127.0.0.1 227.1.2.3"

RESULT=""

for HOST in $HOSTS; do
    if fping $HOST > /dev/null 2>&1; then
       RESULT="${RESULT}1"
    else
       RESULT="${RESULT}0"
    fi
done

echo "RESULT=$RESULT"

case "$RESULT" in

00)
    echo "host 1 dead, host 2 dead"
    ;;

01)
    echo "host 1 dead, host 2 alive"
    ;;


10)
    echo "host 1 alive, host 2 dead"
    ;;


11)
    echo "host 1 alive, host 2 alive"
    ;;
esac

This script will create some kind of binary status of the list of hosts alive (1) or dead (0). In the case block you can easy handle all possible combinations.

If you ad a third host, you will get something like "001" or "010" or "110" ... as result.

You don't need to add all possible combinations, just add what you want to react to and handle the rest of it with

*)
    echo "don't care state of $RESULT detected"
    ;;
Mario Keller
  • 411
  • 2
  • 5