User Tools

Site Tools


unix:osx:rdesktop_script

rdesktop w/keychain

The purpose of this script is to securely (fairly) login to multiple remote desktop sessions at the same time, while using passwords stored in OS X's Keychain facility.

Passwords can be stored as generic password types, the biggest caveat is that the format domain\username cannot be easily used. Username@domain can however, so store your account information in that format.

Once passwords are loaded into Keychain, they can be retrieved in command line scripts via the security command. The output of passwords is on stderr, the output of other information is on stdout. So there's a little redirection that needs to happen to keep things tidy. For example:

security 2>&1 >&- find-generic-password -l $username -g CUIT.keychain
security 2>/dev/null find-generic-password -l $username -g CUIT.keychain|grep acct |cut -f 4 -d \"

The first gets the password for the keychain element with the name $username, and the second gets the account.

The rest of the script just runs through command line arguments and executes the rdesktop command with each host. It's fairly straight forward.

The nice thing is that it can be aliased in your .profile to various account names. E.g.:

 alias rda1.sh='rd.sh acct1@blah.domain'

Here's the script:

#!/bin/bash

if [ -z $BASH_ARGC ]; 
	then echo "Usage: $0 username (domain\username, username@domain) remote_host1 [ remote_host2 remote_host3 ... ]";
	exit 0;
fi

username=$1
#Get a password
passline=$(security 2>&1 >&- find-generic-password -l $username -g CUIT.keychain)
pass=$(echo $passline|grep -e ^password:)

#Depending on the success of the last command, we either output an error, or
#get the password via a cut and the account name via a grep and cut
if (( $? ))
	then 
		echo $passline  #if we're here, then "password" wasn't in the string, so there's a problem...
		exit 1 
	else
		pass=$(echo $pass|cut -f 2 -d \")
		user=$(security 2>/dev/null find-generic-password -l $username -g CUIT.keychain|grep acct |cut -f 4 -d \")

fi

echo "Establishing connections..."

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

for (( i=(( $# - 2 ));i >= 0; i--));
do
      echo ${myarray2[$i]}" ";rdesktop -k en-us -z -a 16 -g 1300x720 -r clipboard:CLIPBOARD -u $user -p $pass -T "${myarray[$i]}" ${myarray2[$i]} &
done

exit 0;
unix/osx/rdesktop_script.txt · Last modified: 2010/03/05 02:55 by ben