-
Notifications
You must be signed in to change notification settings - Fork 1
/
sshtunnel
58 lines (46 loc) · 1.21 KB
/
sshtunnel
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
#!/usr/bin/env bash
#load the config
CONFIG=/etc/myconfig/tunnel.conf
if [ -f "$CONFIG" ]; then
source "$CONFIG"
else
echo "=> ERROR: no config found @ $CONFIG"
exit 1
fi
# supervisord should be able to send a SIGINT to stop this services/daemon
trap finish TERM
finish(){
#kill $ACTUAL_PID $JOB
kill $JOB
echo "=> recieved SIGINT and exited OK"
exit 0
}
echo "=> Tunneling localhost:${TUNNEL_PORT_LOCAL} to ${REMOTE_USER}@${REMOTE_HOST}:${REMOTE_PORT} (on port ${TUNNEL_PORT_REMOTE}) using key ${KEY}"
autossh \
-M 0 \
-o "ServerAliveInterval 20" -o "ServerAliveCountMax 3" -o "StrictHostKeyChecking=no" \
-NR ${TUNNEL_PORT_REMOTE}:127.0.0.1:${TUNNEL_PORT_LOCAL} \
-i "${KEY}" \
${REMOTE_USER}@${REMOTE_HOST} &
# record the JOB (should be backgrounded)
JOB="$(jobs -p)"
# give it a few seconds to settle
sleep 3
ACTUAL_PID="$(pgrep -P $JOB)"
while true ; do
if ! kill -0 "$JOB" 2>/dev/null ; then
echo "=> ERR $$ ssh: $JOB not running"
exit 1
# else
# echo "=> $$ job: $JOB running"
fi
if ! kill -0 "$ACTUAL_PID" 2>/dev/null ; then
echo "=> ERR $$ autossh: running but its child died"
kill $JOB
exit 1
fi
# echo actual pid = $ACTUAL_PID
sleep 1
done
exit 0
echo "=> Exiting"