Skip to content

Instantly share code, notes, and snippets.

@nod0n
Last active December 16, 2015 08:24
Show Gist options
  • Save nod0n/d59186a1b8ee901c8d08 to your computer and use it in GitHub Desktop.
Save nod0n/d59186a1b8ee901c8d08 to your computer and use it in GitHub Desktop.
#!/bin/bash
# ~/bin/connect-rdp or /usr/local/bin/connect-rdp
# or ~/bin/connect-rdp.bash or /usr/local/bin/connect-rdp.bash
#
#
# this script should (when completly written)
# - connect with the xfreerdp tool to hosts
# - get settings from global config file
# - get credentials from a domain specific config file or ask for it.
# - usable with out many or complicated options.
#
declare config domain hostname e_wrong_usage=1
declare -a options
declare -A lines credentials settings
# default values for global settings (if not found in global config file)
settings[resolution]='1024x768'
settings[clipboard_sync]=false
settings[menu_animation]=false
settings[check_cert]=true
# config files
config_dir=~/.freerdp/ # default: ~/.freerdp/
config_file_pattern='rdp-*.conf' # default: rdp-*.conf
global_config_file='rdp-main.conf' # default: rdp-main.conf
#TODO: backup resolution
usage () {
printf '\nusage:\n'
printf '%s [domain] hostname\n\n' "$0"
printf 'the apropirate config file is chosen based on the domain.\n'
printf 'Config file directory:\t\t\t%s\n' "$config_dir"
printf 'Domain config file name pattern:\t%s\n' "$config_file_pattern"
printf 'Global config file:\t\t\t%s' "$global_config_file"
printf '\n'
printf 'Config file should contain user and password. Domain information is'
printf 'optional.\n'
printf '\n'
}
# check if -h is chosen
while getopts h option; do
case "$option" in
h)
usage
exit 0
;;
*)
:
;;
esac
done
case "$#" in
1)
hostname=$1
;;
2)
domain=$1
hostname=$2
;;
0)
read -p 'enter Hostname and press enter:' hostname
;;
*)
usage >&2
exit $e_wrong_usage
;;
esac
# reading global config (not domain specific)
main_config=$(
file=${config_dir%/}/$global_config_file
[[ -f $file ]] && grep -v -e '^[[:space:]]#' -e '^[[:space:]]*$' "$file"
)
printf '\nSetings:\n'
unset 'lines[@]'
for key in resolution clipboard_sync menu_animation check_cert; do
lines[$key]=$(grep -o $key='[^[:space:]]*' <<< "$main_config")
[[ -n ${lines[$key]#$key=} ]] && settings[$key]=${lines[$key]#$key=}
printf '%s:\t%s\n' "$key" "${settings[$key]}"
done
printf '\n'
unset 'lines[@]'
# Reading config file (containing domain specific informations).
config=$(
file=${config_dir%/}/${config_file_pattern/\*/$domain}
[[ -f $file ]] && {
grep -v -e '^[[:space:]]#' -e '^[[:space:]]*$' "$file"
}
)
unset 'lines[@]'
for key in domain user pw; do
lines[$key]=$(grep -o $key='[^[:space:]]*' <<< "$config")
credentials[$key]=${lines[$key]#$key=}
printf '%s:\t%s\n' "$key" "${credentials[$key]}"
done
unset 'lines[@]'
printf 'hostname:\t%s\n' "$hostname"
# user is required, password will be ask by xfreerdp if not given as argument
[[ -z ${credentials[user]} ]] && {
read -p 'Type your user name please: ' credentials[user]
}
# generating xfreerdp params
options+=("-u" "${credentials[user]}")
k="${credentials[domain]}"; [[ -n $k ]] && options+=("-d" "$k")
k="${credentials[pw]}"; [[ -n $k ]] && options+=("-p" "$k")
k="${settings[resolution]}"; [[ -n $k ]] && options+=("-g" "$k")
[[ ${settings[clipboard_sync]} = true ]] && options+=("--plugin" "cliprdr")
[[ ${settings[menu_animation]} = false ]] && {
options+=("--disable-menu-animations")
}
[[ ${settings[check_cert]} = false ]] && options+=("--ignore-certificate")
# print what it will do first, just for testing feel free to remove
[[ -n $hostname ]] && echo xfreerdp "${options[@]}" "$hostname"
# just comment the next line if your are testing, then the script wont realy do
# something.
[[ -n $hostname ]] && xfreerdp "${options[@]}" "$hostname"
# ASDF Angaben
# ~/.freerdp/rdp-bbzo.conf
pw=mysupersecretpwthatyouwillneverguess
user=myuser
domain=myperfectdomain
# Globale Angaben
# ~/.freerdp/rdp-main.conf
resolution=1865x1150
clipboard_sync=true
menu_animation=false
check_cert=false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment