source: trunk/tssh/tssh @ 126

Last change on this file since 126 was 126, checked in by g7moreau, 3 years ago
  • Add section DEPEND
File size: 5.5 KB
Line 
1#!/bin/bash
2#
3# 2014/03/26 Gabriel Moreau <Gabriel Moreau(A)univ-grenoble-alpes.fr> - Initial release
4#
5# From http://hd-recording.at/dokuwiki/doku.php?id=linux:tmux#tssh
6
7# Clean when Ctrl^C
8trap '[ -n "${base_path}" -a -d "/tmp/${base_path}" ] && rm -rf "/tmp/${base_path}"' QUIT
9
10export PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin
11export LANG=C
12
13
14function usage() {
15   cat <<END_USAGE
16NAME
17   $(basename $0) - tmux cluster ssh
18
19SYNOPSIS
20   $0 [-w number] [-f] [-v] <host1> <host2> <clusterssh class>... <hostM>- <hostN>+
21
22OPTIONS
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
28DEPEND
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
40AUTHOR
41   Gabriel Moreau
42
43COPYRIGHT
44   Copyright (C) 2014-2017, LEGI UMR 5519 / CNRS UGA G-INP, Grenoble, France
45   Licence : GNU GPL version 2 or later
46END_USAGE
47   }
48
49export split_number=16
50if which tput > /dev/null
51then
52   export split_number=$(( ($(tput  lines)/ 10) * ($(tput  cols)/ 40) ))
53fi
54
55export dyn_domain=''
56if [ -e "${HOME}/.tsshrc" ]
57then
58   . "${HOME}/.tsshrc"
59fi
60
61# get options
62if [ $# -eq 0 ]; then usage; exit 1; fi 
63while getopts "w:fvh" options
64do
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
86done
87shift $((OPTIND - 1))
88[[ $1 = "--" ]] && shift
89
90cd /tmp/
91export base_path=$(mktemp -d tssh.XXXXXX)
92touch "/tmp/${base_path}/master"
93touch "/tmp/${base_path}/master-"
94touch "/tmp/${base_path}/master+"
95touch "/tmp/${base_path}/master--"
96touch "/tmp/${base_path}/master++"
97
98get_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   }
139declare -fx get_host_list
140
141get_host_list $@
142cat "/tmp/${base_path}/master+" >> "/tmp/${base_path}/master"
143for f in $(grep . "/tmp/${base_path}/master-")
144do
145   egrep "^${f}$" "/tmp/${base_path}/master+" && continue
146   echo "${f}" >> "/tmp/${base_path}/master--"
147done
148for f in $(grep . "/tmp/${base_path}/master")
149do
150   egrep "^${f}$" "/tmp/${base_path}/master--" && continue
151   echo "${f}" >> "/tmp/${base_path}/master++"
152done
153
154sort -u "/tmp/${base_path}/master++" | split -l ${split_number} - /tmp/${base_path}/_
155
156for f in $(ls -1 /tmp/${base_path}/ | grep -v ^master)
157do
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
203done
204
205# Clean temporary folder
206[ -d "/tmp/${base_path}" ] && rm -rf "/tmp/${base_path}"
Note: See TracBrowser for help on using the repository browser.