Tero Karvinen - Articles - Downloads - Courses - Contact

Control Many Computers with SSH

Once you have automated login to target computers with ssh public key authentication, it is easy to run the same command on all of them.

Consider three computers, 172.28.1.219, 172.28.2.9, 172.28.2.14, each with account tero and ssh public key authentication enabled.

To run ‘w’ (better who) on each computer:

$ for S in 1.219 2.9 2.14 ; do ssh tero@172.28.$S w; done

Output of ‘w’ from each computer is printed:

[tee@localhost ~]$ for S in 1.219 2.9 2.14; do ssh tero@172.28.$S w; done
 12:05:26 up  2:27,  2 users,  load average: 0.00, 0.02, 0.00
USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU WHAT
antti    :0       -                09:55   ?xdm?   1:48   0.22s /usr/bin/gnome-
antti    pts/2    :0.0             10:11   12.00s  0.17s  0.01s ssh a0400730@my
 16:05:20 up  2:49,  3 users,  load average: 0.00, 0.03, 0.00
USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU WHAT
samuli   :0       -                13:17   ?xdm?   1:23   0.25s /usr/bin/gnome-
samuli   pts/1    :0.0             13:18   53.00s  0.11s  0.01s ssh a0400722@my
samuli   pts/3    :0.0             14:44   55.00s  0.03s  0.03s bash
 16:05:22 up  1:56,  6 users,  load average: 0.01, 0.05, 0.05
USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU WHAT
juronn   :0       -                14:10   ?xdm?   2:38   0.39s /usr/bin/gnome-
juronn   pts/1    :0.0             14:41   11:37   0.14s 22.41s gnome-terminal
juronn   pts/3    :0.0             15:20   30:28   0.04s  0.01s ssh vieras@172.
juronn   pts/5    :0.0             15:36   20:05   0.11s  0.11s bash
juronn   pts/2    :0.0             15:59   51.00s  0.03s  0.03s bash
juronn   pts/4    :0.0             16:02    1:46   0.03s  0.02s ssh a0500038@my

Add ‘echo’ before command to see what it does:

$ for S in 1.219 2.9 2.14 ; do echo ssh tero@172.28.$S w; done
ssh tero@172.28.1.219 w
ssh tero@172.28.2.9 w
ssh tero@172.28.2.14 w

See also

Last modified: 2005-11-17. Permanent url: http://www.iki.fi/karvinen/control_many_computers_with_ssh.html

Tero Karvinen www.iki.fi/karvinen - Top - Validate HTML - Validate CSS