Skip to content

Instantly share code, notes, and snippets.

@sgdavis1
Created March 3, 2018 23:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sgdavis1/2d545278fc59c95721f05eb3aa40f519 to your computer and use it in GitHub Desktop.
Save sgdavis1/2d545278fc59c95721f05eb3aa40f519 to your computer and use it in GitHub Desktop.
#!/bin/bash
destport=$(echo $1 | awk -F ':' '{print $2}');
desthost=$(echo $1 | awk -F ':' '{print $1}');
tunnelhost=$2
echo "Connecting to $1 via a tunnel through $2";
if [[ $destport == "" ]]; then
destport=22;
echo "Assuming destination port is 22. Specify with 'host:port' if this isn't true..."
fi
if [[ $tunnelport < 1024 ]]; then
let "tunnelport=$tunnelport + 2000"
fi
echo "Executing: ssh -M -S tunnel-ctl-socket -fnNT -L $tunnelport:$desthost:$destport $tunnelhost"
ssh -M -S tunnel-ctl-socket -fnN -L $tunnelport:$desthost:$destport $tunnelhost
ssh -p $tunnelport localhost
echo "Executing: ssh -S tunnel-ctl-socket -O exit localhost"
ssh -S tunnel-ctl-socket -O exit localhost
@sgdavis1
Copy link
Author

sgdavis1 commented Mar 3, 2018

Easier way to connect to a host via a tunnel host when you are either using key auth, or can remember to type the passwords in order (tunnel / dest). Not super useful, but for whatever reason I feel it is easier to remember than trying to remember ssh tunneling syntax, haha.

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