Wednesday, November 28, 2012

SSH Failover

You can have SSH iterate through a list of servers until it finds the one that works.
for i in $(cat servers.txt); do
if ssh -o ConnectTimeout=10 $i; then
echo "$i is the one that works"
fi
done
The ConnectTimeout option specifies that if it cant make a connection in the specified time, than it stops trying, and moves on to the next iteration. Otherwise you can wait the default of 45 seconds.

My suggestion is to have servers.txt file actually be the host entries in ~/.ssh/config :
my_vps
my_other_vps
my_vps2


Otherwise, just type out:

user@example.com
user1@internet.com
user@internet.com
etc...

No comments:

Post a Comment