Check-in [4d6a646b70]
Overview
SHA1:4d6a646b709385317059f167578a294764184059
Date: 2016-03-29 17:39:54
User: rkeene
Comment:Added initial rdp.sh release
Timelines: family | ancestors | descendants | both | trunk
Downloads: Tarball | ZIP archive
Other Links: files | file ages | folders | manifest
Tags And Properties
Context
2016-03-29
17:48
[5996946805] Leaf: Added Makefile (user: rkeene, tags: trunk, 1.0)
17:39
[4d6a646b70] Added initial rdp.sh release (user: rkeene, tags: trunk)
17:27
[9258ed238d] initial empty check-in (user: rkeene, tags: trunk)
Changes

Added rdp.sh version [a814622ace].

            1  +#! /bin/bash
            2  +
            3  +if [ "$1" = '-ssh' ]; then
            4  +	shift
            5  +
            6  +	RDP_SSH_JUMPBOX="$1"
            7  +	shift
            8  +else
            9  +	RDP_SSH_JUMPBOX=''
           10  +fi
           11  +
           12  +RDP_HOST="$1"
           13  +shift
           14  +
           15  +function printHelp() {
           16  +	echo 'Usage: rdp [-ssh host] <host> [<xfreeRdpArgs...>]'
           17  +}
           18  +
           19  +if [ -z "${RDP_HOST}" ]; then
           20  +	printHelp >&2
           21  +
           22  +	exit 1
           23  +fi
           24  +
           25  +# Include a config file, which is actually just a script
           26  +# This script can do things like update the "RDP_SSH_JUMPBOX"
           27  +# based on the "RDP_HOST", provide a password (from a password
           28  +# manager, like hunter2, ideally), etc.
           29  +if [ -f ~/.rdp.conf ]; then
           30  +	. ~/.rdp.conf
           31  +fi
           32  +
           33  +## Split the target into host/port pairs
           34  +case "${RDP_HOST}" in
           35  +	*:*)
           36  +		hostName="$(echo "${RDP_HOST}" | cut -f 1 -d ':')"
           37  +		hostPort="$(echo "${RDP_HOST}" | cut -f 2 -d ':')"
           38  +		;;
           39  +	*)
           40  +		hostName="${RDP_HOST}"
           41  +		hostPort='3389'
           42  +		;;
           43  +esac
           44  +
           45  +# If we are jumping through an SSH jumpbox, setup port forwarding
           46  +if [ -n "${RDP_SSH_JUMPBOX}" ]; then
           47  +	# Pick a random port to forward on
           48  +	randomPort=$[${RANDOM} % 1024 + 3390]
           49  +
           50  +	# Determine an SSH control socket
           51  +	sshControlSocket="/tmp/ssh-sock-${randomPort}-$$${RANDOM}${RANDOM}${RANDOM}"
           52  +
           53  +	function cleanup() {
           54  +		# Kill the SSH session
           55  +		sshPid="$(timeout 30 ssh -S "${sshControlSocket}" -O check /dev/null 2>&1 | grep 'pid=' | sed 's@.*pid=@@;s@).*$@@')"
           56  +		if [ -n "${sshPid}" ]; then
           57  +			kill -9 "${sshPid}" >/dev/null 2>/dev/null
           58  +		fi
           59  +
           60  +		rm -f "${sshControlSocket}"
           61  +	}
           62  +
           63  +	# Start SSH
           64  +	ssh -L${randomPort}:${hostName}:${hostPort} -o ExitOnForwardFailure=yes -o ControlMaster=yes -S "${sshControlSocket}" -a -f -N "${RDP_SSH_JUMPBOX}"
           65  +
           66  +	# Verify SSH started
           67  +	sshPid="$(ssh -S "${sshControlSocket}" -O check /dev/null 2>&1 | grep 'pid=' | sed 's@.*pid=@@;s@).*$@@')"
           68  +	if [ -z "${sshPid}" ]; then
           69  +		echo "Unable to start SSH" >&2
           70  +
           71  +		exit 1
           72  +	fi
           73  +
           74  +	hostId="${hostName}:${hostPort}!${RDP_SSH_JUMPBOX}"
           75  +
           76  +	hostName='localhost'
           77  +	hostPort="${randomPort}"
           78  +else
           79  +	function cleanup() {
           80  +		return 0
           81  +	}
           82  +
           83  +	hostId="${hostName}:${hostPort}"
           84  +fi
           85  +
           86  +trap cleanup EXIT
           87  +
           88  +# Connect to the RDP session
           89  +## Determine username and domain
           90  +if [ -n "${RDP_USERNAME}" ]; then
           91  +	addArgs=("${addArgs[@]}" -u "${RDP_USERNAME}")
           92  +else
           93  +	addArgs=("${addArgs[@]}" -u "$(whoami)")
           94  +fi
           95  +
           96  +if [ -n "${RDP_DOMAIN}" ]; then
           97  +	addArgs=("${addArgs[@]}" -d "${RDP_DOMAIN}")
           98  +fi
           99  +
          100  +## Determine port
          101  +if [ -n "${hostPort}" -a "${hostPort}" != '3389' ]; then
          102  +	addArgs=("${addArgs[@]}" -t "${hostPort}")
          103  +fi
          104  +
          105  +## Export the password to the environment and tell xfreerdp to use
          106  +## it.  This requires a patched xfreerdp.
          107  +if [ -n "${RDP_PASSWORD}" ]; then
          108  +	export RDP_PASSWORD
          109  +
          110  +	addArgs=("${addArgs[@]}" -p ENV:RDP_PASSWORD)
          111  +fi
          112  +
          113  +## Actually call xfreerdp
          114  +xfreerdp --plugin drdynvc --plugin rdpdr --data scard "disk:pwd:$(pwd)" -- --certificate-name "${hostId}" "${addArgs[@]}" "$@" "${hostName}"
          115  +returnCode="$?"
          116  +
          117  +exit "${returnCode}"