2 # this script manages one ssh tunnel inside a named 'screen' session.
6 # this section scavenges and documents the command line parameters...
8 # set this to the user and hostname that serve the tunnel on the remote
9 # side. for example: geoffrey@chaucertales.com
10 TUNNEL_USER_PLUS_HOST="$1"; shift
12 # a tunnel command for ssh that gives us a link between here and there.
13 # this should be in the form:
14 # "-L ${sourcePort}:${tunnelHost}:${destinationPort}"
15 # such as this example that connects a local port 12000 to the remote port
16 # of 25 in order to create a tunnel for smtp traffic:
17 # "-L 12000:localhost:25"
18 # other ssh commands can be used here if they are properly formatted.
19 TUNNEL_LINK="$1"; shift
21 # this variable should be set to the name for the tunnel. one can then
22 # open the tunnel screen with: screen -r -S "name"
23 TUNNEL_SCREEN_NAME="$1"; shift
25 # set this to your key file, plus the -i flag, such as:
26 # SECURITY_KEY="-i $HOME/.ssh/id_rsa"
27 # if you do not have a special one set up or the default is fine, then just
28 # pass a blank parameter (e.g. "").
29 TUNNEL_SECURITY_KEY="$1"; shift
31 # if this is set, it means we're through the script the second time, inside
32 # a screen session, and we need to actually do the work now.
37 function print_instructions()
40 $(basename $0): This script requires at least three parameters and can\n\
41 take up to five. The parameters are (1) tunnel user at hostname, (2) ssh tunnel\n\
42 link command, (3) tunnel screen name, (4) tunnel security key, (5) the launch\n\
43 command 'go'. An example command is shown below, but many more details are\n\
44 explained inside this script:\n\
45 $(basename $0) "geoffrey@chaucertales.com" "-L 12000:localhost:25" \\\n\
46 "tunz" "-i mykey.pem"\n\
47 The fifth flag is really only needed internally, but often the other four\n\
48 parameters are specified."
53 # make sure the required parameters are provided.
54 if [ -z "$TUNNEL_USER_PLUS_HOST" -o -z "$TUNNEL_LINK" -o -z "$TUNNEL_SCREEN_NAME" ]; then
61 # translate command line parameters if desired.
63 if [ "$LAUNCH_IT" == "go" ]; then
69 #hmmm: these variables should be configurable from plug-ins someday.
71 TUNNEL_ALERT_SOUND=$FEISTY_MEOW_APEX/infobase/sounds/woouoo.wav
72 if [ ! -z "$1" ]; then
76 # how often to play sounds when reconnecting.
79 # when we last played a sound.
84 play_sound_periodically()
86 CURRENT_TIME=$(date +"%s")
87 if (( $CURRENT_TIME - $LAST_SOUND_TIME >= $NOISE_PERIOD )); then
88 echo playing sound now.
89 bash $FEISTY_MEOW_SCRIPTS/multimedia/sound_play.sh $TUNNEL_ALERT_SOUND &>/dev/null </dev/null &
90 LAST_SOUND_TIME=$CURRENT_TIME
96 function main_tunnely_loop()
99 echo "Connecting tunnel to destination..."
100 ssh -2 -N -v "$TUNNEL_LINK" "$TUNNEL_SECURITY_KEY" "$TUNNEL_USER_PLUS_HOST"
101 echo "Got dumped from tunnels; re-establishing connection."
102 play_sound_periodically
103 echo "Note: if you're being asked for a password, then you haven't provided\nan RSA key that works yet."
108 if [ $LAUNCHING_TUNNEL -eq 1 ]; then
109 # this version is already ready to tunnel already, so let's tunnel.
111 # loop does not exit on its own.
113 # this version re-launches the script but tells it to start the tunnel.
114 existingScreens="$(screen -ls | grep "$TUNNEL_SCREEN_NAME")"
115 if [ ! -z "$existingScreens" ]; then
116 echo "This script is already running a screen for: $TUNNEL_SCREEN_NAME"
117 echo "Connect to that and zap it first before we try to start a new one,"
118 echo "e.g.: screen -r -S \"$TUNNEL_SCREEN_NAME\""
121 screen -L -S "$TUNNEL_SCREEN_NAME" -d -m bash $0 "$TUNNEL_USER_PLUS_HOST" "$TUNNEL_LINK" "$TUNNEL_SCREEN_NAME" "$TUNNEL_SECURITY_KEY" go