[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 | |
---|
[126] | 28 | DEPEND |
---|
| 29 | On Debian, you need the package |
---|
| 30 | |
---|
| 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 | |
---|
[125] | 40 | AUTHOR |
---|
| 41 | Gabriel Moreau |
---|
| 42 | |
---|
| 43 | COPYRIGHT |
---|
| 44 | Copyright (C) 2014-2017, LEGI UMR 5519 / CNRS UGA G-INP, Grenoble, France |
---|
| 45 | Licence : GNU GPL version 2 or later |
---|
| 46 | END_USAGE |
---|
| 47 | } |
---|
| 48 | |
---|
| 49 | export split_number=16 |
---|
| 50 | if which tput > /dev/null |
---|
| 51 | then |
---|
| 52 | export split_number=$(( ($(tput lines)/ 10) * ($(tput cols)/ 40) )) |
---|
| 53 | fi |
---|
| 54 | |
---|
| 55 | export dyn_domain='' |
---|
| 56 | if [ -e "${HOME}/.tsshrc" ] |
---|
| 57 | then |
---|
| 58 | . "${HOME}/.tsshrc" |
---|
| 59 | fi |
---|
| 60 | |
---|
| 61 | # get options |
---|
| 62 | if [ $# -eq 0 ]; then usage; exit 1; fi |
---|
| 63 | while getopts "w:fvh" options |
---|
| 64 | do |
---|
| 65 | case ${options} in |
---|
| 66 | w) |
---|
| 67 | if echo ${OPTARG} | egrep -q '^[[:digit:]]+$' && [ ${OPTARG} -gt 0 ] |
---|
| 68 | then |
---|
| 69 | export split_number=${OPTARG} |
---|
| 70 | else |
---|
| 71 | usage |
---|
| 72 | exit 2 |
---|
| 73 | fi |
---|
| 74 | ;; |
---|
| 75 | f) |
---|
| 76 | export fast='yes' |
---|
| 77 | ;; |
---|
| 78 | v) |
---|
| 79 | export verbose='yes' |
---|
| 80 | ;; |
---|
| 81 | h|*) |
---|
| 82 | usage |
---|
| 83 | exit 3 |
---|
| 84 | ;; |
---|
| 85 | esac |
---|
| 86 | done |
---|
| 87 | shift $((OPTIND - 1)) |
---|
| 88 | [[ $1 = "--" ]] && shift |
---|
| 89 | |
---|
| 90 | cd /tmp/ |
---|
| 91 | export base_path=$(mktemp -d tssh.XXXXXX) |
---|
| 92 | touch "/tmp/${base_path}/master" |
---|
| 93 | touch "/tmp/${base_path}/master-" |
---|
| 94 | touch "/tmp/${base_path}/master+" |
---|
| 95 | touch "/tmp/${base_path}/master--" |
---|
| 96 | touch "/tmp/${base_path}/master++" |
---|
| 97 | |
---|
| 98 | get_host_list () { |
---|
| 99 | local cluster |
---|
| 100 | for host in $* |
---|
| 101 | do |
---|
| 102 | mode="" |
---|
| 103 | last_char="${host: -1}" |
---|
| 104 | if [ "${last_char}" == "-" -o "${last_char}" == "+" ] |
---|
| 105 | then |
---|
| 106 | mode="${last_char}" |
---|
| 107 | host="${host:0:${#host}-1}" |
---|
| 108 | fi |
---|
| 109 | |
---|
| 110 | cluster=$(grep "^${host}" ${HOME}/.csshrc | cut -f 2 -d '=') |
---|
| 111 | if [ "${cluster}" == "" ] |
---|
| 112 | then |
---|
| 113 | if [ "${fast}" != 'yes' ] |
---|
| 114 | then |
---|
| 115 | # short host without login part if any |
---|
| 116 | if host ${host#*@} | grep -q 'not found' |
---|
| 117 | then |
---|
| 118 | [ "${verbose}" == "yes" ] && echo Warning: ${host#*@} does not exists |
---|
| 119 | continue |
---|
| 120 | fi |
---|
| 121 | if ! nmap -p 22 -sT -PN ${host#*@} | grep -q '\bopen\b' |
---|
| 122 | then |
---|
| 123 | if host ${host#*@}.${dyn_domain} | grep -q 'not found' || ! nmap -p 22 -sT -PN ${host#*@}.${dyn_domain} | grep -q '\bopen\b' |
---|
| 124 | then |
---|
| 125 | [ "${verbose}" == "yes" ] && echo Warning: ${host#*@} is down |
---|
| 126 | continue |
---|
| 127 | else |
---|
| 128 | host=${host}.${dyn_domain} |
---|
| 129 | ssh-keygen -q -R $(LANG=C host ${host} | awk '{print $4}') |
---|
| 130 | fi |
---|
| 131 | fi |
---|
| 132 | fi |
---|
| 133 | echo "${host}" >> "/tmp/${base_path}/master${mode}" |
---|
| 134 | else |
---|
| 135 | cluster=$(get_host_list "${cluster}${mode}") |
---|
| 136 | fi |
---|
| 137 | done |
---|
| 138 | } |
---|
| 139 | declare -fx get_host_list |
---|
| 140 | |
---|
| 141 | get_host_list $@ |
---|
| 142 | cat "/tmp/${base_path}/master+" >> "/tmp/${base_path}/master" |
---|
| 143 | for f in $(grep . "/tmp/${base_path}/master-") |
---|
| 144 | do |
---|
| 145 | egrep "^${f}$" "/tmp/${base_path}/master+" && continue |
---|
| 146 | echo "${f}" >> "/tmp/${base_path}/master--" |
---|
| 147 | done |
---|
| 148 | for f in $(grep . "/tmp/${base_path}/master") |
---|
| 149 | do |
---|
| 150 | egrep "^${f}$" "/tmp/${base_path}/master--" && continue |
---|
| 151 | echo "${f}" >> "/tmp/${base_path}/master++" |
---|
| 152 | done |
---|
| 153 | |
---|
| 154 | sort -u "/tmp/${base_path}/master++" | split -l ${split_number} - /tmp/${base_path}/_ |
---|
| 155 | |
---|
| 156 | for f in $(ls -1 /tmp/${base_path}/ | grep -v ^master) |
---|
| 157 | do |
---|
| 158 | session=$(shuf -n 1 /usr/share/dict/words | tr -cd "[:alpha:]") |
---|
| 159 | |
---|
| 160 | IFS=$'\n' host=($(cat "/tmp/${base_path}/${f}")) |
---|
| 161 | |
---|
| 162 | tmux -2 new-session -d -s $session "ssh ${host[0]}" |
---|
| 163 | |
---|
| 164 | |
---|
| 165 | for (( i=1 ; i < ${#host[@]} ; i++)) |
---|
| 166 | do |
---|
| 167 | tmux splitw -t $session "ssh ${host[$i]}" |
---|
| 168 | tmux select-layout tiled |
---|
| 169 | done |
---|
| 170 | |
---|
| 171 | tmux set-window-option synchronize-panes on > /dev/null |
---|
| 172 | tmux set-window-option -g utf8 on > /dev/null |
---|
| 173 | tmux set -g default-terminal screen-256color > /dev/null |
---|
| 174 | #tmux set-option -g set-clipboard on |
---|
| 175 | |
---|
| 176 | # Sane scrolling |
---|
| 177 | #tmux set -g mode-mouse on |
---|
| 178 | #tmux set -g mouse-resize-pane on |
---|
| 179 | #tmux set -g mouse-select-pane on |
---|
| 180 | #tmux set -g mouse-select-window on |
---|
| 181 | |
---|
| 182 | #set -g terminal-overrides 'xterm*:smcup@:rmcup@' |
---|
| 183 | |
---|
| 184 | # toggle mouse mode to allow mouse copy/paste |
---|
| 185 | # set mouse on with prefix m |
---|
| 186 | tmux bind m \ |
---|
| 187 | set -g mode-mouse on \; \ |
---|
| 188 | set -g mouse-select-pane on \; \ |
---|
| 189 | display 'Mouse: ON' > /dev/null |
---|
| 190 | # set -g mouse-resize-pane on \; \ |
---|
| 191 | #set -g mouse-select-window on \; \ |
---|
| 192 | # set mouse off with prefix M |
---|
| 193 | tmux bind M \ |
---|
| 194 | set -g mode-mouse off \; \ |
---|
| 195 | set -g mouse-select-pane off \; \ |
---|
| 196 | display 'Mouse: OFF' > /dev/null |
---|
| 197 | #set -g mouse-resize-pane off \; \ |
---|
| 198 | #set -g mouse-select-window off \; \ |
---|
| 199 | # toggle Broadcast |
---|
| 200 | tmux bind b set-window-option synchronize-panes |
---|
| 201 | |
---|
| 202 | tmux attach -t $session |
---|
| 203 | done |
---|
| 204 | |
---|
| 205 | # Clean temporary folder |
---|
| 206 | [ -d "/tmp/${base_path}" ] && rm -rf "/tmp/${base_path}" |
---|