11

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.

3 Answers 3

24

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.

2
  • You need to insert an empty line before an indented block, for it to show up in a gray background code block.
    – Anthon
    Commented Dec 6, 2016 at 12:39
  • Try :set synchronize-panes off to disbale it. Commented Jan 23, 2023 at 10:36
2

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 [email protected]
root      5907  0.0  0.0  44924  5684 pts/17   S+   12:51   0:00 ssh [email protected]
root      8074  0.0  0.0  51216  3948 pts/6    S+   Jul26   0:01 ssh [email protected]

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

ttyecho -n /dev/pts/6 ls

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

6
  • It works for shells on the same system (VM in my case), but how will it work for ssh sessions to different VMs?
    – Wajahat
    Commented Jul 27, 2016 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. Commented Jul 27, 2016 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
    Commented Jul 27, 2016 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. Commented Jul 27, 2016 at 19:30
  • How do you give multiple /dev/pts/? devices in on command?
    – Wajahat
    Commented Nov 17, 2016 at 20:17
1

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
  • 1
    LOL! This screenshot made me pretty happy. Thanks. Commented Nov 13, 2018 at 19:17

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .