Skip to content

Commit

Permalink
added hosts file feature
Browse files Browse the repository at this point in the history
contributed by @erpel
  • Loading branch information
hukl committed Apr 7, 2017
1 parent 883acb4 commit 7b8d3f5
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 21 deletions.
50 changes: 36 additions & 14 deletions hostmux
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/sh

USAGE="hostmux -h | [-xpPa] [-l <tmux layout>] [-s <session name>] host1 [host2 ...]"
USAGE="hostmux -h | [-xpPa] [-l <tmux layout>] [-s <session name>] [-f <host_file>] host1 [host2 ...]"

SESSION_NAME=hostmux
LAYOUT=even-vertical
Expand All @@ -10,7 +10,7 @@ SET_PROMPT=false
SYNC_PANES=false

# Get optional user settings
while getopts s:l:hxpPa opt; do
while getopts s:l:f:hxpPa opt; do
case $opt in
s) SESSION_NAME=$OPTARG
;;
Expand All @@ -19,11 +19,13 @@ while getopts s:l:hxpPa opt; do
x) CLOSE=" && exit"
;;
p) SET_PANE_TITLE=true
;;
;;
P) SET_PROMPT=true
;;
;;
a) SYNC_PANES=true
;;
;;
f) HOSTS_FILE=$OPTARG
;;
h) echo $USAGE
exit 0
;;
Expand All @@ -34,12 +36,34 @@ while getopts s:l:hxpPa opt; do
esac done
shift $((OPTIND - 1))

NUMBER_OF_HOSTS=$#
NUMBER_OF_HOSTS=0
NUMBER_OF_HOST_ARGS=$#
HOSTS=""

while [ $NUMBER_OF_HOSTS -lt $NUMBER_OF_HOST_ARGS ]
do
HOSTS="$HOSTS$1 "
NUMBER_OF_HOSTS=$(($NUMBER_OF_HOSTS+1))
shift
done

# Read hosts from the hosts file
if [ -n "$HOSTS_FILE" ]
then
while read line
do
HOSTS="$HOSTS$line "
NUMBER_OF_HOSTS=$(($NUMBER_OF_HOSTS+1))
done < hf
fi

echo $HOSTS
echo $NUMBER_OF_HOSTS

# Initialize Session
tmux -2 new-session -d -s $SESSION_NAME

# Split as many times as we have hosts as arguments and reset
# Split as many times as we have hosts and reset
# the layout each time for even distribution
LOOP_INDEX=1

Expand All @@ -52,8 +76,6 @@ done
# Check what pane-base-index is configured and use it for addressing
# the panes
PANE_INDEX=$(tmux show-window-options -g -v pane-base-index)
PANE_MAX=$(($PANE_INDEX+$NUMBER_OF_HOSTS-1))


# http://stackoverflow.com/questions/9747952/pane-title-in-tmux
if $SET_PANE_TITLE; then
Expand All @@ -65,15 +87,15 @@ fi
# then shift the argument list left, increase the index and go
# into next iteration. Now $1 refers to the next argument passed
# to the script
while [ $PANE_INDEX -le $PANE_MAX ]; do
tmux send-keys -t $PANE_INDEX " ssh $1 $CLOSE" C-m
for host in $HOSTS
do
tmux send-keys -t $PANE_INDEX " ssh $host $CLOSE" C-m
if $SET_PROMPT; then
tmux send-keys -t $PANE_INDEX " export PS1=\"[$1]$ \"" C-m
tmux send-keys -t $PANE_INDEX " export PS1=\"[$host]$ \"" C-m
fi
if $SET_PANE_TITLE; then
tmux send-keys -t $PANE_INDEX " unset PROMPT_COMMAND; printf '\033]2;$1\007'" C-m
tmux send-keys -t $PANE_INDEX " unset PROMPT_COMMAND; printf '\033]2;$host\007'" C-m
fi
shift
PANE_INDEX=$(($PANE_INDEX+1))
done

Expand Down
11 changes: 7 additions & 4 deletions hostmux.mandoc
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
.Li |
.Op Fl s Ar session-name
.Op Fl l Ar tmux-layout
.Op Fl f Ar host_file
.Op Fl x
.Op Fl p
.Op Fl P
Expand All @@ -27,20 +28,23 @@ multiplex/broadcast commands to all split panes / servers (see -a).
.Pp
Its arguments are as follows:
.Bl -tag -width Ds
.It Fl s
.It Fl s session-name
Specify a name for the tmux session. It defaults to 'hostmux' which means that
you can have only one hostmux session at a time if you don't specify unique
names for your sessions
.It Fl l
.It Fl l tmux-layout
Specify a valid tmux layout e.g. even-horizontal, tiled, etc. It defaults to
'even-vertical'
.It Fl f Ar hostfile
Specify a file which contains a newline separated list of [user@]host names. The
hosts from the file will be merged with the hosts provided as optional arguments
.It Fl x
Close the pane and/or session automatically when the ssh session
exits successfully
.It Fl p
Identify panes by setting the pane title to the ssh hostname (tmux >= 2.3), may
not work if the remote host does $PS1 magic like setting the terminal title,
in that case use
in that case use
.Fl P
.It Fl P
Identify panes by setting the remote prompt $PS1 to "[<hostname>]$ " after
Expand All @@ -67,4 +71,3 @@ to ssh. If you do need them, add them to your ~/.ssh/config
was written by
.An John-Paul Bader (hukl) ,
.Mt [email protected] .

10 changes: 7 additions & 3 deletions man/hostmux.1
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
|
[\fB\-s\fR\ \fIsession-name\fR]
[\fB\-l\fR\ \fItmux-layout\fR]
[\fB\-f\fR\ \fIhost_file\fR]
[\fB\-x\fR]
[\fB\-p\fR]
[\fB\-P\fR]
Expand All @@ -28,14 +29,18 @@ multiplex/broadcast commands to all split panes / servers (see -a).
.PP
Its arguments are as follows:
.TP 8n
\fB\-s\fR
\fB\-s\fR \fB\-session-name\fR
Specify a name for the tmux session. It defaults to 'hostmux' which means that
you can have only one hostmux session at a time if you don't specify unique
names for your sessions
.TP 8n
\fB\-l\fR
\fB\-l\fR \fB\-tmux-layout\fR
Specify a valid tmux layout e.g. even-horizontal, tiled, etc. It defaults to
.TP 8n
\fB\-f\fR \fIhostfile\fR
Specify a file which contains a newline separated list of [user@]host names. The
hosts from the file will be merged with the hosts provided as optional arguments
.TP 8n
\fB\-x\fR
Close the pane and/or session automatically when the ssh session
exits successfully
Expand Down Expand Up @@ -75,4 +80,3 @@ The \fBhostmux\fR utility exits\~0 on success, and\~>0 if an error occurs.
was written by
John-Paul Bader (hukl),
\fI[email protected]\fR.
.sp

0 comments on commit 7b8d3f5

Please sign in to comment.