I am using tmux environment and often times I have to run the same python script in 4 different panes (top 4 in the image) with same command line arguments. Is there a way I can execute the script on each shell by executing command on one?

I am aware of this discussion but they suggest using a different terminal environment, I am looking for something that can be done using tmux or shell scripting.

Tmux session

The four different shells are ssh sessions to 4 different VMs.


No need for any tools. tmux can handle this:

just open up the panes, ssh to the individual servers, and then Ctrl-B followed by

:setw synchronize-panes 

and all input gets synchronized to all visible panes.
Re-type this or add "off" to the command to leave.

  • You need to insert an empty line before an indented block, for it to show up in a gray background code block. – Anthon Dec 6 '16 at 12:39

Yes it is possible, with a tool named ttyecho that can emulate user interaction in different terminals.

Download and install:

wget http://www.resulinux.tk/utils/ttyecho.c -O ttyecho.c
gcc ttyecho.c -o /usr/bin/ttyecho

Now lets execute something in other terminal, for example as user john that is loggeg at pts/17 using xterm as you see in the ps command:

ps aux | grep john 
john   9198  0.0  0.0  23836  4524 pts/17   Ss   Jul21   0:00 /bin/bash

Now, lets try to open vi and type some text in the other terminal.

ttyecho -n /dev/pts/17 vi (executed vi command on the other terminal) 
ttyecho  /dev/pts/17 i (entered in insertion mode)
ttyecho -n /dev/pts/17 some text  

When you look to the terminal that john is logged in, you will see that vi is really executed, and you can see the text we type at it "some text". So now you have full control to the others terminal sessions.

Running commands in multiple ssh sessions

Determine the ssh pts devices:

ps aux | grep ssh

root      3540  0.0  0.0  44924  5764 pts/1    S+   14:46   0:00 ssh root@
root      5907  0.0  0.0  44924  5684 pts/17   S+   12:51   0:00 ssh root@
root      8074  0.0  0.0  51216  3948 pts/6    S+   Jul26   0:01 ssh root@

So to execute commands on ssh logged in, i just need:

ttyecho -n /dev/pts/6 ls

And the ls command will execute really remotely in the single script!

  • It works for shells on the same system (VM in my case), but how will it work for ssh sessions to different VMs? – Wajahat Jul 27 '16 at 19:06
  • Are the ssh client sessions running in the same computer? If they are it will work, you just need to determine the /dev/pts of the terminals running the ssh clients. For this do ps aux | grep ssh and you can determine the right device to communicate to the remote session. – Luciano Andress Martini Jul 27 '16 at 19:13
  • Hmmm. So I will need to run the command from the parent terminal you mean. Actually all the ssh sessions are running from hypervisor machine. So I will have to run ttyecho on the hypervisor and give the tty numbers. – Wajahat Jul 27 '16 at 19:18
  • You need to run it on the machine you are operating the ssh sessions and not in the servers accessed by the ssh, like i edited in the answer. – Luciano Andress Martini Jul 27 '16 at 19:30
  • How do you give multiple /dev/pts/? devices in on command? – Wajahat Nov 17 '16 at 20:17

clusterssh may be of interest.

enter image description here

Also try dsh. https://www.linuxhelp.com/how-to-use-dsh-to-run-linux-commands-in-multiple-machines/

  • 1
    LOL! This screenshot made me pretty happy. Thanks. – CoreyJJohnson Nov 13 '18 at 19:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.