Steve mitchell in Side notes 4 minutes

How to Multicast Commands

If you are more Dev than Ops, like me, then you will think this is cool. If you are more Ops than Dev, then you already know how to do this. When I press ⌘-⇧-P from iTerm I am immediately connected to all my Raspberry Pis at the same time. It’s great for running simultaneous software updates.

Ta-Dah!

Sign-in with Shortcut
Sign-in with Shortcut

It just takes three steps to do:

  1. Add the Raspberry Pis to your Host’s File
  2. Use SSH Keys for Authentication
  3. Save a terminal session that automatically signs into all of your nodes from a shortcut key.

Add the Raspberry Pis to your Host’s File

Add the hostname of all the Raspberry nodes to the /etc/hosts file on your computer.

Add Pis to Hosts File
Add Pis to Hosts File

Use SSH Keys for Authentication

Complex passwords are a drag to type over and over. Use SSH keys to authenticate with your Raspberry Pi nodes. You’ll need RSA keys on your laptop. Look in ~/.ssh. If you don’t have one, use ssh-keygen to create one:

1
ssh-keygen

Once you have an RSA Key, send it to each Raspberry Pi node with ssh-copy-id.

1
ssh-copy-id -i ~/.ssh/id_rsa.pub  pi@pi1

You can now ssh into pi1 without a password. Repeat for each Raspberry Pi node in your cluster.

Automatically Sign in to all the Nodes at Once

This last step takes a bit of work to set up, but it allows you to use a keyboard shortcut in iTerm to open terminals to all of your Raspberry Pi nodes at once. This allows you to automatically sign in to all of them. Next, you can use iTerm’s “Broadcast Input to all Panes” feature to send keystrokes simultaneously to all of the nodes.

Read Andrew Ray’s blog post and follow his step-by-step instructions. Here are the screenshots from my setup.

Split your terminal window into panes for all the Raspberry Pi nodes. Then create a separate profile for each host.

Create Profile for Each Host
Create Profile for Each Host

Note how the “Send text at start” field is set to “ssh pi@pi1” to log into the host. Each profile signs into a different host.

Select each terminal pane and create a session. Select the corresponding profile from the list and click the Use Selected Profile button.

Create a Session in each Pane linked to a Profile
Create a Session in each Pane linked to a Profile

When all of the terminal panes are tied to a session, save the entire window.

Save all the Panes in the Window
Save all the Panes in the Window

Finally, add a shortcut to open the window. I chose ⌘-⇧-P (for Pi).

Add Keyboard Shortcut to Launch Window
Add Keyboard Shortcut to Launch Window

Now, when I open iTerm and press, ⌘-⇧-P, iTerm opens a window with six sessions, each automatically connected to each Raspberry Pi.

Sign-in with Shortcut
Sign-in with Shortcut

To simultaneous type in all six terminal windows, use iTerm’s Broadcast feature.

How to Broadcast Keystrokes to all Raspberry Pis
How to Broadcast Keystrokes to all Raspberry Pis

That’s it. These three things will make working with your Raspberry Pi cluster so much more enjoyable.


References