2016-09-30

Bash Script for Linux to Connect to Microsoft VDI

Script by Dino9021

因為 Remmina 一定要記住 User Name & Password 才能連線
有安全上的疑慮, 因此利用 Bash Script 搭配 xfreerdp 來進行連線作業

需要將連線資源寫成文字檔放在 Web Server 上讓 Bash Script 去下載解讀
如此如果資源有異動, 就不需要去 Client 更換 Script, 只要修改 Web Server 上的資源檔案就行了
而且如此一來不需要安裝 Remmina, 只要有 xfreerdp 就可以用了
經過測試, 如果不裝 Remmina 的話 xfreerdp 無法裝到 2.0 以上版本 (才支援 RDG)

安裝時參考 Microsoft 2012 R2 VDI 攻略 中 ubuntu 的安裝方法


  • Install Remmina and FreeRDP to Connect to VDI Through RD Gateway
    • sudo apt-get update
    • sudo apt-get dist-upgrade
    • sudo apt-add-repository ppa:remmina-ppa-team/remmina-next
    • sudo apt-get update
    • sudo apt-get install freerdp-x11 remmina remmina-plugin-rdp
    • sudo apt-get upgrade
    • xfreerdp --version


連線參數有需要可以自己改

# Need a website (RD Web is first choice) to store and download three files:

#
#

# Menu_Connection.txt for connection info, Single Line

# Logon Domain.com;RDCB Server FQDN;RDGateway FQDN;

#

# Menu_Workstation.txt for VM Base, Multi-Line

# Collection Name;Desc;Load Balance Info;

#

# Menu_Application.txt for RemoteAPP, Multi-Line

# App Name;Desc;Load Balance Info;App Alternate Name;

#

# End with ; at the end of line





#!/bin/bash

clear

SettingsURL="http://RDWeb.Contoso.com/Raspberry"

TypeVar=("Connection" "Workstation" "Application")

#
# Menu_Connection.txt for connection info, Single Line
# Logon Domain.com;RDCB Server FQDN;RDGateway FQDN;
#
# Menu_Workstation.txt for VM Base, Multi-Line
# Collection Name;Desc;Load Balance Info;
#
# Menu_Application.txt for RemoteAPP, Multi-Line
# App Name;Desc;Load Balance Info;App Alternate Name;
#
# End with ; at the end of line

function MainMenu {
 Type=$(dialog --clear \
      --backtitle "Main Menu" \
      --title "Contoso VDI" \
      --menu "Choose Resource Type:" \
      10 40 3 \
      "Exit" "" \
      "${TypeVar[1]}" "" \
      "${TypeVar[2]}" "" \
      3>&1 1>&2 2>&3 3>&-)

 if [ -z $Type ]; then
  clear
  exit
 fi

 unset ConnectionLine
 unset MenuLine
 unset ConnectionItems
 unset MenuItems
 unset Options
 unset Resource
 unset UserName
 unset UserPassword

 unset Domain
 unset RDCBServer
 unset Gateway

 unset ItemName
 unset ItemConnectionInfo
 unset ItemAppInfo

 ConnectionFile="Menu_${TypeVar[0]}.txt"
 if [ $Type = 'Exit' ]; then
  clear
  exit
 elif [ $Type = "${TypeVar[1]}" ]; then
  MenuFile="Menu_${TypeVar[1]}.txt"
 elif [ $Type = "${TypeVar[2]}" ]; then
  MenuFile="Menu_${TypeVar[2]}.txt"
 fi

 rm -rf $ConnectionFile
 wget "$SettingsURL/$ConnectionFile"
 while read -r ConnectionLine
 do
  IFS=';' read -a ConnectionItems <<< $ConnectionLine
     Domain=("${ConnectionItems[0]}")
     RDCBServer=("${ConnectionItems[1]}")
  Gateway=("${ConnectionItems[2]}")
 done < $ConnectionFile
 rm -rf $ConnectionFile

 rm -rf $MenuFile
 wget "$SettingsURL/$MenuFile"
 while read -r MenuLine
 do
  IFS=';' read -a MenuItems <<< $MenuLine
     Options+=("${MenuItems[0]}")
     Options+=("${MenuItems[1]}")
  ItemName+=("${MenuItems[0]}")
  ItemConnectionInfo+=("${MenuItems[2]}")
  ItemAppInfo+=("${MenuItems[3]}")
 done < $MenuFile
 rm -rf $MenuFile
 
 if [ ${#ItemName[@]} = 0 ]; then
  dialog --clear --title "[ Warning ]" --msgbox "\nNo Resources Found\nPlease contact your Administrator" 8 40
  MainMenu
 fi
 
 ChooseTarget

 clear
 exit
}

function ChooseTarget {

 Resource=$(dialog --clear \
      --backtitle "Resource Type: $Type" \
      --title "$Type" \
      --menu "Choose Resource:" \
      18 50 11 \
      "&1 1>&2 2>&3 3>&-)

 if [ "$Resource" = '' ]; then
  MainMenu
 elif [ "$Resource" = '&1 1>&2 2>&3 3>&-)

 if [ "$UserName" = '' ]; then
  MainMenu
 else
  InputUserPassword
 fi
 clear
 exit
}

function InputUserPassword {
 UserPassword=$(whiptail --clear \
        --backtitle "Authentication" \
        --title "Password" \
        --passwordbox "Enter Password of [$UserName] \nfor [$Resource]:" \
        8 40 3>&1 1>&2 2>&3 3>&-)
 ret=$?

 if [ -z "$UserPassword" ]; then
  MainMenu
 fi

 if [ "$UserPassword" = '' ]; then
  MainMenu
 fi

 i=0
 until [ $i -gt ${#ItemName[@]} ];
 do
  if [[ ${ItemName[$i]} != '' ]]; then

   if [ "$Resource" = "${ItemName[$i]}" ]; then
    Connection=("${ItemConnectionInfo[$i]}")
    App=("${ItemAppInfo[$i]}")
   fi
  fi
  let i+=1
 done

 ConnectToEnvironment

 clear
 exit
}

function ConnectToEnvironment {

 ConnectionString="xfreerdp /v:\"$RDCBServer\""
 ConnectionString+=" /w:1024 /h:768 /size:100% /f /multimon /span"
 ConnectionString+=" /t:\"$Resource\" /d:\"$Domain\" /g:\"$Gateway\""
 ConnectionString+=" /load-balance-info:\"$Connection\""
 if [ $Type = "${TypeVar[2]}" ]; then
  ConnectionString+=" /app:\"$App\""
 fi
 ConnectionString+=" /sound:sys:alsa /codec-cache:rfx /rfx /rfx-mode:video"
 ConnectionString+=" /cert-ignore /log-level:ERROR"
 ConnectionString+=" /u:\"$UserName\" /p:\"$UserPassword\""

clear
echo 
echo "  Press [Enter] to Connect -> $Type - $Resource"
echo 
echo "  Please wait with patient"
echo 
echo "  Press [Ctrl] + [Alt] + [Enter] to Switch Full Screen Mode"
echo
echo "  If you found any trouble in connecting to the environment,"
echo "  Please contact administrator with below information."
echo 
echo "--------------------------------------------------------------------------------"
eval $ConnectionString
echo "--------------------------------------------------------------------------------"
echo
echo "  If you found any trouble in connecting to the environment,"
echo "  Please contact administrator with above information."
echo
read -rsp $'  Press any key to back to Main Menu... \n\n' -n1 key
read -rsp $'\n' -n1 key

MainMenu

 exit
}

MainMenu

exit

沒有留言:

張貼留言