开发者

Continue to grep for traceroute result with bash

开发者 https://www.devze.com 2023-01-03 20:08 出处:网络
Every night I go through the same process of checking failover systems for our T1\'s. I essentially go through the following process:

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 com开发者_C百科mand.


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.


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
0

精彩评论

暂无评论...
验证码 换一张
取 消