[125] | 1 | #!/bin/bash |
---|
| 2 | # |
---|
[126] | 3 | # 2014/03/26 Gabriel Moreau <Gabriel Moreau(A)univ-grenoble-alpes.fr> - Initial release |
---|
[125] | 4 | # |
---|
| 5 | # From http://hd-recording.at/dokuwiki/doku.php?id=linux:tmux#tssh |
---|
| 6 | |
---|
| 7 | # Clean when Ctrl^C |
---|
| 8 | trap '[ -n "${base_path}" -a -d "/tmp/${base_path}" ] && rm -rf "/tmp/${base_path}"' QUIT |
---|
| 9 | |
---|
| 10 | export PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin |
---|
| 11 | export LANG=C |
---|
| 12 | |
---|
| 13 | |
---|
| 14 | function usage() { |
---|
| 15 | cat <<END_USAGE |
---|
| 16 | NAME |
---|
| 17 | $(basename $0) - tmux cluster ssh |
---|
| 18 | |
---|
| 19 | SYNOPSIS |
---|
| 20 | $0 [-w number] [-f] [-v] <host1> <host2> <clusterssh class>... <hostM>- <hostN>+ |
---|
| 21 | |
---|
| 22 | OPTIONS |
---|
| 23 | -w windows to open (integer, default 16) |
---|
| 24 | -f fast, no nmap scan to eliminate sleeping computer |
---|
| 25 | -v verbose |
---|
| 26 | -h help |
---|
| 27 | |
---|
[128] | 28 | DEPENDS |
---|
[126] | 29 | On Debian, you need the package |
---|
[128] | 30 | |
---|
[126] | 31 | apt-get install tmux ncurses-bin wamerican nmap |
---|
| 32 | |
---|
| 33 | wamerican (or wfrench...) is used to choose a random word in the file /usr/share/dict/words |
---|
| 34 | for each new tmux session. |
---|
| 35 | ncurses-bin is required for the tput command |
---|
| 36 | to automatically split your terminal into several small panels. |
---|
| 37 | nmap is only used for dynamic DNS domain and dynamic scan. |
---|
| 38 | This is not mandatory for general use. |
---|
| 39 | |
---|
[128] | 40 | By default, tssh use tput to know the number of columns and lines of your terminal. |
---|
| 41 | It takes 10 lines and 40 columns for each windows by default. |
---|
| 42 | If tput is not installed, the default is 16 windows... |
---|
| 43 | |
---|
[125] | 44 | AUTHOR |
---|
| 45 | Gabriel Moreau |
---|
| 46 | |
---|
| 47 | COPYRIGHT |
---|
| 48 | Copyright (C) 2014-2017, LEGI UMR 5519 / CNRS UGA G-INP, Grenoble, France |
---|
| 49 | Licence : GNU GPL version 2 or later |
---|
| 50 | END_USAGE |
---|
| 51 | } |
---|
| 52 | |
---|
[128] | 53 | export remote_command='' |
---|
[125] | 54 | export split_number=16 |
---|
| 55 | if which tput > /dev/null |
---|
| 56 | then |
---|
| 57 | export split_number=$(( ($(tput lines)/ 10) * ($(tput cols)/ 40) )) |
---|
| 58 | fi |
---|
| 59 | |
---|
| 60 | export dyn_domain='' |
---|
| 61 | if [ -e "${HOME}/.tsshrc" ] |
---|
| 62 | then |
---|
| 63 | . "${HOME}/.tsshrc" |
---|
| 64 | fi |
---|
| 65 | |
---|
| 66 | # get options |
---|
| 67 | if [ $# -eq 0 ]; then usage; exit 1; fi |
---|
[128] | 68 | while getopts "w:c:fvh" options |
---|
[125] | 69 | do |
---|
| 70 | case ${options} in |
---|
| 71 | w) |
---|
| 72 | if echo ${OPTARG} | egrep -q '^[[:digit:]]+$' && [ ${OPTARG} -gt 0 ] |
---|
| 73 | then |
---|
| 74 | export split_number=${OPTARG} |
---|
| 75 | else |
---|
| 76 | usage |
---|
| 77 | exit 2 |
---|
| 78 | fi |
---|
| 79 | ;; |
---|
[128] | 80 | c) |
---|
| 81 | if echo ${OPTARG} | egrep -q '[[:alpha:]]' |
---|
| 82 | then |
---|
| 83 | export remote_command=${OPTARG} |
---|
| 84 | else |
---|
| 85 | usage |
---|
| 86 | exit 2 |
---|
| 87 | fi |
---|
| 88 | ;; |
---|
[125] | 89 | f) |
---|
| 90 | export fast='yes' |
---|
| 91 | ;; |
---|
| 92 | v) |
---|
| 93 | export verbose='yes' |
---|
| 94 | ;; |
---|
| 95 | h|*) |
---|
| 96 | usage |
---|
| 97 | exit 3 |
---|
| 98 | ;; |
---|
| 99 | esac |
---|
| 100 | done |
---|
| 101 | shift $((OPTIND - 1)) |
---|
| 102 | [[ $1 = "--" ]] && shift |
---|
| 103 | |
---|
| 104 | cd /tmp/ |
---|
| 105 | export base_path=$(mktemp -d tssh.XXXXXX) |
---|
| 106 | touch "/tmp/${base_path}/master" |
---|
| 107 | touch "/tmp/${base_path}/master-" |
---|
| 108 | touch "/tmp/${base_path}/master+" |
---|
| 109 | touch "/tmp/${base_path}/master--" |
---|
| 110 | touch "/tmp/${base_path}/master++" |
---|
| 111 | |
---|
| 112 | get_host_list () { |
---|
| 113 | local cluster |
---|
| 114 | for host in $* |
---|
| 115 | do |
---|
| 116 | mode="" |
---|
| 117 | last_char="${host: -1}" |
---|
| 118 | if [ "${last_char}" == "-" -o "${last_char}" == "+" ] |
---|
| 119 | then |
---|
| 120 | mode="${last_char}" |
---|
| 121 | host="${host:0:${#host}-1}" |
---|
| 122 | fi |
---|
| 123 | |
---|
[133] | 124 | # short host without login part if any |
---|
| 125 | justhost=${host#*@} |
---|
| 126 | |
---|
| 127 | cluster=$(grep "^${justhost}\b" ${HOME}/.csshrc | cut -f 2 -d '=') |
---|
[125] | 128 | if [ "${cluster}" == "" ] |
---|
| 129 | then |
---|
[133] | 130 | # just a host to scan and add |
---|
[125] | 131 | if [ "${fast}" != 'yes' ] |
---|
| 132 | then |
---|
[133] | 133 | # test if exists host |
---|
| 134 | if host ${justhost} | grep -q 'not found' |
---|
[125] | 135 | then |
---|
[133] | 136 | [ "${verbose}" == 'yes' ] && echo Warning: ${justhost} does not exists |
---|
[125] | 137 | continue |
---|
| 138 | fi |
---|
[133] | 139 | if ! nmap -p 22 -sT -PN ${justhost} | grep -q '\bopen\b' |
---|
[125] | 140 | then |
---|
[133] | 141 | if host ${justhost}.${dyn_domain} | grep -q 'not found' || ! nmap -p 22 -sT -PN ${justhost}.${dyn_domain} | grep -q '\bopen\b' |
---|
[125] | 142 | then |
---|
[133] | 143 | [ "${verbose}" == 'yes' ] && echo Warning: ${justhost} is down |
---|
[125] | 144 | continue |
---|
| 145 | else |
---|
[133] | 146 | [ "${verbose}" == 'yes' ] && echo Warning: remove ssh key of ${justhost}.${dyn_domain} |
---|
| 147 | host=${justhost}.${dyn_domain} |
---|
| 148 | ssh-keygen -q -R $(LANG=C host ${justhost} | awk '{print $4}') |
---|
[125] | 149 | fi |
---|
| 150 | fi |
---|
| 151 | fi |
---|
[133] | 152 | [ "${verbose}" == 'yes' ] && echo Warning: add ${host} on list with mode ${mode} |
---|
[125] | 153 | echo "${host}" >> "/tmp/${base_path}/master${mode}" |
---|
| 154 | else |
---|
[133] | 155 | # cluster, jump in a recursive mode |
---|
| 156 | [ "${verbose}" == 'yes' ] && echo Warning: recursive mode for cluster ${cluster} with mode ${mode} |
---|
[125] | 157 | cluster=$(get_host_list "${cluster}${mode}") |
---|
| 158 | fi |
---|
| 159 | done |
---|
| 160 | } |
---|
| 161 | declare -fx get_host_list |
---|
| 162 | |
---|
| 163 | get_host_list $@ |
---|
| 164 | cat "/tmp/${base_path}/master+" >> "/tmp/${base_path}/master" |
---|
| 165 | for f in $(grep . "/tmp/${base_path}/master-") |
---|
| 166 | do |
---|
| 167 | egrep "^${f}$" "/tmp/${base_path}/master+" && continue |
---|
| 168 | echo "${f}" >> "/tmp/${base_path}/master--" |
---|
| 169 | done |
---|
| 170 | for f in $(grep . "/tmp/${base_path}/master") |
---|
| 171 | do |
---|
| 172 | egrep "^${f}$" "/tmp/${base_path}/master--" && continue |
---|
| 173 | echo "${f}" >> "/tmp/${base_path}/master++" |
---|
| 174 | done |
---|
| 175 | |
---|
[134] | 176 | # split master list in paquet of split_number computer |
---|
| 177 | sort -u "/tmp/${base_path}/master++" | split -l ${split_number} - /tmp/${base_path}/__splitted_ |
---|
[125] | 178 | |
---|
[134] | 179 | # loop on each split windows |
---|
| 180 | for f in $(ls -1 /tmp/${base_path}/ | grep ^__splitted_) |
---|
[125] | 181 | do |
---|
[136] | 182 | if [ "${verbose}" == 'yes' ] |
---|
| 183 | then |
---|
| 184 | echo Info: next host to be splitted |
---|
| 185 | cat "/tmp/${base_path}/${f}" |
---|
| 186 | sleep 5 |
---|
| 187 | fi |
---|
| 188 | |
---|
[125] | 189 | session=$(shuf -n 1 /usr/share/dict/words | tr -cd "[:alpha:]") |
---|
| 190 | |
---|
| 191 | IFS=$'\n' host=($(cat "/tmp/${base_path}/${f}")) |
---|
| 192 | |
---|
[128] | 193 | tmux -2 new-session -d -s $session "ssh ${host[0]} ${remote_command}" |
---|
[135] | 194 | # wait 1s to let new session start... |
---|
| 195 | sleep 1 |
---|
[125] | 196 | |
---|
| 197 | for (( i=1 ; i < ${#host[@]} ; i++)) |
---|
| 198 | do |
---|
[128] | 199 | tmux splitw -t $session "ssh ${host[$i]} ${remote_command}" |
---|
[125] | 200 | tmux select-layout tiled |
---|
| 201 | done |
---|
| 202 | |
---|
| 203 | tmux set-window-option synchronize-panes on > /dev/null |
---|
| 204 | tmux set-window-option -g utf8 on > /dev/null |
---|
| 205 | tmux set -g default-terminal screen-256color > /dev/null |
---|
| 206 | #tmux set-option -g set-clipboard on |
---|
| 207 | |
---|
| 208 | # Sane scrolling |
---|
| 209 | #tmux set -g mode-mouse on |
---|
| 210 | #tmux set -g mouse-resize-pane on |
---|
| 211 | #tmux set -g mouse-select-pane on |
---|
| 212 | #tmux set -g mouse-select-window on |
---|
| 213 | |
---|
| 214 | #set -g terminal-overrides 'xterm*:smcup@:rmcup@' |
---|
| 215 | |
---|
| 216 | # toggle mouse mode to allow mouse copy/paste |
---|
| 217 | # set mouse on with prefix m |
---|
| 218 | tmux bind m \ |
---|
| 219 | set -g mode-mouse on \; \ |
---|
| 220 | set -g mouse-select-pane on \; \ |
---|
| 221 | display 'Mouse: ON' > /dev/null |
---|
| 222 | # set -g mouse-resize-pane on \; \ |
---|
| 223 | #set -g mouse-select-window on \; \ |
---|
| 224 | # set mouse off with prefix M |
---|
| 225 | tmux bind M \ |
---|
| 226 | set -g mode-mouse off \; \ |
---|
| 227 | set -g mouse-select-pane off \; \ |
---|
| 228 | display 'Mouse: OFF' > /dev/null |
---|
| 229 | #set -g mouse-resize-pane off \; \ |
---|
| 230 | #set -g mouse-select-window off \; \ |
---|
| 231 | # toggle Broadcast |
---|
| 232 | tmux bind b set-window-option synchronize-panes |
---|
| 233 | |
---|
| 234 | tmux attach -t $session |
---|
| 235 | done |
---|
| 236 | |
---|
| 237 | # Clean temporary folder |
---|
| 238 | [ -d "/tmp/${base_path}" ] && rm -rf "/tmp/${base_path}" |
---|