Run a command on multiple servers one after another
run command on multiple servers ssh | ssh loop over hosts | execute same command on many servers | ssh | loop | automationfor h in web1.example.com web2.example.com web3.example.com; do echo "== $h"; ssh -o BatchMode=yes -o ConnectTimeout=5 "$h" 'uptime; df -h /'; doneConnects to each host in turn and runs the command, printing the hostname as a header for each one. BatchMode=yes prevents password prompts, so servers without key access are simply skipped with an error. ConnectTimeout limits the wait for unreachable hosts.
Note: The risk level applies to the default command. Commands that change something affect all servers at once, so test them on a single host first.
Also searched as
- run command on multiple servers ssh
- ssh loop over hosts
- execute same command on many servers