Thursday, July 24, 2014

Poor man's implementation of rdesktop configuration

In my current job I have to connect to various windows boxes with rdesktop. It is really annoying to pass all options and parameters on command line. I would prefer some kind of configuration for storing connection information for servers. Something like ~/.rdesktoprc. Poor man's implementation of resktop config will be discussed in this post.

Unfortunatelly no such thing like configuration exists although this feature is requested on the official site. I won't wait for this feature to be developed. Therefore I've written a small script to provide this functionality. It's named rdesktop.sh:

#!/bin/bash
config=${HOME}/.rdesktoprc

function usage()
{
    echo "Usage: $(basename ${0}) server"
}

function read_config()
{
    local found=false
    local comment_regexp="^\s*\#$"

    while read line
    do
        # Ignore empty lines and lines with comments
        [[ ${line} =~ ^\s*$ ]] && continue
        [[ ${line} =~ ${comment_regexp} ]] && continue

        # Line with [server] string
        if [[ ${line} =~ ^\s*\[${server}\]\s*$ ]]
        then
            found=true
            continue
        fi

        if ${found}
        then
            # Line with [] string - required server section ends
            [[ ${line} =~ ^\s*\[.*\]\s*$ ]] && break
            options+="${line} "
        fi
    done < ${config}
}

if [ $# != 1 ]
then
    usage
    exit 0
fi

server=${1}
options=""

if [ ! -f "${config}" ]
then
    echo "Config file ${config} does not exists!"
    exit 1
fi

if ! grep "\[${server}\]" ${config} >/dev/null 2>&1
then
    echo "Server ${server} not found in config file!"
    exit 2
fi

read_config
rdesktop ${options}

Script expects configuration file in $HOME/.rdesktoprc. Config file is divided to sections. Section begins with [server_name], where server_name is only descriptive name. Each section can contain rdesktop options and corresponding parameters. Following configuration file contains two sections - win-7 and kra:

[vajko@kra ~]$ cat ~/.rdesktoprc 
# Commented out lines as well as empty lines (like next one) are ignored

[win-7]
-u vajko
-d win_domain
-r sound:local
-p superServerPassword!
-g 90%
192.168.122.81

[win-8]
-u vajko
-p anotherSuperSecretPassw0rd
192.168.45.39

To connect to chosen server just run script above with single parameter which is name of corresponding section:

[vajko@kra ~]$ rdesktop.sh win-7

Behind the scene script will find corresponding section in config file, contatenate all lines in the section and finally call rdesktop binary with collected options and parameters.

No comments:

Post a Comment