Skip to content

Instantly share code, notes, and snippets.

@redmoses
Last active February 28, 2016 13:33
Show Gist options
  • Save redmoses/e00618d09468ca7e0b8e to your computer and use it in GitHub Desktop.
Save redmoses/e00618d09468ca7e0b8e to your computer and use it in GitHub Desktop.
SSH tunnelling using dynamic proxy

SSH Proxy Script by Red Moses

http://redmoses.me

This script connects to a SSH server to create a dynamic tunnel proxy. I'm assuming you use a private key for authenticating to the server.

Script configuration

To use this script you must first configure it according to your details. I have supplied some dummy values for the configuration fields to start with.

# SSH user
USER=johndoe

# Server domain/ip
SERVER=proxyserver.com

# Private key for authenticating to the server
KEY=~/.ssh/id_rsa

# SSH port of the server
SSH_PORT=22

# Local port to be used for proxy
PROXY_PORT=8999

Script usage

# Start tunnel
./tunnel.sh start

# Stop tunnel
./tunnel.sh stop

# Restart tunnel
./tunnel.sh restart

# Tunnel status
./tunnel.sh status

SSH Example

Suppose you want to connect to server.com using the proxy service you've started using the script. Open your 'ssh_config' file (for Ubuntu),

sudo vim /etc/ssh/ssh_config

and enter the following at the end of the file. I'm using an example configuration. Please change the fields according to your own specifications.

Host server.ssh
  Hostname      example.com
  Port          2020
  IdentityFile  ~/.ssh/server
  User          admin
  ProxyCommand  nc -x localhost:8999 %h %p

From now on you can ssh to the above server by using the following command,

ssh server.ssh

Firefox or other browsers

For using the proxy with firefox or other browsers just use the following configuration as Socks v5 proxy,

Server: 127.0.0.1
Port: 8999
#!/bin/bash
## A script by RedMoses, redmoses.me ##
## Proxy Configuration ##
# Please change it accordingly
USER=johndoe
SERVER=proxyserver.com
KEY=~/.ssh/id_rsa
SSH_PORT=22
PROXY_PORT=8999
#########################
# start, stop, or restart
ACTION=$1
## MESSAGES ##
start_msg="Proxy service started."
stop_msg="Proxy service stopped."
status_msg="Proxy service is "
usage_msg="* Usage: ./proxy.sh {start|stop|restart|status}"
function tunnel_start {
ssh -C -c blowfish -f -N -D $PROXY_PORT -p $SSH_PORT -i $KEY $USER@$SERVER
echo "$start_msg"
}
function tunnel_stop {
sudo kill 9 $(sudo lsof -t -i:$PROXY_PORT)
echo "$stop_msg"
}
function tunnel_status {
PID=$(sudo lsof -t -i:$PROXY_PORT)
if [[ "$PID" = "" ]]; then
echo "$status_msg not running."
elif [[ condition ]]; then
echo "$status_msg running."
fi
}
if [ ! -z "$ACTION" ]; then
# start tunnel
if [ "$ACTION" = "start" ]; then
tunnel_start
# stop tunnel
elif [ "$ACTION" = "stop" ]; then
tunnel_stop
# restart tunnel
elif [[ "$ACTION" = "restart" ]]; then
tunnel_stop
sleep 3
tunnel_start
# tunnel status
elif [[ "$ACTION" = "status" ]]; then
tunnel_status
else
echo "$usage_msg"
fi
else
echo "$usage_msg"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment