-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathssh-tunnel-manager.sh
More file actions
executable file
·438 lines (394 loc) · 12.1 KB
/
ssh-tunnel-manager.sh
File metadata and controls
executable file
·438 lines (394 loc) · 12.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
#!/bin/bash
#
# @author Gerhard Steinbeis (info [at] tinned-software [dot] net)
# @copyright Copyright (c) 2013
version=0.6.7
# @license http://opensource.org/licenses/GPL-3.0 GNU General Public License, version 3
# @package net
#
# Configuration file for the ssh-tunnel-manager script. Default location for
# the config file is /etc/ssh-tunnel-manager.conf or if that does not exist,
# the ssh-tunnel-manager.conf in the same directory as the script itself.
#
#
# Define names for the tunnel to identify them. The list needs to be configured
# in the same order as the tunnel config in the TUNELS list.
#
#TUNNEL_NAMES=(
# "Tunnel-A"
# "Tunnel-B"
#)
#
# Ths TUNNELS array is used to configure the individual tunnels. Each
# configuration entry needs to follow the SSH options. An example of
# how such a configuration line might look like is listed here.
#
# TUNNELS=(
# "-p 1234 username@host1.example.com -L 10001:127.0.0.1:3306 -L 10011:127.0.0.1:27017"
# "-p 1234 username@host2.example.com -L 10002:127.0.0.1:3306 -L 10012:127.0.0.1:27017"
#)
#
# The RECONNECT_TIMER is used in case of a tunnel connection to be lost. After
# the script is detecting that the connection was lost, the time defined the
# time to wait before the the script tries to reconnect the tunnel.
#
RECONNECT_TIMER=5
#
# The LOGFILE setting defines the path of the logfile. You have the possibility to use
# the $SCRIPT_PATH variable to define the path of the logfile to be the same
# as the script directory.
#
LOGFILE=""
#
# This DBG setting is adding additional details to the logfile. The values are 0
# to hide the extra log content and 1 to show it.
#
DBG=0
#
# Default configuration file to load settings from can be defined here.
#
DEFAULT_CONFIG="ssh-tunnel-manager.conf"
#
# Non Config values
#
# Get script directory
SCRIPT_PATH="$(cd $(dirname $0);pwd -P)"
LOGFILE="$SCRIPT_PATH/ssh-tunnel-manager.log"
#
# Parse all parameters
#
HELP=0
TERMINATE=0
COMMAND_INDEX_NAME=''
while [ $# -gt 0 ]; do
case $1 in
# General parameter
-h|--help)
HELP=1
shift
;;
-v|--version)
echo
echo "Copyright (c) 2013 Tinned-Software (Gerhard Steinbeis)"
echo "License GNUv3: GNU General Public License version 3 <http://opensource.org/licenses/GPL-3.0>"
echo
echo "`basename $0` version $version"
echo
exit 0
;;
# specific parameters
--config)
# load settings file
if [[ -f "$2" ]]; then
. $2
CONFIG_FILE=$2
CONFIG_LOADED=1
else
HELP=1
echo "ERROR: Specified config file cound not be found."
fi
shift 2
;;
# specific parameters
start)
COMMAND='start'
shift
if [[ $# -gt 0 ]]; then
COMMAND_INDEX_NAME=$1
shift
fi
;;
stop)
COMMAND='stop'
shift
if [[ $# -gt 0 ]]; then
COMMAND_INDEX_NAME=$1
shift
fi
;;
restart)
COMMAND='restart'
shift
if [[ $# -gt 0 ]]; then
COMMAND_INDEX_NAME=$1
shift
fi
;;
status)
COMMAND='status'
shift
if [[ $# -gt 0 ]]; then
COMMAND_INDEX_NAME=$1
shift
fi
;;
show)
COMMAND='show'
shift
;;
manage)
COMMAND='manage'
INDEX=$2
shift 2
;;
# undefined parameter
*)
echo "ERROR: Unknown option '$1'"
HELP=1
shift
break
;;
esac
done
# check if a command has been provided
if [[ -z "${COMMAND}" ]]; then
if [ "$HELP" -ne "1" ]; then
echo "ERROR: No command defined in parameter list."
HELP=1
fi
fi
# Load the default configuration file if no other configuration file is provided
if [[ -z "$CONFIG_LOADED" ]]; then
# Check if configuration file exists
if [[ -f "/etc/$DEFAULT_CONFIG" ]]; then
. /etc/$DEFAULT_CONFIG
CONFIG_FILE=/etc/$DEFAULT_CONFIG
else
if [[ -f "$SCRIPT_PATH/$DEFAULT_CONFIG" ]]; then
. $SCRIPT_PATH/$DEFAULT_CONFIG
CONFIG_FILE=$SCRIPT_PATH/$DEFAULT_CONFIG
fi
fi
fi
# check if a tunnel configuration has been provided
if [[ "${#TUNNELS[@]}" -lt "1" && ! -z "${COMMAND}" ]]; then
if [ "$HELP" -ne "1" ]; then
echo "ERROR: No tunnel configuration found."
HELP=1
fi
fi
# show help message
if [ "$HELP" -eq "1" ]; then
echo
echo "Copyright (c) 2013 Tinned-Software (Gerhard Steinbeis)"
echo "License GNUv3: GNU General Public License version 3 <http://opensource.org/licenses/GPL-3.0>"
echo
echo "This script is used to setup multiple ssh tunnels and manage to keep "
echo "them alive. This script will launch manager instances to keep the "
echo "individual tunnels alive. See the configuration file for more details "
echo "about the configuration."
echo
echo "Usage: `basename $0` [-hv] [--config filename.conf] [start|stop|status|restart|show] [tunnel-ID]"
echo " -h --help print this usage and exit"
echo " -v --version print version information and exit"
echo " --config Configuration file to read parameters from"
echo " start [TN] Start the ssh tunnels configured"
echo " stop [TN] Stop the ssh tunnels configured"
echo " status [TN] Check the status of the ssh tunnel"
echo " show [TN] Show the ssh tunnel configuration and there tunnel name"
echo " restart [TN] Restart the ssh tunnels configured"
echo " "
echo " TN Specify one tunnel to start/stop/... identified by its name"
echo
echo
exit 1
fi
#
# Function to print out a string including a time and date info at the
# beginning of the line. If the string is empty, only the timestamp is printed
# out.
#
# @param $1 The string to print out
# @param $2 (optional) Option to echo like ">>logfile.log"
#
function echotime
{
TIME=`date "+[%Y-%m-%d %H:%M:%S]"`
if [ -z "$LOGFILE" ]; then
#stdout for non-privileged user on systemd
echo -e "$TIME - $@"
else
echo -e "$TIME - $@" >>$LOGFILE
fi
}
#
# This function will search the index of the name provided to this function.
#
function get_id_from_name
{
for (( idx=0; idx<${#TUNNEL_NAMES[@]}; idx++ ))
do
if [[ "$1" == "${TUNNEL_NAMES[$idx]}" ]]
then
echo $idx
return
fi
done
echo -1
}
# Execute function signal_terminate() receiving TERM signal
#
# Function to print out a string including a time and date info at the
# beginning of the line. If the string is empty, only the timestamp is printed
# out.
#
# @param $1 The string to print out
# @param $2 (optional) Option to echo like ">>logfile.log"
#
function signal_terminate
{
echotime "MANAGER - Received TERM for tunnel ${TUNNEL_NAMES[$INDEX]} (ID $INDEX)"
TERMINATE=1
}
trap 'signal_terminate' TERM
SCRIPT_PID=$$
#
# start procedure according to the action
#
case $COMMAND in
restart)
echotime "COMM - Execute RESTART procedure ... "
$0 --config $CONFIG_FILE stop $COMMAND_INDEX_NAME
sleep 2
$0 --config $CONFIG_FILE start $COMMAND_INDEX_NAME
echotime "COMM - Execute RESTART procedure ... Done"
echotime ""
;;
stop)
echotime "COMM - Execute STOP procedure ... "
# check if a tunnel index has been provided
if [[ "$COMMAND_INDEX_NAME" != '' ]]; then
COMMAND_INDEX=$(get_id_from_name "$COMMAND_INDEX_NAME")
if [[ "$COMMAND_INDEX" -eq "-1" ]]; then
echo "The tunnel with the name '$COMMAND_INDEX_NAME' can not be found."
exit 6
fi
IDX_START=$COMMAND_INDEX
IDX_END=$((COMMAND_INDEX+1))
else
IDX_START=0
IDX_END=${#TUNNELS[@]}
fi
for (( idx=$IDX_START; idx<$IDX_END; idx++ ));
do
# notify "manage" script of terminate request. This avoids the restart of the tunnel
RESULT_PID=`ps aux | grep -v grep | grep "$0 --config $CONFIG_FILE manage $idx\$" | awk '{print $2}' | tr '\n' ' '`
[ "$DBG" -gt "0" ] && echotime "STOP - *** DBG-CMD: ps aux | grep -v grep | grep \"$0 --config $CONFIG_FILE manage $idx\\\$\" | awk '{print \$2}'"
for PID in $RESULT_PID; do
kill $PID &>/dev/null
done
echotime "STOP - Stop sent manager of tunnel '${TUNNEL_NAMES[$idx]}' (ID $idx) ... PID: $RESULT_PID"
# Terminate the ssh tunnel processes.
RESULT_PID=`ps aux | grep -v grep | grep "ssh -N ${TUNNELS[$idx]}" | awk '{print $2}' | tr '\n' ' '`
[ "$DBG" -gt "0" ] && echotime "STOP - *** DBG-CMD: ps aux | grep -v grep | grep \"ssh -N ${TUNNELS[$idx]}\" | awk '{print \$2}'"
for PID in $RESULT_PID; do
kill $PID &>/dev/null
done
echotime "STOP - Stopped tunnel '${TUNNEL_NAMES[$idx]}' (ID $idx) ... PID: $RESULT_PID"
# check if the tunnels really down
sleep "0.3"
TUNNELS_COUNT=0
TMANAGER_COUNT=0
TUNNELS_COUNT=`ps aux | grep -v grep | grep "ssh -N ${TUNNELS[$idx]}" | awk '{print $2}' | wc -l`
TMANAGER_COUNT=`ps aux | grep -v grep | grep "$0 --config $CONFIG_FILE manage $idx\$" | awk '{print $2}' | wc -l`
if [[ "$TUNNELS_COUNT" -lt "1" ]] && [[ "$TUNNELS_COUNT" -lt "1" ]]; then
echo "Stopping tunnel '${TUNNEL_NAMES[$idx]}' ... Done"
else
echo "Stopping tunnel '${TUNNEL_NAMES[$idx]}' ... Failed"
fi
done
echotime "COMM - Execute STOP procedure ... Done"
echotime ""
;;
status)
echotime "COMM - Execute STATUS procedure ... "
# check if a tunnel index has been provided
if [[ "$COMMAND_INDEX_NAME" != '' ]]; then
COMMAND_INDEX=$(get_id_from_name "$COMMAND_INDEX_NAME")
if [[ "$COMMAND_INDEX" -eq "-1" ]]; then
echo "The tunnel with the name '$COMMAND_INDEX_NAME' can not be found."
exit 3
fi
IDX_START=$COMMAND_INDEX
IDX_END=$((COMMAND_INDEX+1))
else
IDX_START=0
IDX_END=${#TUNNELS[@]}
fi
EXIT_CODE=0
for (( idx=$IDX_START; idx<$IDX_END; idx++ ));
do
# get the list of processs
RESULT=0
RESULT=`ps aux | grep -v grep | grep "ssh -N ${TUNNELS[$idx]}" | wc -l`
# show the result
if [[ "$RESULT" -gt "0" ]]; then
echotime "STATUS - Status of Tunnel '${TUNNEL_NAMES[$idx]}' (ID $idx) is ... running"
echo "Status of Tunnel '${TUNNEL_NAMES[$idx]}' is ... running"
else
echotime "STATUS - Status of Tunnel '${TUNNEL_NAMES[$idx]}' (ID $idx) is ... NOT running"
echo "Status of Tunnel '${TUNNEL_NAMES[$idx]}' is ... NOT running"
EXIT_CODE=3
fi
done
echotime "COMM - Execute STATUS procedure ... Done"
echotime ""
exit $EXIT_CODE
;;
show)
for (( idx=0; idx<${#TUNNELS[@]}; idx++ ));
do
# show the tunnel config
echo "Configuration of Tunnel '${TUNNEL_NAMES[$idx]}' ... ${TUNNELS[$idx]}"
done
;;
start)
echotime "COMM - Execute START procedure ... "
# check if a tunnel index has been provided
if [[ "$COMMAND_INDEX_NAME" != '' ]]; then
COMMAND_INDEX=$(get_id_from_name "$COMMAND_INDEX_NAME")
if [[ "$COMMAND_INDEX" -eq "-1" ]]; then
echo "The tunnel with the name '$COMMAND_INDEX_NAME' can not be found."
exit 6
fi
IDX_START=$COMMAND_INDEX
IDX_END=$((COMMAND_INDEX+1))
else
IDX_START=0
IDX_END=${#TUNNELS[@]}
fi
for (( idx=$IDX_START; idx<$IDX_END; idx++ ));
do
RESULT_PID=0
RESULT_PID=`ps aux | grep -v grep | grep "$0 --config $CONFIG_FILE manage $idx\$" | awk '{print $2}' | tr '\n' ' '`
if [[ ! -z $RESULT_PID ]]; then
echotime "START - Already running tunnel '${TUNNEL_NAMES[$idx]}' (ID $idx) ... PID: $RESULT_PID"
echo "Starting tunnel '${TUNNEL_NAMES[$idx]}' ... Already running"
else
$0 --config $CONFIG_FILE manage $idx &
sleep "0.2"
RESULT_PID=`ps aux | grep -v grep | grep "$0 --config $CONFIG_FILE manage $idx\$" | awk '{print $2}' | tr '\n' ' '`
[ "$DBG" -gt "0" ] && echotime "START - *** DBG-CMD: ps aux | grep -v grep | grep \"$0 --config $CONFIG_FILE manage $idx\\\$\" | awk '{print \$2}'"
echotime "START - Starting tunnel '${TUNNEL_NAMES[$idx]}' (ID $idx) ... PID: $RESULT_PID"
echo "Starting tunnel '${TUNNEL_NAMES[$idx]}' ... Done"
fi
# sleep before every cycle to aviod overloading
done
echotime "COMM - Execute START procedure ... Done"
echotime ""
;;
manage)
echotime "MANAGE - Connecting tunnel ${TUNNEL_NAMES[$INDEX]} (ID $INDEX) with parameters ... ${TUNNELS[$INDEX]}"
while [ "$TERMINATE" -eq "0" ]
do
SSH_RESULT=`ssh -N ${TUNNELS[$INDEX]} 2>&1`
if [[ "$TERMINATE" -eq "0" ]]; then
echotime "MANAGE - Detected tunnel '${TUNNEL_NAMES[$INDEX]}' (ID $INDEX) disconnected. $SSH_RESULT"
sleep $RECONNECT_TIMER
echotime "MANAGE - Reconnecting tunnel '${TUNNEL_NAMES[$INDEX]}' (ID $INDEX) with parameters ... ${TUNNELS[$INDEX]}"
else
echotime "MANAGE - Shutdown manager for tunnel '${TUNNEL_NAMES[$INDEX]}' (ID $INDEX) "
fi
done
;;
esac