[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 |
---|
[144] | 8 | trap '[ -n "${base_path}" -a -d "/tmp/${base_path}" ] && rm -rf "/tmp/${base_path}"; exit 4;' QUIT INT TERM |
---|
[125] | 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 |
---|
[390] | 20 | $0 [-w number] [-f] [-v] [-c remote_cmd] [-o ssh_option] <host1> <host2> <clusterssh class>... <hostM>- <hostN>+ |
---|
[125] | 21 | |
---|
| 22 | OPTIONS |
---|
[416] | 23 | -w windows windows to open (integer, default 16) |
---|
[392] | 24 | -o ssh_option option to pass to ssh |
---|
[197] | 25 | -f fast, no nmap scan to eliminate sleeping computer |
---|
| 26 | -v verbose |
---|
| 27 | -c remote_cmd launch the remote command on hosts and exit |
---|
[416] | 28 | -p launch in parallel (only with -c) with command parallel or xargs |
---|
[197] | 29 | -h help |
---|
[125] | 30 | |
---|
[197] | 31 | DESCRIPTION |
---|
| 32 | tssh can be use to launch terminal on many computer in parallel with tmux |
---|
| 33 | multiplexer and ssh. |
---|
| 34 | The tmux windows is splitted automatically. |
---|
| 35 | If you need more computers on the same windows, you can zoom in and out |
---|
| 36 | under gnome terminal with Ctrl- or Ctrl+. |
---|
| 37 | This must be done before launching tssh. |
---|
| 38 | |
---|
| 39 | On the command line, you can put host, login@host, clusterssh class. |
---|
| 40 | A host or a class can be remove from the list with a dash append |
---|
| 41 | and force to be in this one with a plus append. |
---|
| 42 | Example with the cluster ssh config below: |
---|
| 43 | |
---|
| 44 | tssh all team- node005 laptop04+ |
---|
| 45 | |
---|
| 46 | Is equivalent to: |
---|
| 47 | |
---|
| 48 | tssh srv-mail srv-dns srv-imap srv-web srv-proxy \\ |
---|
| 49 | node001 node002 node003 node004 \\ |
---|
| 50 | node101 node102 node103 node104 \\ |
---|
| 51 | node005 laptop04 |
---|
| 52 | |
---|
| 53 | The control command for tmux is Ctrl^b. |
---|
| 54 | You can switch from broadcast to a local machine with Ctrl^b Ctrl^b |
---|
| 55 | and move between machine with Ctrl^b ArrowKey. |
---|
| 56 | |
---|
[128] | 57 | DEPENDS |
---|
[126] | 58 | On Debian, you need the package |
---|
[128] | 59 | |
---|
[126] | 60 | apt-get install tmux ncurses-bin wamerican nmap |
---|
| 61 | |
---|
| 62 | wamerican (or wfrench...) is used to choose a random word in the file /usr/share/dict/words |
---|
| 63 | for each new tmux session. |
---|
[197] | 64 | |
---|
[126] | 65 | ncurses-bin is required for the tput command |
---|
| 66 | to automatically split your terminal into several small panels. |
---|
| 67 | nmap is only used for dynamic DNS domain and dynamic scan. |
---|
| 68 | This is not mandatory for general use. |
---|
| 69 | |
---|
[128] | 70 | By default, tssh use tput to know the number of columns and lines of your terminal. |
---|
| 71 | It takes 10 lines and 40 columns for each windows by default. |
---|
| 72 | If tput is not installed, the default is 16 windows... |
---|
| 73 | |
---|
[197] | 74 | CONFIGURATION |
---|
| 75 | The clusterssh config file ~/.csshrc is a key values file. |
---|
| 76 | The "clusters" is mandatory for clusterssh (not tssh) and define the other keys. |
---|
| 77 | Values could be computer list or other key... |
---|
| 78 | |
---|
| 79 | clusters = all server s1 s2 s3 node n1 n2 team switch |
---|
| 80 | all = server node team |
---|
| 81 | server = s1 s2 |
---|
| 82 | node = n1 n2 |
---|
| 83 | s1 = srv-mail srv-dns srv-imap |
---|
| 84 | s2 = srv-web srv-proxy |
---|
| 85 | n1 = node001 node002 node003 node004 |
---|
| 86 | n2 = node101 node102 node103 node104 |
---|
| 87 | team = pc01 pc06 laptop04 laptop05 laptop09 |
---|
| 88 | switch = root@switch01 root@switch05 root@switch17 |
---|
| 89 | |
---|
| 90 | The tssh config file (~/.tsshrc) can be use change the default parameters. |
---|
| 91 | |
---|
| 92 | #export split_number=16 |
---|
| 93 | export dyn_domain='mycompagny.local' |
---|
[392] | 94 | #export ssh_option='' |
---|
[197] | 95 | #export fast='yes' |
---|
| 96 | #export verbose='yes' |
---|
| 97 | |
---|
[125] | 98 | AUTHOR |
---|
| 99 | Gabriel Moreau |
---|
| 100 | |
---|
| 101 | COPYRIGHT |
---|
[390] | 102 | Copyright (C) 2014-2019, LEGI UMR 5519 / CNRS UGA G-INP, Grenoble, France |
---|
[125] | 103 | Licence : GNU GPL version 2 or later |
---|
| 104 | END_USAGE |
---|
| 105 | } |
---|
| 106 | |
---|
[128] | 107 | export remote_command='' |
---|
[390] | 108 | export ssh_option='' |
---|
[125] | 109 | export split_number=16 |
---|
| 110 | if which tput > /dev/null |
---|
| 111 | then |
---|
| 112 | export split_number=$(( ($(tput lines)/ 10) * ($(tput cols)/ 40) )) |
---|
| 113 | fi |
---|
| 114 | |
---|
| 115 | export dyn_domain='' |
---|
| 116 | if [ -e "${HOME}/.tsshrc" ] |
---|
| 117 | then |
---|
| 118 | . "${HOME}/.tsshrc" |
---|
| 119 | fi |
---|
| 120 | |
---|
| 121 | # get options |
---|
| 122 | if [ $# -eq 0 ]; then usage; exit 1; fi |
---|
[416] | 123 | while getopts "w:o:c:fvph" options |
---|
[125] | 124 | do |
---|
| 125 | case ${options} in |
---|
| 126 | w) |
---|
| 127 | if echo ${OPTARG} | egrep -q '^[[:digit:]]+$' && [ ${OPTARG} -gt 0 ] |
---|
| 128 | then |
---|
| 129 | export split_number=${OPTARG} |
---|
| 130 | else |
---|
| 131 | usage |
---|
| 132 | exit 2 |
---|
| 133 | fi |
---|
| 134 | ;; |
---|
[128] | 135 | c) |
---|
| 136 | if echo ${OPTARG} | egrep -q '[[:alpha:]]' |
---|
| 137 | then |
---|
| 138 | export remote_command=${OPTARG} |
---|
| 139 | else |
---|
| 140 | usage |
---|
| 141 | exit 2 |
---|
[416] | 142 | fi |
---|
[128] | 143 | ;; |
---|
[416] | 144 | p) |
---|
| 145 | export parallel='yes' |
---|
| 146 | ;; |
---|
[390] | 147 | o) |
---|
| 148 | if echo ${OPTARG} | egrep -q '[[:alpha:][:digit:]]' |
---|
| 149 | then |
---|
| 150 | export ssh_option=${OPTARG} |
---|
| 151 | else |
---|
| 152 | usage |
---|
| 153 | exit 2 |
---|
| 154 | fi |
---|
| 155 | ;; |
---|
[125] | 156 | f) |
---|
| 157 | export fast='yes' |
---|
| 158 | ;; |
---|
| 159 | v) |
---|
| 160 | export verbose='yes' |
---|
| 161 | ;; |
---|
| 162 | h|*) |
---|
| 163 | usage |
---|
| 164 | exit 3 |
---|
| 165 | ;; |
---|
| 166 | esac |
---|
| 167 | done |
---|
| 168 | shift $((OPTIND - 1)) |
---|
| 169 | [[ $1 = "--" ]] && shift |
---|
| 170 | |
---|
| 171 | cd /tmp/ |
---|
| 172 | export base_path=$(mktemp -d tssh.XXXXXX) |
---|
| 173 | touch "/tmp/${base_path}/master" |
---|
| 174 | touch "/tmp/${base_path}/master-" |
---|
| 175 | touch "/tmp/${base_path}/master+" |
---|
| 176 | touch "/tmp/${base_path}/master--" |
---|
| 177 | touch "/tmp/${base_path}/master++" |
---|
| 178 | |
---|
| 179 | get_host_list () { |
---|
| 180 | local cluster |
---|
[137] | 181 | local default_mode='' |
---|
| 182 | |
---|
| 183 | # set local mode |
---|
| 184 | if echo $1 | grep -- ^--mode= |
---|
| 185 | then |
---|
| 186 | default_mode=$(echo $1 | grep -- ^--mode= | cut -f 2 -d '=') |
---|
| 187 | shift |
---|
| 188 | fi |
---|
| 189 | |
---|
[125] | 190 | for host in $* |
---|
| 191 | do |
---|
[137] | 192 | local mode=${default_mode} |
---|
[138] | 193 | local last_char="${host: -1}" |
---|
[125] | 194 | if [ "${last_char}" == "-" -o "${last_char}" == "+" ] |
---|
| 195 | then |
---|
| 196 | mode="${last_char}" |
---|
| 197 | host="${host:0:${#host}-1}" |
---|
| 198 | fi |
---|
| 199 | |
---|
[133] | 200 | # short host without login part if any |
---|
[138] | 201 | local justhost=${host#*@} |
---|
[133] | 202 | |
---|
[143] | 203 | cluster=$(grep "^${justhost}\b" ${HOME}/.csshrc | cut -f 2 -d '=' | sed -e 's/^[[:space:]]*//;') |
---|
[125] | 204 | if [ "${cluster}" == "" ] |
---|
| 205 | then |
---|
[133] | 206 | # just a host to scan and add |
---|
[139] | 207 | if [ "${fast}" != 'yes' -a "${mode}" != '-' ] |
---|
[125] | 208 | then |
---|
[133] | 209 | # test if exists host |
---|
| 210 | if host ${justhost} | grep -q 'not found' |
---|
[125] | 211 | then |
---|
[142] | 212 | [ "${verbose}" == 'yes' ] && echo "Warning: ${justhost} does not exists" |
---|
[125] | 213 | continue |
---|
| 214 | fi |
---|
[133] | 215 | if ! nmap -p 22 -sT -PN ${justhost} | grep -q '\bopen\b' |
---|
[125] | 216 | then |
---|
[133] | 217 | if host ${justhost}.${dyn_domain} | grep -q 'not found' || ! nmap -p 22 -sT -PN ${justhost}.${dyn_domain} | grep -q '\bopen\b' |
---|
[125] | 218 | then |
---|
[142] | 219 | [ "${verbose}" == 'yes' ] && echo "Warning: ${justhost} is down" |
---|
[125] | 220 | continue |
---|
| 221 | else |
---|
[142] | 222 | [ "${verbose}" == 'yes' ] && echo "Warning: remove ssh key of ${justhost}.${dyn_domain}" |
---|
[133] | 223 | host=${justhost}.${dyn_domain} |
---|
| 224 | ssh-keygen -q -R $(LANG=C host ${justhost} | awk '{print $4}') |
---|
[125] | 225 | fi |
---|
| 226 | fi |
---|
| 227 | fi |
---|
[142] | 228 | [ "${verbose}" == 'yes' ] && echo "Warning: add ${host} on list with mode ${mode}" |
---|
[125] | 229 | echo "${host}" >> "/tmp/${base_path}/master${mode}" |
---|
| 230 | else |
---|
[133] | 231 | # cluster, jump in a recursive mode |
---|
[142] | 232 | [ "${verbose}" == 'yes' ] && echo "Warning: recursive call for cluster ${justhost} (${cluster}), with mode ${mode}" |
---|
[137] | 233 | cluster=$(get_host_list --mode=${mode} "${cluster}") |
---|
[125] | 234 | fi |
---|
| 235 | done |
---|
| 236 | } |
---|
| 237 | declare -fx get_host_list |
---|
| 238 | |
---|
| 239 | get_host_list $@ |
---|
| 240 | cat "/tmp/${base_path}/master+" >> "/tmp/${base_path}/master" |
---|
| 241 | for f in $(grep . "/tmp/${base_path}/master-") |
---|
| 242 | do |
---|
| 243 | egrep "^${f}$" "/tmp/${base_path}/master+" && continue |
---|
| 244 | echo "${f}" >> "/tmp/${base_path}/master--" |
---|
| 245 | done |
---|
| 246 | for f in $(grep . "/tmp/${base_path}/master") |
---|
| 247 | do |
---|
| 248 | egrep "^${f}$" "/tmp/${base_path}/master--" && continue |
---|
| 249 | echo "${f}" >> "/tmp/${base_path}/master++" |
---|
| 250 | done |
---|
| 251 | |
---|
[416] | 252 | if [ "${parallel}" == 'yes' -a -n "${remote_command}" ] |
---|
| 253 | then |
---|
[417] | 254 | if parallel --version 2> /dev/null | grep -q ^'GNU parallel' |
---|
[416] | 255 | then |
---|
[417] | 256 | sort -u "/tmp/${base_path}/master++" | parallel -j ${split_number} -I {} -- "ssh ${ssh_option} {} ${remote_command}" |
---|
[416] | 257 | else |
---|
[417] | 258 | sort -u "/tmp/${base_path}/master++" | xargs -r -n 1 -P ${split_number} -I {} -- ssh ${ssh_option} {} "${remote_command}" |
---|
[416] | 259 | fi |
---|
| 260 | |
---|
| 261 | # Clean temporary folder |
---|
| 262 | [ -d "/tmp/${base_path}" ] && rm -rf "/tmp/${base_path}" |
---|
| 263 | |
---|
| 264 | exit 0 |
---|
| 265 | fi |
---|
| 266 | |
---|
| 267 | |
---|
[134] | 268 | # split master list in paquet of split_number computer |
---|
| 269 | sort -u "/tmp/${base_path}/master++" | split -l ${split_number} - /tmp/${base_path}/__splitted_ |
---|
[125] | 270 | |
---|
[148] | 271 | # wait is needed by time tmux session open and ssh time connection |
---|
| 272 | tempo=0.8 |
---|
| 273 | first_tempo=0 |
---|
| 274 | other_tempo=0 |
---|
| 275 | if [ -n "${remote_command}" ] |
---|
| 276 | then |
---|
| 277 | # add tempo after remote command |
---|
| 278 | other_tempo=${tempo} |
---|
[149] | 279 | first_tempo="${tempo} ${other_tempo}" |
---|
[148] | 280 | fi |
---|
| 281 | |
---|
[134] | 282 | # loop on each split windows |
---|
| 283 | for f in $(ls -1 /tmp/${base_path}/ | grep ^__splitted_) |
---|
[125] | 284 | do |
---|
[136] | 285 | if [ "${verbose}" == 'yes' ] |
---|
| 286 | then |
---|
[142] | 287 | echo "Info: next hosts to be splitted" |
---|
[140] | 288 | cat "/tmp/${base_path}/${f}" | sed -e 's/^/ /;' |
---|
[136] | 289 | sleep 5 |
---|
| 290 | fi |
---|
| 291 | |
---|
[138] | 292 | session=$(shuf -n 1 /usr/share/dict/words | tr -cd "[:alpha:]") |
---|
[125] | 293 | |
---|
| 294 | IFS=$'\n' host=($(cat "/tmp/${base_path}/${f}")) |
---|
| 295 | |
---|
[390] | 296 | tmux -2 new-session -d -s $session "ssh ${ssh_option} ${host[0]} ${remote_command}; sleep ${first_tempo}" |
---|
[148] | 297 | # wait ${tempo} second to let new session start... |
---|
| 298 | sleep ${tempo} |
---|
[125] | 299 | |
---|
| 300 | for (( i=1 ; i < ${#host[@]} ; i++)) |
---|
| 301 | do |
---|
[148] | 302 | # wait ${tempo} needed in case of ${remote_command} |
---|
[392] | 303 | tmux splitw -t $session "ssh ${ssh_option} ${host[$i]} ${remote_command}; sleep ${other_tempo}" |
---|
[125] | 304 | tmux select-layout tiled |
---|
| 305 | done |
---|
| 306 | |
---|
| 307 | tmux set-window-option synchronize-panes on > /dev/null |
---|
| 308 | tmux set-window-option -g utf8 on > /dev/null |
---|
| 309 | tmux set -g default-terminal screen-256color > /dev/null |
---|
| 310 | #tmux set-option -g set-clipboard on |
---|
| 311 | |
---|
| 312 | # Sane scrolling |
---|
| 313 | #tmux set -g mode-mouse on |
---|
| 314 | #tmux set -g mouse-resize-pane on |
---|
| 315 | #tmux set -g mouse-select-pane on |
---|
| 316 | #tmux set -g mouse-select-window on |
---|
| 317 | |
---|
| 318 | #set -g terminal-overrides 'xterm*:smcup@:rmcup@' |
---|
| 319 | |
---|
| 320 | # toggle mouse mode to allow mouse copy/paste |
---|
| 321 | # set mouse on with prefix m |
---|
| 322 | tmux bind m \ |
---|
| 323 | set -g mode-mouse on \; \ |
---|
| 324 | set -g mouse-select-pane on \; \ |
---|
| 325 | display 'Mouse: ON' > /dev/null |
---|
| 326 | # set -g mouse-resize-pane on \; \ |
---|
| 327 | #set -g mouse-select-window on \; \ |
---|
| 328 | # set mouse off with prefix M |
---|
| 329 | tmux bind M \ |
---|
| 330 | set -g mode-mouse off \; \ |
---|
| 331 | set -g mouse-select-pane off \; \ |
---|
| 332 | display 'Mouse: OFF' > /dev/null |
---|
| 333 | #set -g mouse-resize-pane off \; \ |
---|
| 334 | #set -g mouse-select-window off \; \ |
---|
| 335 | # toggle Broadcast |
---|
| 336 | tmux bind b set-window-option synchronize-panes |
---|
| 337 | |
---|
| 338 | tmux attach -t $session |
---|
| 339 | done |
---|
| 340 | |
---|
| 341 | # Clean temporary folder |
---|
| 342 | [ -d "/tmp/${base_path}" ] && rm -rf "/tmp/${base_path}" |
---|