Skip to content

Instantly share code, notes, and snippets.

@jotajr
Last active April 28, 2017 20:02
Show Gist options
  • Select an option

  • Save jotajr/6a6495e8cc05fd2d77a019f4d07b91c1 to your computer and use it in GitHub Desktop.

Select an option

Save jotajr/6a6495e8cc05fd2d77a019f4d07b91c1 to your computer and use it in GitHub Desktop.
Script to create a ssh tunnel to a host
#!/bin/bash
#Script to create a SSH Tunnel by Jota Freitas Jr
clear
echo "SSH Tunnel - Create a Connection"
echo "Let's Config our Dynamic Connection!"
read -p "Enter hostname/IP that you want to connect: " host
if [[ $host ]]
then
echo "Host entered: $host"
else
echo "Error: no host/ip entered! - Try Again!"
exit 1
fi
read -p "Enter the server username: " username
if [[ $username ]]
then
echo "Server username: $username"
else
echo "Error: no username entered! - Try Again!"
exit 1
fi
read -p "Enter local proxy port (blank - port 22): " port
if [[ $port ]]
then
echo "Port entered: $port"
else
port=22
echo "Using default port: $port"
fi
read -p "Are using private key file? (y/n - default n): " key_yn
#to lower case
if [[ $key_yn ]]
then
if [[ $key_yn == "y" ]]
then
read -p "Enter key file path: " key_path
fi
else
key_yn="n"
echo "No key file entered"
fi
read -p "Enter listening port (blank - port 8080): " list_port
if [[ $list_port ]]
then
echo "Port entered: $list_port"
else
list_port=8080
echo "Using default listening port: $list_port"
fi
echo "Connecting to host $host at port $port - Press ^C to exit"
if [[ $key_yn == "y" ]]
then
ssh -D $list_port -C -N -i $key_path $username@$host
else
ssh -D $list_port -C -N $username@$host
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment