forked from flaupretre/terraform-ssh-tunnel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtunnel.sh
executable file
·91 lines (76 loc) · 2.94 KB
/
tunnel.sh
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
MPID="$1"
ret=0
#---
if [ -z "$MPID" ] ; then
if [ -n "$TUNNEL_DEBUG" ] ; then
exec 2>/tmp/t1
set -x
env >&2
fi
ABSPATH=$(cd "$(dirname "$0")"; pwd -P)
query="`dd 2>/dev/null`"
[ -n "$TUNNEL_DEBUG" ] && echo "query: <$query>" >&2
export TIMEOUT="`echo $query | sed -e 's/^.*\"timeout\": *\"//' -e 's/\".*$//g'`"
export SSH_CMD="`echo $query | sed -e 's/^.*\"ssh_cmd\": *\"//' -e 's/\",.*$//g' -e 's/\\\"/\"/g'`"
export LOCAL_HOST="`echo $query | sed -e 's/^.*\"local_host\": *\"//' -e 's/\".*$//g'`"
export LOCAL_PORT="`echo $query | sed -e 's/^.*\"local_port\": *\"//' -e 's/\".*$//g'`"
export TARGET_HOST="`echo $query | sed -e 's/^.*\"target_host\": *\"//' -e 's/\".*$//g'`"
export TARGET_PORT="`echo $query | sed -e 's/^.*\"target_port\": *\"//' -e 's/\".*$//g'`"
export GATEWAY_HOST="`echo $query | sed -e 's/^.*\"gateway_host\": *\"//' -e 's/\".*$//g'`"
export GATEWAY_PORT="`echo $query | sed -e 's/^.*\"gateway_port\": *\"//' -e 's/\".*$//g'`"
export GATEWAY_USER="`echo $query | sed -e 's/^.*\"gateway_user\": *\"//' -e 's/\".*$//g'`"
export SHELL_CMD="`echo $query | sed -e 's/^.*\"shell_cmd\": *\"//' -e 's/\",.*$//g' -e 's/\\\"/\"/g'`"
export SSH_TUNNEL_CHECK_SLEEP="`echo $query | sed -e 's/^.*\"ssh_tunnel_check_sleep\": *\"//' -e 's/\",.*$//g' -e 's/\\\"/\"/g'`"
export SSH_PARENT_WAIT_SLEEP="`echo $query | sed -e 's/^.*\"ssh_parent_wait_sleep\": *\"//' -e 's/\",.*$//g' -e 's/\\\"/\"/g'`"
export CREATE="`echo $query | sed -e 's/^.*\"create\": *\"//' -e 's/\",.*$//g' -e 's/\\\"/\"/g'`"
if [ "X$CREATE" = X -o "X$GATEWAY_HOST" = X ] ; then
# No tunnel - connect directly to target host
do_tunnel=''
cnx_host="$TARGET_HOST"
cnx_port="$TARGET_PORT"
else
do_tunnel='y'
cnx_host="$LOCAL_HOST"
cnx_port="$LOCAL_PORT"
fi
echo "{ \"host\": \"$cnx_host\", \"port\": \"$cnx_port\" }"
if [ -n "$do_tunnel" ] ; then
p=`ps -p $PPID -o "ppid="`
clog=`mktemp`
nohup timeout $TIMEOUT $SHELL_CMD "$ABSPATH/tunnel.sh" $p <&- >&- 2>$clog &
CPID=$!
# A little time for the SSH tunnel process to start or fail
sleep $SSH_PARENT_WAIT_SLEEP
# If the child process does not exist anymore after this delay, report failure
if ! ps -p $CPID >/dev/null 2>&1 ; then
echo "Child process ($CPID) failure - Aborting" >&2
echo "Child diagnostics follow:" >&2
cat $clog >&2
rm -f $clog
ret=1
fi
rm -f $clog
fi
else
#------ Child
if [ -n "$TUNNEL_DEBUG" ] ; then
exec 2>/tmp/t2
set -x
env >&2
fi
gw="$GATEWAY_HOST"
[ "X$GATEWAY_USER" = X ] || gw="$GATEWAY_USER@$GATEWAY_HOST"
$SSH_CMD -N -L $LOCAL_HOST:$LOCAL_PORT:$TARGET_HOST:$TARGET_PORT -p $GATEWAY_PORT $gw &
CPID=$!
sleep $SSH_TUNNEL_CHECK_SLEEP
while true ; do
if ! ps -p $CPID >/dev/null 2>&1 ; then
echo "SSH process ($CPID) failure - Aborting" >&2
exit 1
fi
ps -p $MPID >/dev/null 2>&1 || break
sleep 1
done
kill $CPID
fi
exit $ret