How do I run many SSH remote commands, on multiple machines, in batch? How do I run many SSH remote commands, on multiple machines, in batch? shell shell

How do I run many SSH remote commands, on multiple machines, in batch?


Your use of ConnectTimeout is correct, so it is not obvious why it only times out after 30 or more seconds.

Here's how I would change your script to avoid the timeout problem entirely:

  • Use GNU parallel to connect to more than one destination host at the same time.
  • Use the -f option to SSH to process it in the background.

Here is a solution with GNU parallel, running at most 50 connections at the same time:

parallel --gnu --bg --jobs 50 \ssh -o BatchMode=yes \    -o StrictHostKeyChecking=no \    -o ConnectTimeout=10 \    -l ${USERNAME} \    {} \    "${COMMAND} -i {} || echo timeout" \::: ${IP}

parallel <command> ::: <arguments> will execute <command> <argument> many times in parallel by splitting the <arguments> list. The placeholder for <argument> is {}.

Use parallel --jobs n to limit the number of parallel connections.


The connection timeout is for when you have already established a connection and if the connection stays idle for that amount of time in seconds, then it will disconnect (That is if you did not also activate the KEEP_ALIVE ssh parameter that prevent a connection from ever being idle).

The reason it takes 30+ seconds before you get a time out is because it is the TCP protocol internal timer that try to connect for that amount of time and return that error message that he cannot connect to the sftp server. It does not comes from ssh.