Created
March 25, 2014 23:26
-
-
Save jesseditson/9773690 to your computer and use it in GitHub Desktop.
Tunnel script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
function stopTunnels { | |
echo "stopping db tunnnels:" | |
if [ -n "$PID" ]; then | |
echo "stopping $PID..." | |
kill $PID | |
sleep 1 | |
fi | |
} | |
# usage | |
if [ "$1" == "stop" ] || [ -z "$1" ]; then | |
echo "You must pass some arguments to tunnel. Pass the server to tunnel via, then the server to tunnel to, and finally, pass the arguments to run on the server." | |
echo "./tunnel 127.0.0.1 someuser@127.0.0.1 somecommand --whatever" | |
exit 0 | |
fi | |
# tunnel to a db | |
tunnelserver=$1 | |
remoteserver=$2 | |
args=${@:3} | |
host=$tunnelserver | |
remotehost=$remoteserver | |
echo "tunneling to $remotehost via $host" | |
ssh -N -L 27018:$remotehost:27017 $host & | |
PID=$! | |
sleep 3 | |
echo "started tunnel with PID: $PID" | |
echo "running $args" | |
$args | |
trap stopTunnels EXIT |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Looks like line 27 would need to be modified to use this for things not running on 27017 - but this is a good start.