How to execute a command in multiple Terminals by executing a command in only one?

6,799

Solution 1

One that I have used is ClusterSSH to send to multiple terminal windows at once.

sudo apt-get install clusterssh

ClusterSSH will open its own terminal windows that you can send commands to with the application. Once in ClusterSSH press Ctrl+Shift++ or click on Hosts in the menu and select Add host(s) or cluster(s) and type in all you want open separated by space between each one. You should get a window looking like this one:

enter image description here

then any command that you want to send to all the windows at once you type in the box below the File Hosts Send Help menu items.

I have also written a script that you can run from a command prompt to open as many Cluster SSH windows as you want. I do recommend that before you use this script to run ssh-copy-id localhost so that it launches without asking to enter a password.

The script:

#!/bin/bash

if [[ $1 == "" ]]; then

echo -n "Please enter number of CSSH windows you want to open: "
read num1

else

num1=$1

fi

if [[ ! ${num1} || ${num1} = *[^0-9]* ]]; then
    echo "${num1} is invalid input.  Please only use numbers."
    exit 1
fi

for i in $(seq $num1); do echo localhost; done | xargs nohup cssh >/dev/null 2>&1 &
disown -a

Hope this helps!

Solution 2

Take a look at the terminator terminal emulator. Homepage with screenshots: http://gnometerminator.blogspot.hu/p/introduction.html

It can open multiple panes (right click menu -> Split Horizontally/Vertically, or its corresponding Ctrl+Shift+O/E shortcuts).

Then click on the upper left button of a pane to organize these panes into groups, and broadcast keypresses to members of a group (or all the terminals). (These also have shortcuts, as you can see or alter them in the right click -> Preferences menu.)

There's an Ubuntu package available, but if you really like this app and plan to use it in the long run, I recommend to check out the gtk3 version from https://code.launchpad.net/~gnome-terminator/terminator/gtk3. Ubuntu ships the gtk2 version, but the gtk3 version is way more improved (mostly due to plenty of fixes to the underlying VTE library's gtk3 version which is responsible for the actual terminal emulation, but also due to quite a few recent fixes to Terminator itself).

Share:
6,799

Related videos on Youtube

Admin
Author by

Admin

Updated on September 18, 2022

Comments

  • Admin
    Admin over 1 year

    I am going to have multiple machines that I am going to connect to via SSH so I am going to have multiple Terminal windows up, though what I want to do is to execute a command so that all the machines execute it, but currently what I will have to do is execute it in each individual Terminal window, is there any way that I can execute a command in one Terminal which will execute a specified command in all specified Terminal sessions for greater efficiency as I don't want to have to enter it into every individual Terminal window? I am going to be using gnome-terminal if that makes any difference.

    I am running Ubuntu GNOME 15.10 with GNOME 3.18 and the other machines should be running the same.

    • Jacob Vlijm
      Jacob Vlijm over 8 years
      Not sure if this is what you are looking for, but it can be used like that: askubuntu.com/a/642095/72216
    • Panther
      Panther over 8 years
      If you are using keys, and your keys are loaded, in a separate terminal, script it. for ssh in list ; ssh user@$ssh "command"; done
    • Admin
      Admin over 8 years
      @JacobVlijm: Yeah, well if I could tell it in one command to send the command to multiple Terminal windows then that would do.
    • Jacob Vlijm
      Jacob Vlijm over 8 years
      Should be possible!