From 5c4658d39e2109b268a2f00a57a29853038da187 Mon Sep 17 00:00:00 2001 From: Andrew Tomaka Date: Fri, 5 Jun 2015 09:51:36 -0400 Subject: [PATCH] Add some tunnels script; not sure what this is I'll look at it later --- bin/tunnels.sh | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100755 bin/tunnels.sh diff --git a/bin/tunnels.sh b/bin/tunnels.sh new file mode 100755 index 0000000..e30e449 --- /dev/null +++ b/bin/tunnels.sh @@ -0,0 +1,60 @@ +#!/bin/bash + +readonly PROGRAM_NAME=$(basename $0) +readonly PROGRAM_LOC=$(readlink -m $(dirname $0)) +readonly ARGS="$@" + +usage() { + echo usage: $PROGRAM_NAME LOCAL_PORT REMOTE_PORT REMOTE_HOST + exit 1 +} + +create_tunnel() { + local readonly local_port=$1; shift + local readonly remote_port=$1; shift + local readonly remote_host=$1; shift + + ssh -f -N $remote_host -R $remote_port":localhost:"$local_port +} + +tunnel_not_present() { + local readonly remote_port=$1; shift + + ps ax \ + | grep ssh \ + | grep $remote_port \ + | grep -v grep >> /dev/null + + [[ $? -ne 0 ]] +} + +maintain_tunnel() { + local readonly local_port=$1; shift + local readonly remote_port=$1; shift + local readonly remote_host=$1; shift + + if tunnel_not_present $remote_port; then + create_tunnel $local_port $remote_port $remote_host + fi +} + +is_empty() { + local readonly string=$1; shift + + [[ -z "$string" ]] +} + +main() { + local readonly local_port=$1; shift + local readonly remote_port=$1; shift + local readonly remote_host=$1; shift + + if is_empty $local_port || is_empty $remote_port || is_empty $remote_host + then + usage + fi + + maintain_tunnel $local_port $remote_port $remote_host +} + +main