Thursday, 8 September 2016

Tmux for ssh multiplexing/parallel execution

Continuing with the pursuit of tools that allow parallel execution of commands across hosts, I came across the tmux utility.  the inherent purpose of tmux is not parallel execution but it is the ability to run multiple TTYs in a single terminal window.

Tmux provides features similar to screen utility. I won't dive into the details here in this article as the focus of this post is parallel execution.

I would like to point to a links here & I found them as awesome tutorials exploring the features of tmux in vivid detail:
Tmux gets installed as a rpm that is available in the EPEL repository via the command 'yum install tmux'.

Tmux essentially uses a configuration file called .tmux.conf to gather its session parameters or settings.
Most of the features in tmux are modified using ctrl+b followed by a keystroke. The different combinations can be fond in the tmux cheatsheet. To enter commands interactively type ctrl+b: on the terminal window.

I've used a custom .tmux.conf file & shell script to do a parallel ssh to 2 hosts & run comands in sync on the hosts.

The .tmux.conf file looks like this:

[root@devbox ~]# cat .tmux.conf
# Enable Mouse Mode
set -g mode-mouse on
set -g mouse-resize-pane on
set -g mouse-select-pane on
set -g mouse-select-window on
set-option -g mouse-utf8
bind-key | split-window -h  #Horizontal
bind-key - split-window -v  #Vertical
bind-key s set-window-option synchronize-panes
[root@devbox ~]#


The shell script is as follows:

[root@devbox ~]# cat ut.sh
#!/bin/bash
tmux new-session -d -s UnderstandingIT

tmux new-window -t UnderstandingIT:1 -n 'Server1' 'ssh root@192.168.44.135'
tmux new-window -t UnderstandingIT:2 -n 'Server2' 'ssh root@192.168.44.137'

tmux select-window -t UnderstandingIT:1
tmux -2 attach-session -t UnderstandingIT
[root@devbox ~]#


I've defined a couple of shortcuts in the .tmux.conf file which are:

ctrl+b | => split panes vertically.
ctrl+b - => split panes horizontally.
ctrl+b s => turn on synchronization.

Here's the demo:

type the following command to source the .tmux.conf file:

tmux source-file .tmux.conf

Then run the script.ut.sh

[root@devbox ~]# ./ut.sh

This will start our session as shown below:



Now to split the window vertically, I typed ctrl+b |


Now as you can see we are logged into both servers & are viewing the panes split in a vertical fashion.
We'll be able to move across panes with the click of a mouse.

Finally, to synchronize command execution across the panes, type ctrl+b s:



Sharing a session:

As screen allows to share session so does tmux 
In order to share session on the same machine, you have to explicitly give tmux the path to the Unix session which will be used through the session lifetime:

tmux -S /tmp/our_session

Then you have to give other users access to the newly created file:

chmod 777 /tmp/our_session

As expected, when a new user wants to join the session, he has to pass the session path

tmux -S /tmp/our_session attach

No comments:

Post a Comment

Using capture groups in grep in Linux

Introduction Let me start by saying that this article isn't about capture groups in grep per se. What we are going to do here with gr...