Skip to content

Instantly share code, notes, and snippets.

@jesseditson
Created March 25, 2014 23:26
Show Gist options
  • Save jesseditson/9773690 to your computer and use it in GitHub Desktop.
Save jesseditson/9773690 to your computer and use it in GitHub Desktop.
Tunnel script
#!/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
@jesseditson
Copy link
Author

Looks like line 27 would need to be modified to use this for things not running on 27017 - but this is a good start.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment