0

Every night I go through the same process of checking failover systems for our T1's. I essentially go through the following process:

Start the failover process.

traceroute $server;

Once I see it's failed over, I verify that connections work by SSHing into a server.

ssh $server;

Then once I see it works, I take it off of failover.

So what I want to do is to continually run a traceroute until I get a certain result, then run a SSH command.

Michael Petrotta
  • 59,888
  • 27
  • 145
  • 179

2 Answers2

1

Put your list of successful messages in a file (omit the variable lines and fractions of the line, and use a ^ to identify the start of the line, as such:)

patterns.list:

^ 7  4.68.63.165 
^ 8  4.68.17.133 
^ 9  4.79.168.210 
^10  216.239.48.108 
^11  66.249.94.46 
^12  72.14.204.99 

Then a simple while loop:

while ! traceroute -n ${TARGET} | grep -f patterns.list
do
  sleep 5   # 5 second delay between traceroutes, for niceness.
done
ssh ${DESTINATION}

Use traceroute -n to generate the output so you don't get an IP address that resolves one time, but and a name the next, resulting in a false positive.

Slartibartfast
  • 1,694
  • 9
  • 8
1

I think you could be better off using ping command to verify server's accessability than traceroute.

It is easy to check for return status of ping command without using any grep at all:

if [ ping -c 4 -n -q 10.10.10.10 >/dev/null 2>& ]; then
    echo "Server is ok"
else
    echo "Server is down"
fi

If you want to do it continually in a loop, try this:

function check_ssh {
    # do your ssh stuff here
    echo "performing ssh test"
}
while : ; do
    if [ ping -c 4 -n -q 10.10.10.10 >/dev/null 2>& ]; then
        echo "Server is ok"
        check_ssh
    else
        echo "Server is down"
    fi
    sleep 60
done
thor
  • 2,204
  • 3
  • 20
  • 23
  • The reason I probably wouldn't use a ping would be because this failover system is pretty good, and there are usually only a few seconds before it kicks. The traceroute is for me to see that it's on the failover route. Thanks though :) – PsychoKlown Mar 27 '11 at 04:45