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

automationcommand linegnome-terminalssh

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.

Best Answer

  • 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!