Skip to content

Instantly share code, notes, and snippets.

@greg-fischer
Last active August 29, 2015 14:13
Show Gist options
  • Save greg-fischer/a785339b4cf6829910f1 to your computer and use it in GitHub Desktop.
Save greg-fischer/a785339b4cf6829910f1 to your computer and use it in GitHub Desktop.
remote-ssh-rdp-wrapper.sh
#!/usr/bin/env bash
## Need 6 parms
# 1 sshuser
# 2 sshkeyfile
# 3 sshhost
# 4 sshport
# 5 rdphost
# 6 rdpport (optional, will def to 3389)
COLORDEPTH=16
if [ "$1" = "rdpdirect" ]
then
SUSER=
SHOST=
SPORT=
SKEY=
###RDPHOST=emily7b
RDPHOST=$2
###RDPPORT=3389
RDPPORT=$3
FWDPORT=${RDPPORT}
RDPCMDHOST=${RDPHOST}
COLORDEPTH=24
else
SUSER=$1
SHOST=$3
SPORT=$4
#SKEY=/home/pi/.ssh/id_rsa
SKEY=$2
###RDPHOST=emily7b
RDPHOST=$5
###RDPPORT=3389
#RDPPORT=$6
# lets always use this forwarded port
FWDPORT=63389
RDPCMDHOST="localhost"
fi
if [ ! -z $6 ]
then
RDPPORT=$6
# or in 6, we can set local fwd port instead
if [[ "$6" == *"localport="* ]]
then
RDPPORT=3389
FWDPORT=$(echo ${6} |awk 'BEGIN {FS="=" } { print $2 }')
echo "Using local rdp port: ${FWDPORT}"
fi
else
RDPPORT=3389
fi
#rdp cmd
#RDP="dfreerdp"
#RDP="xfreerdp"
RDP="rdesktop"
# d and x free opts
# -g 1024x768 -z
#RDPOPTS="--ignore-certificate -g 1392x868 -a 16 --gdi sw -x l -T ${RDPHOST}"
#### !!! This appears to be the BEST so far!
## Will try for 24bit, or fallback to 16
## make sure tcp_window_scaling disabled in sysctl too
# rdesktop ops
RDPOPTS="-k en-us -f -a ${COLORDEPTH} -B -x m -b -P -z -r sound:local -0 -T ${RDPHOST}"
export NO_AT_BRIDGE=1
if [ "$1" = "rdpdirect" ]
then
echo ""
else
zenity --info --text="You are about to create a connection to tunnel host: ${SHOST} with account: ${SUSER} on port: ${SPORT} Please enter your usernmae and password to your remote computer in the next dialog."
fi
UOPTS=$(zenity --forms --width='380' --height='220' --title="Credz for ${RDPHOST}" --text="Enter your credzzz" --add-entry="Username" --add-password="Password" --add-entry="Domain(opt)" )
#echo ${UOPTS}
opuser=$(echo ${UOPTS} |awk 'BEGIN {FS="|" } { print $1 }')
oppass=$(echo ${UOPTS} |awk 'BEGIN {FS="|" } { print $2 }')
opdom=$(echo ${UOPTS} |awk 'BEGIN {FS="|" } { print $3 }')
#override the credentials here if needed, prob not wise
# Although, commandline will show a pass
#
#
#
if [ ! -z ${opdom} ]
then
RDPOPTS="${RDPOPTS} -d ${opdom}"
fi
if [ ! -z ${opuser} ]
then
if [ ! -z ${oppass} ]
then
#####################################
# ${opuser}
# ${oppass}
RDPCMD="${RDP} ${RDPOPTS} -u ${opuser} -p - ${RDPCMDHOST}:${FWDPORT}"
#check if local net rdp only
if [ "$1" = "rdpdirect" ]
then
echo "Connecting local network only (no tunnel)..."
else
ssh -i ${SKEY} -p ${SPORT} ${SUSER}@${SHOST} -L ${FWDPORT}:${RDPHOST}:${RDPPORT} -N &
sshpid=$!
echo "SSH PID: $sshpid"
NSCMD="netstat --protocol=inet -t -n -a"
echo -n "Waiting for tunnel..."
while ! ${NSCMD} | grep -i listen | gawk '{print $4}' | gawk 'BEGIN{FS=":"}{print $2}' | grep -q ${FWDPORT} ; do
sleep 1
echo .
done
echo "Connected!"
fi
echo "Starting remote desktop..."
echo ${oppass} | ${RDPCMD}
if [ "$1" != "rdpdirect" ]
then
kill $sshpid
fi
#####################################
else
echo "Passord is empty, exiting"
fi
else
echo "Username empty, exiting"
fi
read -p "Done! Press ENTER key to exit"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment