Skip to content

Instantly share code, notes, and snippets.

@leandrocruz
Last active March 10, 2016 13:41
Show Gist options
  • Save leandrocruz/1eba1bfec75bd897f23f to your computer and use it in GitHub Desktop.
Save leandrocruz/1eba1bfec75bd897f23f to your computer and use it in GitHub Desktop.
#! /bin/bash
port=8022
nat_ip=10.0.0.1
nat_key=~/.ssh/nat.pem
target_key=~/.ssh/private_machine.pem
reuse=0
ssh-keygen -f ~/.ssh/known_hosts -R [localhost]:$port
while getopts "r" opt; do
case $opt in
r)
reuse=1
;;
\?)
echo "Invalid option: -$OPTARG" >&2
exit
;;
esac
done
if [ $reuse -eq 0 ];
then
pid=$(netstat -ltpn 2> /dev/null | grep $port | awk '{print $7}' | tr '\n' ' ' | awk '{print $1}' | cut -d '/' -f1)
if [ "$pid" != "" ];
then
echo "Killing previous tunnel: $pid"
kill -9 $pid
fi
ssh -fNL $port:$1:22 ec2-user@$nat_ip -i $nat_key
fi
ssh -o StrictHostKeyChecking=no -i $target_key -p $port ubuntu@localhost
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment