Moritz Schepp

Poor Man’s VPN

This is just a short guide on how to connect to RDP servers within an internal network through a SSH tunnel

This is just a short guide on how to connect to RDP servers within an internal network through a SSH tunnel. Specifically, the solution allows you to do it with the call of a Bash script that sets up everything and then tears it down once your RDP connection stops.

So I wanted this to be easy and also customizable to multiple hosts within network. Check out what I came up with:

#!/bin/bash -e

GATEWAY=root@login.example.com
USER=admin
DOMAIN=example.local

HOST=$1
PORT=`shuf -i 20000-30000 -n 1`
SSH_COMMAND="ssh -fNT -L $PORT:$HOST:3389 $GATEWAY"
echo "Tunnel established on port $PORT"

$SSH_COMMAND
rdesktop -g 1400x900 -u $USER -d $DOMAIN 127.0.0.1:$PORT || true

SSH_PID=`ps x | grep "$SSH_COMMAND" | grep -v "grep" | sed -E "s/^ *([0-9]+).*$/\\1/"`
echo "killing remaining SSH process $SSH_PID"
kill $SSH_PID

So what does this do? It first generates a random number to be used as a local port for the communication. Then the SSH tunnel is established in the background. After that, the RDP client can connect. After it disconnects, the SSH process is reeped. Put the above lines in an executable file tunnel.sh Modify the GATEWAY, USER and DOMAIN variables and then use it like this:

$ ./tunnel.sh my.internal.host

I hope you find it useful!