# Copyright (c) 2016 Uwe Werler # # Permission to use, copy, modify, and distribute this software for any # purpose with or without fee is hereby granted, provided that the above # copyright notice and this permission notice appear in all copies. # # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. _path=$0 _path=${_path%/*} _sock=$0 _sock=${_sock##*/} _sock=${_sock%%.*} _ssh_config=${_path}/${_sock}.conf _style_map=${_path}/${_sock}.map _sess="SSH" _tabw="15" # width of the tabs created # run tmux with some defaults like utf-8 support and at an separate socket _tmux="tmux -2 -u -L ${_sock}" usage(){ echo \ " usage: /bin/sh $0 -cgsu [args] -c : start server and connect xterm or attach term to detached session -c args: start server and run ssh with args or start ssh and attach xterm -r sets pane title of the active pane For usage from within shell create an alias like this: alias ssh=\"sh $0 -c\" For usage from within cwm put this in your ~/.cwmrc: command term \'ksh -c \". $0 -c \$1\"\' bind CM-Return xterm #...and if autogroup preferred autogroup 1 \"${_sess},XTerm\" If xdotool is installed it focusses automatically to the xterm running this tmux session or attaches a xterm if the session is detached. " exit 1 } _checkname(){ local _host # parse the output from ssh -G for _l; do case $_l in hostname) # make stupid check if hostname is an IP _ip=${2##+([0-9]).+([0-9]).+([0-9]).+([0-9])} _ip=${_ip##+([0-9a-f]):+([0-9a-f]):+([0-9a-f]):*([0-9a-f:]):+([0-9a-f])} if [[ -z ${_ip} ]]; then _host=$(dig +short -x ${2}) [[ -n "${_host}" ]] && _host=${_host} || _host=${2} else _host=${2} fi break ;; esac shift done print ${_host} } _runxdotool(){ # with xdotool installed, activate the window or reattach to a detached session if type xdotool >/dev/null; then _winid="xdotool search --classname ${_sess}" [[ -z $(${_winid}) ]] && { xterm -title ${_sess} -name ${_sess} -e \ $_tmux attach -t ${_sess} & sleep 0.5; } 2>/dev/null xdotool search --classname ${_sess} windowactivate fi } _readstyle() { [[ -s $_style_map ]] || return 0 while read _pattern _style; do _pattern=${_pattern%%#*} # delete comments _style=${_style%%#*} # delete comments [[ -z ${_pattern} || -z ${_style} ]] && continue [[ ${_host} == ${_pattern} ]] && break done <$_style_map } _setpane(){ local _title=${1} _style _title=${_title%%.*} #[[ -z ${_title} ]] && _title=$(hostname -s) [[ -z ${_title} ]] && _title=$(hostname -s) $_tmux select-pane -T "$(printf %-${_tabw}.${_tabw}s ${_title})" _readstyle if [[ -n ${_oldstyle} ]]; then $_tmux select-pane -P ${_oldstyle} elif [[ -n ${_style} ]]; then $_tmux select-pane -P $_style fi } _ssh() { # remove string when invoked via cwm [[ $1 == "[ssh]" ]] && shift if [[ -n $1 ]]; then # force a tty e.g. when running a command at the remote side like tmux ;) _cmd="ssh -t $@" # let ssh parse the command line so we are sane # avoid overriding the hostname via main config _host=$(_checkname $(/usr/bin/ssh -F /dev/null -G $@)) fi [[ $($_tmux list-session -F "#S" 2>/dev/null) == *${_sess}* ]] && _s=${_sess} # check if called from inside our tabbed tmux to avoid a new window if [[ $TMUX == *${_sock}* && ${_s} == ${_sess} ]]; then # trap to be able to name the pane back after ssh session endet from within the pane trap "_oldstyle=$($_tmux select-pane -g) _setpane" INT EXIT _setpane ${_host} $_cmd exit # run in already started session or reattach elif [[ ${_s} == ${_sess} ]]; then [[ -z ${_cmd} ]] && _runxdotool && exit $_tmux new-window -P -t ${_sess} ${_cmd} _setpane ${_host} _runxdotool # create new session else $_tmux -f $_ssh_config new-session -d -s ${_sess} ${_cmd} # add some settings into the session environment $_tmux set-environment -g -t ${_sess} CMD "/bin/sh $0 -r" $_tmux set-hook -g -t ${_sess} after-new-window "run \$CMD" $_tmux set-hook -g -t ${_sess} after-split-window "run \$CMD" $_tmux bind R source-file $_ssh_config \\\; display-message "source-file done" _setpane ${_host} xterm -title ${_sess} -name ${_sess} -e \ $_tmux attach -t ${_sess} 2>/dev/null & fi } getopts ":cr" _opt shift $((OPTIND-1)) case "$_opt" in c) _ssh $OPTARG $@ ;; r) _setpane $OPTARG $@ ;; *) usage ;; esac #vim: set ai:ts=2:et:sw=2