Ssh – Increase Connection Timeout for “ssh: connect to host xxxxx port 22: Connection timed out”

connectionsshtimeout

I need to perform a sort of scan over 20k+ machines in the network (all linux), and I am using ssh to connect to each machine, run a small command on it, and gather the output. I am running parallel threads with around 3k to 4k machines in each.

Problem is that for some machines, login is not working, which in my case indicates that machine was not properly provisioned. SSH takes about a minute to timeout, this 1 minutes delays the over scan.

Is there any option to reduce the timeout period ssh has to wait before ending the attempt to connect and display the message:

ssh: connect to host xxxxx port 22: Connection timed out

and the move on to the next machine? This will speed up the scans overall significantly, not only when I am doing a scan on too many machines, but even on a small number of machines.

Best Answer

Use the ConnectTimeout option. You can use it via -o flag. It takes the argument in seconds. For example, to set the timeout period to 3 seconds, use the following:

ssh -o ConnectTimeout=3 username@hostname

Note: If the machine is not on your local network, or there is a high latency in your local network, you should keep the timeout period to a higher value, as latency may affect the initial connection making.

Related Topic