User Tools

Site Tools


unix:rdp_through_ssh

RDP via SSH tunnels created on the fly

This is pretty basic, and I gave up on the system eventually in favor of OpenVPN, which works beautifully.

The script takes a remote host, the server you will ssh to, username, domain (I think “.” works for local?), a local port number to start with (this allows you to open another set of servers not on the same range of local ports), and finally all the hostnames you want to log into with rdesktop. This all assumes you have ssh working, and that you have public key authentication configured correctly.

It's not bad, but I wrote a better one which uses Mac OS X's keychain facility to get passwords and pass them to rdesktop, and which doesn't bother creating an SSH tunnel for every host. Still, if you don't want to setup a VPN, this works pretty well and allows you to login to many, many servers at once. I use the new script to login to dozens of servers at a time during Windows patch season.


Here's the code:

#!/bin/bash

#TO-DO: add argument checking, add argument for ssh host, add argument for ssh opts
#       establish one ssh session per host which is killed automatically after disconnecting
#       parameters- username, domain, etc.

if [ -z $BASH_ARGC ]; 
        then echo "Usage: $0 host username domain start_port_number remote_host1 [ remote_host2 remote_host3 ... ]";
        echo "Note: 'start_port_number' must be greater than 1024.";
        exit 0;
fi

host=$1
username=$2
domain=$3
start_port=$4

#Get a password

read -s -p "Enter password: " pass
echo
echo "Establishing connections..."
echo

if [ -z $pass ]; 
        then echo;echo;echo "You must enter a password";echo;exit 1;
fi

# Old...
#start_port=$1
#$(echo $1|cut -b1-$((${#1} - 1)))
#echo $start_port

#Fill arrays with values
j=0
for (( i=(( $# + $start_port - 5 ));i >= $start_port; i--));
do
        hostlist[$j]="$(echo $i:${BASH_ARGV[$j]}:3389)"
        myarray[$j]="${BASH_ARGV[$j]} $i"
        myarray2[$j]="localhost:$i"
        j=$(( $j+1 ));
done

for (( i=(( $# - 5 ));i >= 0; i--));
do
        ssh -C -L ${hostlist[$i]} -N $host &
done

sleep 2

for (( i=(( $# - 5 ));i >= 0; i--));
do
        rdesktop -a 16 -g 1300x720 -d $domain -u $username -p $pass -T "${myarray[$i]}" ${myarray2[$i]} &
done

for (( i=(( ${#hostlist[@]} - 1 )) ; i >= 0; i-- ))
do
        tmpvar=$(ps ax | grep "${hostlist[$i]}" | grep -v grep | cut -b1-6); 
        SSHPID=$(echo $SSHPID $tmpvar);
done

echo
echo
read -p "Press Enter to terminate ssh connections..."
echo "Killing PID's: $SSHPID"
kill $SSHPID
exit 0;
unix/rdp_through_ssh.txt · Last modified: 2010/03/06 01:50 by ben