Lefty G Balogh Lefty G Balogh - 1 month ago 14
Linux Question

How to reboot several remote machines via ssh in a single CLI command

What I want to achieve is to reboot several remote servers without having to log into each individually.
So I fire away the following, hoping it would hop onto server 93, 95 and 97 and reboot each.

[root@<home_server> ~]# seq 93 2 97 |xargs -I{} ssh <remoteservernumber_>{} "hostname; reboot"

The authenticity of host '<remoteservernumber_93 (IP.IP.IP.IP)' can't be established.
RSA key fingerprint is ...
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'remoteservernumber_93 (IP.IP.IP.IP)' (RSA) to the list of known hosts.

root@remoteservernumber_93's password:
remoteservernumber_93
Connection to remoteservernumber_93 closed by remote host.
xargs: ssh: exited with status 255; aborting


However, what I end up getting is when the first server is rebooted it terminates the connection and it does not hop onto the next server. What am I doing wrong?

Answer

For want of a better alternative I found the following script works:

for i in <server-1> <server-2> ... <server-n> ; do ssh $i "hostname; sudo reboot"; done