How to do ssh with a timeout in a script?

user57421 picture user57421 · Feb 8, 2011 · Viewed 251.4k times · Source

I'm executing a script connecting via password-less SSH on a remote host. I want to set a timeout, so that if the remote host is taking an infinite time to run, I want to come out of that ssh session and continue other lines in my sh script.

Any idea on how to do it?

Answer

user57421 picture user57421 · Mar 10, 2011
ssh -o ConnectTimeout=10  <hostName>

Where 10 is time in seconds. This Timeout applies only to the creation of the connection.