NoPaste

virtualbox ssh manage script - preversion

von speefak

SNIPPET_TEXT:
  1. #!/bin/bash
  2. # name          : vbox_cli-manager
  3. # desciption    : shell based cli management for virtualbox on various servers
  4. # autor         : speefak (itoss@gmx.de)
  5. # licence       : (CC) BY-NC-SA
  6. # version       : 0.2.8
  7.  
  8. #-------------------------------------------------------------------------------------------------------------------------------------------------------
  9. ############################################################################################################
  10. #######################################   define global variables   ########################################
  11. ############################################################################################################
  12. #-------------------------------------------------------------------------------------------------------------------------------------------------------
  13.  
  14.  ConfigFile=/home/user/.vbox_mgr.cfg                                                                    # configure => path to config file
  15.  XfreeRDPPassword="password"                                                                            # configure => password from virtual box user on VM host
  16.  
  17.  ConfigfileMD5SumOld=$(md5sum $ConfigFile 2> /dev/null)
  18.  VirtualBoxInstalledVersion=$(dpkg -l | grep virtualbox | awk '{printf  $3}' | cut -d "-" -f1)
  19.  
  20.  CheckMark="\033[0;32m\xE2\x9C\x94\033[0m"
  21.  Version=$(cat $(readlink -f $(which $0)) | grep "# version" | head -n1 | awk -F ":" '{print $2}' | sed 's/ //g')
  22.  ScriptName=$(basename $(readlink -f $(which $0)))
  23.  
  24. #-------------------------------------------------------------------------------------------------------------------------------------------------------
  25. ############################################################################################################
  26. #####################################   set vars from input options  #######################################
  27. ############################################################################################################
  28. #-------------------------------------------------------------------------------------------------------------------------------------------------------
  29.  
  30.         OptionVarList="
  31.  
  32.                 VirtualMachineManagement;-vmm
  33.                 HelpDialog;-?
  34.                 HelpDialog;-help
  35.  
  36.                 BootVM;-b
  37.                 PauseVM;-pv
  38.                 ResumeVM;-rv
  39.                 HibernateVM;-h
  40.                 ShutdownVM;-s
  41.                 ResetVM;-r
  42.                 PowerOffVM;-p
  43.                 KillVM;-k
  44.                 RDPConnectVM;-c
  45.                 RDPCloseVM;-C
  46.                 RDPQuitVM;-q
  47.  
  48.                 UpdateVMSpecs;-us
  49.                 ShowVMSpecs;-ss
  50.                 ListVMStatus;-ls
  51.                 ShowConfig;-sc
  52.                 NewConfig;-nc
  53.                 Reconfigure;-rc
  54.                 CheckConfig;-cc
  55.                
  56.                 Monochrome;-m
  57.                 ScriptInformation;-i
  58.  
  59.                 VMConfigSet;-vmcfg
  60.         "
  61.  
  62.         # set entered vars from optionvarlist
  63.         OptionAllocator=" "                                                                             # for option seperator "=" use cut -d "="
  64.         SAVEIFS=$IFS
  65.         IFS=$(echo -en "\n\b")
  66.         for InputOption in $(echo " $@" | sed 's/ -/\n-/g') ; do
  67.                 for VarNameVarValue in $OptionVarList ; do
  68.                         VarName=$(echo "$VarNameVarValue" | cut -d ";" -f1)
  69.                         VarValue=$(echo "$VarNameVarValue" | cut -d ";" -f2)
  70. #                       if [[ -n $(echo " $InputOption" | grep " $VarValue" 2>/dev/null) ]]; then
  71.                         if [[ $InputOption == "$VarValue" ]]; then
  72.                                 eval $(echo "$VarName"='$InputOption')                                  # if [[ -n Option1 ]]; then echo "Option1 set";fi
  73.                                 #eval $(echo "$VarName"="true")
  74.                         elif [[ $(echo $InputOption | cut -d "$OptionAllocator" -f1) == "$VarValue" ]]; then   
  75.                                 eval $(echo "$VarName"='$(echo $InputOption | cut -d "$OptionAllocator" -f 2-10)')
  76.                         fi
  77.                 done
  78.         done
  79.         IFS=$SAVEIFS
  80.  
  81. #-------------------------------------------------------------------------------------------------------------------------------------------------------
  82. ############################################################################################################
  83. ###########################################   define functions   ###########################################
  84. ############################################################################################################
  85. #-------------------------------------------------------------------------------------------------------------------------------------------------------
  86. usage () {
  87. cat << USAGE
  88.  Virtualbox Shell Manager - version $VERSION
  89.  Usage: $(basename $0) [-option]
  90.  
  91.  virtual machine management
  92.  -b(X.X)        (b)oot virtual machine (hostnumber.VMnumner)
  93.  -pv(X.X)       (p)ause (v)irtual machine (hostnumber.VMnumner)
  94.  -rv(X.X)       (r)esume (v)irtual machine (hostnumber.VMnumner)
  95.  -h(X.X)        (h)ibernate virtual machine (hostnumber.VMnumner)
  96.  -s(X.X)        (s)hutdown virtual machine via acpi (hostnumber.VMnumner)
  97.  -r(X.X)        (r)eset virtual machine (hostnumber.VMnumner)
  98.  -p(X.X)        (p)oweroff virtual machine (hostnumber.VMnumner)
  99.  -k(X.X)        (k)ill virtual machine (hostnumber.VMnumner)
  100.  -c(X.X)        (c)onnect virtual machine via rdp protokoll (hostnumber.VMnumner)
  101.  -C(X.X)        (C)lose RDP connection (hostnumber.VMnumner)
  102.  -q(X.X)        (q)uit (a)ll rdp sessions
  103.  
  104.  configuration and information
  105.  -us            (u)pdate virtual machines (s)pecifications
  106.  -ss            (s)how virtual machines (s)pecifications
  107.  -ls (X)        (l)ist virtual machines (s)tatus (X=<hostnumber|h|string>)
  108.  -sc            (s)how (c)onfiguration
  109.  -nc            (n)ew (c)onfiguration
  110.  -rc            (r)e(c)onfigure
  111.  -cc            (c)heck (c)onnections (-ls h)
  112.  
  113.  virtual machine configuration
  114.  -vmcfg (X.X) rdp       configure virtual machine (hostnumber.VMnumner) rdp functions
  115.  
  116.  -i             show script (i)nformation"
  117.  -m             (m)onocrome output"
  118.  
  119.  toggle RDP fullscreen: Strg+Alt+Enter
  120.  
  121. USAGE
  122. printf "$(tput setaf 1) $@ $(tput sgr0)\n\n"
  123. exit
  124. }
  125. #-------------------------------------------------------------------------------------------------------------------------------------------------------
  126. load_color_codes () {
  127.         # parse required colours for echo/printf usage: printf "%s\n" "Text in ${Red}red${Reset}, white and ${Blue}blue${Reset}."
  128.         Black='\033[0;30m'      &&      DGray='\033[1;30m'
  129.         LRed='\033[0;31m'       &&      Red='\033[1;31m'
  130.         LGreen='\033[0;32m'     &&      Green='\033[1;32m'
  131.         LYellow='\033[0;33m'    &&      Yellow='\033[1;33m'
  132.         LBlue='\033[0;34m'      &&      Blue='\033[1;34m'
  133.         LPurple='\033[0;35m'    &&      Purple='\033[1;35m'
  134.         LCyan='\033[0;36m'      &&      Cyan='\033[1;36m'
  135.         LLGrey='\033[0;37m'     &&      White='\033[1;37m'
  136.         Reset='\033[0m'
  137.  
  138.         BG='\033[47m'
  139.         FG='\033[0;30m'
  140.  
  141.         # parse required colours for sed usage: sed 's/status=sent/'${Green}'status=sent'${Reset}'/g' |\
  142.         if [[ $1 == sed ]]; then
  143.                 for i in $(cat $0 | sed -n '/^load_color_codes/,/FG/p' | tr "&" "\n" | grep "='"); do
  144.                         eval $(sed 's|\\|\\\\|g' <<< $i)                                                # sed parser '\033[1;31m' => '\\033[1;31m'
  145.                 done
  146.         fi
  147. }
  148. #-------------------------------------------------------------------------------------------------------------------------------------------------------
  149. script_information () {
  150.         printf "\n"
  151.         printf " Scriptname: $ScriptName\n"
  152.         printf " Version:    $Version \n"
  153.         printf " Location:   $(pwd)/$ScriptName\n"
  154.         printf " Filesize:   $(ls -lh $0 | cut -d " " -f5)\n"
  155.         printf "\n"
  156.         exit 0
  157. }
  158. #-------------------------------------------------------------------------------------------------------------------------------------------------------
  159. check_input_options () {
  160.  
  161.  
  162. #############TODO###############!
  163.  
  164. check_input_options () {
  165.  
  166.         # create available options list
  167.         InputOptionList=$(cat $ScriptFile | sed -n '/usage()/,/exit/p' | grep " -[[:alpha:]]" | awk '{print $3}' | grep "^\-")
  168.  
  169.         # check for valid input options
  170.         for Option in $@ ; do  
  171.                 if [[ -z $(grep -w -- "$Option" <<< "$InputOptionList") ]]; then
  172.                         InvalidOptionList=$(echo $InvalidOptionList $Option)
  173.                 fi
  174.         done
  175.  
  176.         # print invalid options and exit script_information
  177.         if [[ -n $InvalidOptionList ]]; then
  178.                 usage "invalid option: $InvalidOptionList"
  179.         fi
  180. }
  181.  
  182. ############################
  183. testing () {
  184.         # create available options list
  185.         AvailableOptionList=$(cat $ScriptName | sed -n '/USAGE/,/^exit/p' | grep "^ -[[:alpha:]]" | awk -F "(" '{print $1 $2}' | awk '{print $1}' | sed 's/X\./'[[:digit:]].*'/g' | cut -d "X" -f1 )
  186.  
  187.         # print
  188.         echo "$AvailableOptionList"
  189.  
  190.         for EntreredOption in $@ ; do
  191.                 for AvailableOption in $AvailableOptionList ; do
  192.  
  193.                         echo "|$EntreredOption|$AvailableOption|"
  194.  
  195.                         if [[ ! "$EntreredOption" == "$AvailableOption" ]]; then
  196.                                 echo $EntreredOption
  197.                         fi
  198.  
  199.                 done
  200.         done
  201.         exit
  202. }
  203. #testing
  204.  
  205.  
  206.         for EntreredOption in $@ ; do
  207.                 for AvailableOption in $InputOptionList ; do
  208.  
  209.                         echo "|$AvailableOption|$EntreredOption|"                               # If/ else doesnt work , even when the expression should match
  210.  
  211.                         #if [[ -n $(grep -- "$AvailableOption" <<< "$EntreredOption")  ]]; then
  212.                         if [[ "$EntreredOption" == "$AvailableOption" ]]; then                  # "-m" == "-m"   => if does not break the loop ? WHY !!!????
  213.                                 break
  214.                         fi
  215.  
  216.                         if [[ -n $(echo " $EntreredOption" | grep " $AvailableOption" )  ]]; then
  217.                                 break                          
  218.                         fi
  219.                         InvalidOptionList=$(echo -e "$InvalidOptionList \n $EntreredOption")
  220.  
  221.                 done
  222.         done
  223.  
  224.         InvalidOptionList=$(echo "$InvalidOptionList" | sed 's/ //g' | sort -u | tr "\n" " ")
  225.  
  226.         # print invalid options and exit script_information
  227.         if [[ -n $InvalidOptionList ]]; then
  228.                 usage "invalid option: $InvalidOptionList"
  229.         fi
  230. }
  231. #-------------------------------------------------------------------------------------------------------------------------------------------------------
  232. configure_dialog () {
  233.  
  234.         # create config file
  235.         ConfigParameterList=$(cat $0 | grep -A16 "configure_dialog () {" | grep "read -e -p \" Enter" | awk -F " " '{print $NF}')
  236.  
  237.         # display Var input prompt and default value, enter/edit value
  238.         printf "\n"
  239.                                                                                         # default value                                         # varname in configfile
  240.         read -e -p " Enter available hosts (user1@host1 user1@host2): "                 -i "${VboxHostLoginList}"                               VboxHostLoginList
  241.         read -e -p " Enter vbox manager user:                         "                 -i "${VboxManagerSSHKeyUser:-$(whoami)}"                VboxManagerSSHKeyUser
  242.         read -e -p " Enter configuration file:                        "                 -i "${ConfigFile:-$HOME/.vbox_mgr.cfg}"                 ConfigFile
  243.         read -e -p " Enter VM specifications storage file:            "                 -i "${VmSpecListFile:-$HOME/.vbox_mgr_vm_specs.list}"   VmSpecListFile
  244.         read -e -p " Enter connection timeout (seconds):              "                 -i "${SSHCheckConnectionTimeout:-10}"                   SSHCheckConnectionTimeout
  245.         if [[ -z $XfreeRDP ]] && [[ -z $(which xfreerdp) ]]; then
  246.                 XfreeRDP="not found, please install xfreerdp"
  247.         fi
  248.         read -e -p " Enter xfreerdp path:                             "                 -i "${XfreeRDP:-$(which xfreerdp)}"                     XfreeRDP
  249.         read -e -p " Enter screen resolution:                         "                 -i "${ScreenRes:-1650x900x32}"                          ScreenRes
  250.  
  251.         # print new Vars
  252.         printf "\n new configuration values: \n\n"
  253.         for i in $ConfigParameterList; do
  254.                 echo " $i=\""$(eval echo $(echo "$"$i))\"
  255.         done
  256.  
  257.         # check for existing config file
  258.         if [[ -s $ConfigFile  ]]; then
  259.                 printf "\n"
  260.                 read -e -p " overwrite existing configuration (y/n) " -i "y" OverwriteConfig
  261.                 if [[ $OverwriteConfig == [yY] ]]; then
  262.                         rm $ConfigFile
  263.                 else
  264.                         sed -i '/Reconfigure=true/d'  $ConfigFile
  265.                         sed -i '/CreateNewConfig=true/d'  $ConfigFile
  266.                         printf "\n existing configuration :\n\n"
  267.                         cat $ConfigFile
  268.                         exit
  269.                 fi
  270.         fi
  271.  
  272.         # write Vars to config file
  273.         for i in $ConfigParameterList; do
  274.         echo "$i=\""$(eval echo $(echo "$"$i))\" >> $ConfigFile
  275.         done
  276.  
  277.         printf " configuration saved in: $ConfigFile\n\n"
  278.         sed -i "s|^ ConfigFile=.*| ConfigFile=$ConfigFile|" $(readlink -f $0)    $HOME/.vbox_mgr.cfg                            #check for first run
  279.         chmod 600 $ConfigFile
  280.  
  281.         # check for configuration changes
  282.         if [[ ! $(md5sum $ConfigFile) == $ConfigfileMD5SumOld ]]; then
  283.                 printf "$(tput setaf 1) configuration changed - please update VM specs: $(tput sgr0)\n"
  284.                 printf "$(tput setaf 2) ( $0 -us ) $(tput sgr0)\n\n"
  285.         else
  286.                 printf "$(tput setaf 3) no configuration changes $(tput sgr0)\n\n"
  287.         fi
  288. }
  289. #-------------------------------------------------------------------------------------------------------------------------------------------------------
  290. set_vm_vars_from_configfileline () {                                                            # usage: set_vm_vars_from_configfileline <hostnumer.vmnumber>
  291.  
  292.         # load processingline from config file
  293.         ProcccessingLine=$(grep "^$1" <<< $VmSpecList)
  294.         if [[ -n $ProcccessingLine ]]; then    
  295.                 # set processing vars
  296.                 for Var in User=2 Host=3 VMID=4 RDPPort=5 CPU=6 RAM=7 OS=8 VMName=9 VMDisk=10 VMDiskSize=11 ;do
  297.                         VarName=$( cut -d "=" -f1 <<< $Var)
  298.                         ConfigfileField=$( cut -d "=" -f2 <<< $Var)
  299.                         eval $VarName=\"$(echo $ProcccessingLine | cut -d "|" -f$ConfigfileField)\"
  300.                 done
  301.                 VarLoadError=0
  302.         else
  303.                 printf "$(tput setaf 1)virtual machine $1 not found$(tput sgr0)\n"
  304.                 VarLoadError=1
  305.         fi
  306. }
  307. #-------------------------------------------------------------------------------------------------------------------------------------------------------
  308. check_config () {
  309.  
  310.         if   [[ -s $ConfigFile  ]] && [[ -z $(cat $ConfigFile | grep "Reconfigure=true\|CreateNewConfig=true") ]]; then
  311.                 # read config file
  312.                 source $ConfigFile
  313.  
  314.         elif [[ -s $ConfigFile  ]] && [[ -n $(cat $ConfigFile | grep "Reconfigure=true") ]]; then
  315.                 # read config and reconfigure
  316.                 source $ConfigFile
  317.                 configure_dialog
  318.  
  319.         elif [[ ! -s $ConfigFile  ]] || [[ -n $(cat $ConfigFile | grep "CreateNewConfig=true") ]]; then
  320.                 # create new config file
  321.                 rm $ConfigFile &> /dev/null
  322.                 configure_dialog
  323.                 exit
  324.         fi
  325. }
  326. #-------------------------------------------------------------------------------------------------------------------------------------------------------
  327. check_execution_user () {
  328.  
  329.         # check execution user
  330.         if [ ! "$(whoami)" = "$VboxManagerSSHKeyUser" ]; then
  331.                 printf "$(tput setaf 1) SSH key authentication user required $VboxManagerSSHKeyUser. You are $(whoami) \n$(tput sgr0)"
  332.                 exit 1
  333.         fi
  334. }
  335. #-------------------------------------------------------------------------------------------------------------------------------------------------------
  336. host_connection_check () {                                                              # VboxHostLogin var has to be set
  337.  
  338.         host_connection_check_thread () {
  339.  
  340.                 Host=$(echo $VboxHostLogin | cut -d "@" -f2)
  341.                 Host=${Host/127.0.0.1/localhost}
  342.                 User=$(echo $VboxHostLogin | cut -d "@" -f1)
  343.        
  344.                 # check for local execution on same user, no ssh connection required
  345.                 if [[ -n $( grep localhost <<< "$VboxHostLogin") ]]; then
  346.  
  347.                         if [[ ! $User == $(whoami) ]]; then
  348.                                 printf "$(tput setaf 1)unknown vbox user on local machine$(tput sgr0)"
  349.                                 return
  350.                         fi
  351.        
  352.                         HostVMListDetails=$(for i in $(VBoxManage list vms ) ; do vboxmanage showvminfo $i 2>> /dev/null ; done && echo VirtualHarddisks: && VBoxManage list hdds)
  353.  
  354.                 if [[ $? == 0 ]]; then
  355.                         printf "$(tput setaf 2)online$(tput sgr0)"
  356.                 else
  357.                         printf "$(tput setaf 1)unknown error$(tput sgr0)"
  358.                 fi
  359.  
  360.                 # check SSH connection to destination host
  361.                 else
  362.  
  363.                         SSHCheckConnectionTimeout=2
  364.                         CMDTimeout=$(($SSHCheckConnectionTimeout+1))
  365.  
  366.                         SSHOutoutLogfile="/tmp/.vbx-ssh.log"
  367.                         echo > $SSHOutoutLogfile
  368.                         timeout $CMDTimeout ssh -v -o ConnectTimeout=$SSHCheckConnectionTimeout $VboxHostLogin -E $SSHOutoutLogfile 2>&1 exit
  369.                         #TODO => use ssh return values for error output => https://support.microfocus.com/kb/doc.php?id=7021696
  370.                         SSHOutputV="$(cat $SSHOutoutLogfile)"          
  371.  
  372.                         if [[ -n $(grep "No route to host" <<< $SSHOutputV) ]] ; then  
  373.                                 printf "$(tput setaf 1)offline, host unreachable$(tput sgr0)"
  374.                         elif [[ -n $(grep "Connection established" <<< $SSHOutputV) ]] ; then  
  375.                                 printf "$(tput setaf 2) online$(tput sgr0)"
  376.                                 if   [[ -n $(grep "Authenticated to $Host" <<< $SSHOutputV) ]] ; then  
  377.                                         printf ",$(tput setaf 2) authentication ok $CheckMark$(tput sgr0)"
  378.                                 elif [[ -n $(grep "Next authentication method: password" <<< $SSHOutputV | tail -n 1) ]] ; then
  379.                                         printf ",$(tput setaf 1) authentication failed (ssh key) $(tput sgr0)"
  380.                                 elif [[ -n $(grep "Connection refused" <<< $SSHOutputV | tail -n 1) ]] ; then  
  381.                                         printf ",$(tput setaf 1) authentication error$(tput sgr0)"
  382.                                 fi
  383.                         else
  384.                                 printf "$(tput setaf 1)Unknown error$(tput sgr0)"
  385.                         fi
  386.                 fi
  387.         }
  388.  
  389.         # check all hosts from config file
  390.         if [[ $1 == ALL ]]; then
  391.                 for VboxHostLogin in $VboxHostLoginList ; do
  392.                           HostNumber=$(echo "$VboxHostLoginList" | tr  " " "\n" | nl | grep -w "$VboxHostLogin" | awk '{print $1}')
  393.                           printf "Connection check for host #$HostNumber $VboxHostLogin: " | awk -F ":"  '{printf " %-65s %s %s ",$1, $2, $3}'
  394.                           host_connection_check_thread
  395.                           printf "\n"
  396.                 done
  397.         else
  398.                 host_connection_check_thread
  399.         fi
  400. }
  401. #-------------------------------------------------------------------------------------------------------------------------------------------------------
  402. list_VM_status () {
  403.  
  404.         AvailableHosts=$(echo "$VboxHostLoginList" | tr " " "\n" | nl |sed 's/^[[:blank:]]*//')
  405.  
  406.         # create specific hostlist ( -ls <number|host|string )
  407.         ProcessingHosts=$(for i in $( grep -v "ls" <<< $ListVMStatus) ; do
  408.  
  409.                                 # check for host number input
  410.                                 if   [[ -z $(sed 's/[[:digit:]]//g' <<< $i)  ]]; then
  411.                                         if [[ -n $(echo "$AvailableHosts" | grep ^$i)  ]]; then
  412.                                                 echo "$AvailableHosts" | grep ^$i
  413.                                         else
  414.                                                 # write error message
  415.                                                 echo "unknown host number: $i"
  416.                                         fi
  417.  
  418.                                 # check for string input ( IP,host,user, etc.)
  419.                                 elif [[ -n $(echo "$AvailableHosts" | grep $i ) ]]; then
  420.                                         echo "$AvailableHosts" | grep $i
  421.  
  422.                                 # write error message  
  423.                                 else
  424.                                         echo "string not found in hostlist: $i"
  425.                                 fi
  426.                         done | sort -u )
  427.  
  428.         # print input errors
  429.         if [[ -n $(grep -v ^[[:digit:]] <<< $ProcessingHosts) ]]; then
  430.                 usage "$( grep -v ^[[:digit:]] <<< "$ProcessingHosts")"
  431.         fi
  432.  
  433.         # set/clear VboxHostLoginList ( set all hosts from config for -ls only command )
  434.         if [[ $ListVMStatus == "-ls" ]]; then
  435.                 VboxHostLoginList=$VboxHostLoginList
  436.         else
  437.                 VboxHostLoginList=
  438.         fi
  439.  
  440.         # rewrite VboxHostLoginList. check entered hosts/strings.
  441.         for i in $( awk -F "^[[:digit:]]" '{print $2}' <<< $ProcessingHosts ) ; do
  442.                 # check for unknown hosts/strings
  443.                 if [[ -z $(echo "$AvailableHosts" | grep -w $i ) ]]; then
  444.                         echo "unknown host"
  445.                 fi
  446.  
  447.                 # write specefied hosts to VboxHostLoginList
  448.                 if [[ -n $(echo "$AvailableHosts" | grep $i ) ]]; then
  449.                         VboxHostLoginList=$(echo $VboxHostLoginList $i)
  450.                 fi
  451.         done
  452.  
  453.         # get virtual machine list from $VboxHostLoginList
  454.         for VboxHostLogin in $(echo "$VboxHostLoginList" | tr " " "\n") ; do
  455.                 Host=$(echo $VboxHostLogin | cut -d "@" -f2)
  456.                 Host=${Host/127.0.0.1/localhost}
  457.                 User=$(echo $VboxHostLogin | cut -d "@" -f1)
  458.                 HostNumber=$(echo "$AvailableHosts" | grep $VboxHostLogin | awk '{print $1}')
  459.  
  460.                 # check for config errors ( missing VMs ) in $VmSpecListFile
  461.                 if [[ -n $(cat $VmSpecListFile | grep $VboxHostLogin | grep UpdateSpecsError ) ]]; then
  462.                         printf  "|VMs on Host $HostNumber ($VboxHostLogin): |" | awk -F "|" '{printf "\n%s %-42s %s ",$1, $2, $3}' >&2
  463.                         printf  "$(tput setaf 1)|No virtual machines detected $(tput setaf 3)| update virtual machine specs: $0 -us|\n $(tput sgr0)" |\
  464.                                 awk -F "|" '{printf "\n%s %-47s %s ",$1, $2, $3}' >&2
  465.                         continue
  466.                 else
  467.                         printf  "|VMs on Host $HostNumber ($VboxHostLogin): |" | awk -F "|" '{printf "\n%s %-42s %s ",$1, $2, $3}' >&2
  468.                 fi
  469.  
  470.                 #TODO count calculate max caracter count for $VboxHostLogin VMName auto adjust slot width count VboxHostLogin
  471.                 SlotWidthCountVboxHostLogin=$(echo "$VmSpecList" | cut -d "|" -f3 | sed 1d | sort -u | awk '{ print length, $2 }' | sort -n | tail -1)
  472.                 SlotWidthCountVMName=$(echo "$VmSpecList" | grep -v "|||" |  cut -d "|" -f9 | sed 1d | sort -u | awk '{ print length, $2 }' | sort -n | tail -1)
  473.  
  474.                 ## Create HostVMList
  475.                 # create virtual machine list from local host
  476.                 if [[ $Host == "localhost" ]]; then
  477.  
  478.                         if [[ ! $User == $(whoami) ]]; then
  479.                                 printf "$(tput setaf 1) unknown user on local machine$(tput sgr0)\n" >&2
  480.                                 continue
  481.                         fi
  482.  
  483.                         HostVMList=$(VBoxManage list vms && echo running vms: && VBoxManage list runningvms)
  484.                         HostVMListCMDExitCode=$?
  485.  
  486.                         # create virtual machine list from remote host
  487.                 else
  488.                         HostVMList=$(timeout $SSHCheckConnectionTimeout ssh $VboxHostLogin "VBoxManage list vms && echo running vms: && VBoxManage list runningvms" 2>/dev/null )
  489.                         HostVMListCMDExitCode=$?
  490.                 fi
  491.  
  492.                 # if host is unreachable run host connection check
  493.                 if [[ ! $HostVMListCMDExitCode == 0 ]]; then
  494.                         HostConnectionCheckOutput=$(host_connection_check $VboxHostLogin)
  495.                         printf "$HostConnectionCheckOutput\n" >&2
  496.  
  497.                         # create virtual machine list from local VmSpecList
  498.                         HostVMList=$(echo "$VmSpecList"  | grep "$Host" |sed '1d' |  awk -F "|" '{printf "\"%s\" {%s}\n", $9 , $4}')
  499.                 else
  500.                         printf "$(tput setaf 2)host connected$(tput sgr0)\n" >&2
  501.                         HostConnectionCheckOutput=
  502.                 fi
  503.                 ## Create HostVMList completed
  504.  
  505.                 # proccess HostVMList for function output
  506.                 SAVEIFS=$IFS
  507.                 IFS=$(echo -en "\n\b")
  508.                 HostVMRunIDList=$(echo "$HostVMList" | grep -A50 "running vms:" | awk '{print $NF}' | sed '1,1d' )
  509.                 HostVMRunIDList=${HostVMRunIDList:-none}
  510.        
  511.                 for VM in $HostVMList; do
  512.  
  513.                         # skip extra output for running vm entires in HostVMList, entries are used to mark running vms from HostVMList
  514.                         if [[ -n $(echo $VM| grep "running vms:")  ]]; then
  515.                                 break
  516.                         fi
  517.        
  518.                         VMName=$(echo $VM | cut -d '"' -f2)
  519.                         VMID=$(echo $VM | cut -d '"' -f3 | sed 's/ {//g' | tr -d "}")
  520.        
  521.                         # create VM status entry ( running vms )
  522.                         for RunningVMID in $HostVMRunIDList ;do
  523.                                 VMStatus="$(tput setaf 1) Offline $(tput sgr0)"
  524.                                 if [[ $(echo $VM | grep "$RunningVMID") ]]; then
  525.                                         VMStatus="$(tput setaf 2) Online $(tput sgr0)"
  526.                                         break
  527.                                 fi     
  528.                         done
  529.        
  530.                         if [[ -n $HostConnectionCheckOutput ]]; then
  531.                                 VMStatus="$(tput setaf 1) Unkown $(tput sgr0)"
  532.                         fi
  533.  
  534.                         VMNumber=$(echo "$VmSpecList" | grep $VMID | awk -F "|" '{print $1 }'| head -n1)
  535.  
  536.                         echo "|$VMNumber|$VMName|$VboxHostLogin|$VMID|$VMStatus|"
  537.                 done
  538.                 IFS=$SAVEIFS
  539.  
  540.                             #printf "Nr.|Virtual Machine Name|Host|ID|Status|\n"
  541.         done | awk -F "|" '{printf " %-4s %-38s %-20s %s %s %s %s \n", $2, $3, $4, $5, $6, $8, $7}'
  542.         printf "\n"
  543. }
  544. #-------------------------------------------------------------------------------------------------------------------------------------------------------
  545. update_vm_specifications () {
  546.  
  547.         echo "specs updated on $(date +%s)" > $VmSpecListFile
  548.         chmod 600  $VmSpecListFile
  549.  
  550.         # load vm specs from all hosts
  551.         for VboxHostLogin in $VboxHostLoginList ; do
  552.  
  553.                 HostCounter=${HostCounter:-0}
  554.                 HostCounter=$(($HostCounter+1))
  555.                 Host=$(echo $VboxHostLogin | cut -d "@" -f2)
  556.                 User=$(echo $VboxHostLogin | cut -d "@" -f1)
  557.        
  558.                 printf "\n%-70s" " updating virtual machine specifications from $VboxHostLogin"  >&2
  559.  
  560.                 CMDTimeout=$(($SSHCheckConnectionTimeout+1))
  561.  
  562.                 # get host vm information details - local execution - no ssh connection required
  563.                 if [[ -n $( grep localhost <<< "$VboxHostLogin") ]]; then
  564.        
  565.                         # check for correct user on local machine
  566.                         if [[ ! $User == $(whoami) ]]; then
  567.                                 host_connection_check $VboxHostLogin >&2
  568.                                 continue
  569.                         fi
  570.                
  571.                         HostVMListDetails=$(for i in $(VBoxManage list vms ) ; do vboxmanage showvminfo $i 2>> /dev/null ; done && echo VirtualHarddisks: && VBoxManage list hdds)
  572.        
  573.                         if [[ $? == 0 ]]; then
  574.                                 printf "$(tput setaf 2)done$(tput sgr0)" >&2
  575.                         else
  576.                                 printf "$(tput setaf 1)unknown error$(tput sgr0)" >&2
  577.                         fi
  578.        
  579.                 # get host vm information details - ssh connection required
  580.                 else
  581.                 HostVMListDetails=$(timeout $CMDTimeout ssh $VboxHostLogin 2>/dev/null  \
  582.                         '''for i in $(VBoxManage list vms ) ; do vboxmanage showvminfo $i 2>> /dev/null ; done && echo VirtualHarddisks: && VBoxManage list hdds ''')
  583.        
  584.                         # if host is offline run single connection check and quit loop
  585.                         if [[ ! $? == 0 ]]; then
  586.                                 UpdateSpecsErrorMSG=UpdateSpecsError
  587.                                 host_connection_check $VboxHostLogin >&2
  588.                         else
  589.                                 printf "$(tput setaf 2)done$(tput sgr0)" >&2
  590.                         fi
  591.                 fi
  592.        
  593.                 printf "\n$HostCounter.0||||||||Virtual Machines located on: $VboxHostLogin $UpdateSpecsErrorMSG\n"
  594.                 UpdateSpecsErrorMSG=
  595.        
  596.                 # Filter VM parameter
  597.                 VMUUIDList=$(echo "$HostVMListDetails" | grep -A4 "^Name:" | grep UUID: | awk '{print $2}')
  598.        
  599.                 SAVEIFS=$IFS
  600.                 IFS=$(echo -en "\n\b")
  601.        
  602.                 VMNumber=0
  603.        
  604.                 for VMUUID in $VMUUIDList ; do
  605.                         VMNumber=$(($VMNumber+1))
  606.                         VMName=$(echo "$HostVMListDetails" | grep -B4 $VMUUID | grep "Name:" | awk '{print $2}')
  607.                         GuestOS=$(echo "$HostVMListDetails" | grep -B1 $VMUUID | grep  "Guest OS:" | awk '{print $3,$4}' | sed 's/ //')
  608.                         VMCPU="$(echo "$HostVMListDetails" | grep -A100 $VMUUID | grep "CPU exec cap:" | awk '{print $4}')%"
  609.                         VMRAM=$(echo "$HostVMListDetails" | grep -A100 $VMUUID | grep "Memory size" | awk '{print $3}')
  610.                         VMNet=$(echo "$HostVMListDetails" | grep "NIC 1:                       MAC:" | awk '{print $3}')                                                # not needed
  611.                         VMGuiRes=$(echo "$HostVMListDetails" | grep -A100 $VMUUID | grep "Video mode:" | awk '{print $3}')                                              # value only if vm runs,
  612.                         RDPPort=$(echo "$HostVMListDetails" | grep -A150 $VMUUID | grep "VRDE port:" | awk '{print $3}')                                                # VBox 6.x
  613.                         if [[ -z $RDPPort ]]; then
  614.                                 RDPPort=$(echo "$HostVMListDetails" | grep -A150 $VMUUID | grep "VRDE property .* : TCP/Ports  = " | awk '{print $NF}' | tr -d '"')     # VBox 5.X
  615.                         fi
  616.                         RDPPort=${RDPPort:- none}
  617.        
  618.                         # write new line for each virtual disk
  619.                         VMDiskPathAll=$(echo "$HostVMListDetails" | grep -A100 $VMUUID | grep "vmdk\|vdi" | awk -F ": " '{print $2}' | sed 's/ (UUID.*$//' )
  620.                         for VMDiskPath in $VMDiskPathAll ; do
  621.                                 VMDiskSize="$(echo "$HostVMListDetails" | grep -A100 "VirtualHarddisks:" | grep -A2 $VMDiskPath | grep "Capacity" | awk '{print $2}')MB"
  622.                                 printf "$HostCounter.$VMNumber|$User|$Host|$VMUUID|:$RDPPort|$VMCPU|$VMRAM|$GuestOS|$VMName|$VMDiskPath|$VMDiskSize|\n"
  623.                         done
  624.                 done
  625.                 IFS=$SAVEIFS
  626.        
  627.          done  >> $VmSpecListFile
  628.          printf "\n\n"
  629. }
  630. #-------------------------------------------------------------------------------------------------------------------------------------------------------
  631. show_vm_specifications () {
  632.         #        1     2    3                        4   5    6     7        8    9         10      11
  633.          (echo "Nr.| User|Host    |               UUID|  RDP| CPU|  RAM|     OS|VM-Name|VM-Disk-Path| Disk" && echo "$VmSpecList" | grep -v "specs updated on") \
  634.          | awk -F "|" '{printf " %3-s %-38s %20s%s %-8s %-15s %5s %8s %10s %-37s %s %s \n",$1, $9, $3, $5, $2, $8, $6, $7, $11, $4, $10 ,$1}'
  635.          printf "\n last update: $(date -d @$(echo $(echo "$VmSpecList"  | grep 'specs updated on ') | awk -F 'specs updated on ' '{printf $NF}'))\n"
  636. }
  637. #-------------------------------------------------------------------------------------------------------------------------------------------------------
  638. parse_numeric_processing_input () {                                                              # usage: parse_numeric_processing_input <1.1|1-X.1-X>
  639.  
  640.         proccessing_vm_input () {
  641.  
  642.                 VMs=$( cut -d "." -f2 <<< $HostVMProcessing)
  643.  
  644.                 # processing vms
  645.                 if [[ -n $(grep "-" <<< $VMs) ]]; then
  646.                         for VMs in $(seq $(sed 's/-/ /' <<< $VMs)); do
  647.                                 printf "$Host.$VMs\n"
  648.                         done
  649.                 else
  650.                         printf "$Host.$VMs\n"
  651.                 fi
  652.         }
  653.  
  654.         # proccessing input options
  655.         for HostVMProcessing in $( tr " " "\n" <<< $@) ; do
  656.                 Host=$( cut -d "." -f1 <<< $HostVMProcessing)
  657.  
  658.                 # processing hosts
  659.                 if [[ -n $(grep "-" <<< $Host) ]]; then
  660.                         for Host in $(seq $(sed 's/-/ /' <<< $Host) | tr "\n" " "); do
  661.                         proccessing_vm_input
  662.                         done
  663.                 else
  664.                         proccessing_vm_input
  665.                 fi
  666.         done
  667. }
  668. #-------------------------------------------------------------------------------------------------------------------------------------------------------
  669. proccessing_vm_control_options () {
  670.  
  671.         for VMControlOption in -b -rv -pv -r -h -s -p -c -C -q ; do
  672.  
  673.                 # read option line
  674.                 OptionProccessingSyntax=$(echo $@ | sed 's/ -/ \n-/g'| grep '\'$VMControlOption'[0-9]' | cut -c 3-100 | tr -d [[:alpha:]])
  675.  
  676.                 # check for correct numeric input
  677.                 if [[ -n $(grep [[:alpha:]] <<< $OptionProccessingSyntax) ]]; then
  678.                         printf "Syntax error for option: $VMControlOption$OptionProccessingSyntax\n" >&2
  679.                         continue
  680.                 elif [[ -n $(grep "\." <<< $OptionProccessingSyntax) ]] && [[ -n $(echo " $OptionProccessingSyntax" | grep '\'$VMControlOption) ]]; then
  681.                         printf "\n$(tput setaf 1)Error: Virtual machine $HostVMNumner not found$(tput sgr0)" >&2
  682.                         continue       
  683.                 elif [[ -n $(echo $OptionProccessingSyntax | grep "\.0" ) ]]; then
  684.                         printf "\n$(tput setaf 1)Error: $OptionProccessingSyntax is a Host$(tput sgr0)" >&2
  685.                         continue
  686.                 elif [[ -n $(echo $OptionProccessingSyntax | grep "^0\." ) ]]; then
  687.                         printf "\n$(tput setaf 1)Error: $OptionProccessingSyntax is not a Host$(tput sgr0)" >&2
  688.                         continue
  689.                 elif [[ -n $(echo $OptionProccessingSyntax | grep "0-" ) ]]; then
  690.                         printf "\n$(tput setaf 1)Error: $OptionProccessingSyntax is not a Host$(tput sgr0)" >&2
  691.                         continue
  692.                 fi
  693.        
  694.                 # parse multi inputs for single processing and load var for each selected VM Number for further proccessing | clear for 0.0 input
  695.                 #for HostVMNumner in $(parse_numeric_processing_input "$OptionProccessingSyntax" | sed '/.0/d' | sed '/0./d') ; do      #X.10 does not work
  696.                 for HostVMNumner in $(parse_numeric_processing_input "$OptionProccessingSyntax" ) ; do
  697.                         set_vm_vars_from_configfileline $(parse_numeric_processing_input "$HostVMNumner")
  698.                         if [[ $VarLoadError == 1 ]]; then
  699.                                 continue
  700.                         fi
  701.  
  702.                         # connect VBOX host, execute command
  703.                         if [[ $VMControlOption == "-b" ]]; then
  704.                                 printf " start virtual machine: $VMName\n"
  705.                                 ssh $User@$Host -t -t vboxmanage startvm --type headless $VMID
  706.                         fi
  707.  
  708.                         # connect VBOX host, execute command
  709.                         if [[ $VMControlOption == "-h" ]]; then
  710.                                 printf " hibernate virtual machine: $VMName\n"
  711.                                 ssh $User@$Host -t -t vboxmanage controlvm $VMID savestate
  712.                         fi
  713.  
  714.                         # connect VBOX host, execute command
  715.                         if [[ $VMControlOption == "-pv" ]]; then
  716.                                 printf " pause virtual machine: $VMName\n"
  717.                                 ssh $User@$Host -t -t vboxmanage controlvm $VMID pause
  718.                         fi
  719.  
  720.                         # connect VBOX host, execute command
  721.                         if [[ $VMControlOption == "-rv" ]]; then
  722.                                 printf " resume virtual machine: $VMName\n"
  723.                                 ssh $User@$Host -t -t vboxmanage controlvm $VMID resume
  724.                         fi
  725.        
  726.                         # connect VBOX host, execute command
  727.                         if [[ $VMControlOption == "-s" ]]; then
  728.                                 printf "shutdown (acpi) virtual machine: $VMName\n"
  729.                                 ssh $User@$Host -t -t vboxmanage controlvm $VMID acpipowerbutton
  730.                         fi
  731.  
  732.                         # connect VBOX host, execute command
  733.                         if [[ $VMControlOption == "-r" ]]; then
  734.                                 printf " reset off virtual machine: $VMName\n"
  735.                                 ssh $User@$Host -t -t vboxmanage controlvm $VMID reset
  736.                         fi
  737.  
  738.                         # connect VBOX host, execute command
  739.                         if [[ $VMControlOption == "-p" ]]; then
  740.                                 printf " power off virtual machine: $VMName\n"
  741.                                 ssh $User@$Host -t -t vboxmanage controlvm $VMID poweroff
  742.                         fi
  743.  
  744.                         # connect VBOX host, execute command
  745.                         if [[ $VMControlOption == "-k" ]]; then
  746.                                 printf " kill virtual machine: $VMName\n"
  747.                                 ssh $User@$Host -t -t VBoxManage startvm $VMID --type emergencystop
  748.                         fi
  749.  
  750.                         # processing options
  751.                         if [[ $VMControlOption == "-c" ]]; then
  752.                                 printf " connect virtual machine via rdp protokoll: $VMName\n"
  753.                                 printf " execute => xfreerdp -u $User -p V\!rtlMchn  -g $ScreenRes -T $VMName @ $Host$RDPPort ($User) $Host$RDPPort & \n"
  754. #                               xfreerdp -u $User -p V\!rtlMchn  -g $ScreenRes -T "$VMName @ $Host$RDPPort ($User)" $Host$RDPPort
  755.  
  756.                                 # Temporary file to store the command's output
  757.                                 local output_file=$(mktemp)
  758.                                 # Run xfreerdp in the background and redirect output to the temporary file
  759.                                 xfreerdp -u "$User" -p "$XfreeRDPPassword" -g "$ScreenRes" -T "$VMName @ $Host$RDPPort ($User)" "$Host$RDPPort" > "$output_file" 2>&1 &
  760.                                 # Save the background process PID
  761.                                 local pid=$!
  762.                                 # Small delay to capture potential error messages
  763.                                 sleep 1
  764.                                 # Check if the background process is still running
  765.                                 #       if grep -q "Do you trust the above certificate?" "$output_file"; then   # check only for certificate error
  766.                                 if ! kill -0 "$pid" 2>/dev/null; then
  767.                                         echo "An error occurred. Starting xfreerdp in the foreground..."
  768.                                         # Re-run the command in the foreground to allow interactive input
  769.                                         xfreerdp -u "$User" -p "$XfreeRDPPassword" -g "$ScreenRes" -T "$VMName @ $Host$RDPPort ($User)" "$Host$RDPPort"
  770.                                 else
  771.                                         echo "Command started successfully in the background. Process ID: $pid"
  772.                                 fi
  773.                                 # Remove the temporary file
  774.                                 rm -f "$output_file"
  775.                         fi
  776.  
  777.                         # processing options
  778.                         if [[ $VMControlOption == "-C" ]]; then
  779.                                 printf " close RDP connection : $VMName\n "
  780.                                 #ssh $User@$Host -t -t vboxmanage controlvm $VMID poweroff
  781.                                 # connect VBOX host, execute command_check
  782.                         fi
  783.        
  784.                         # processing options
  785.                         if [[ $VMControlOption == "-q" ]]; then
  786.                                 printf " (q)uit (a)ll rdp sessions: $VMName\n "
  787.                                 #ssh $User@$Host -t -t vboxmanage controlvm $VMID poweroff
  788.                                 # connect VBOX host, execute command_check
  789.                         fi
  790.                 done
  791.  
  792.         done
  793.  
  794.         #echo "---------------------------------------------"
  795.         #echo "|$Host|$User|$VMID|$RDPPort|$CPU|$RAM|$OS|$VMName|$VMDisk|$VMDiskSize|"
  796.         #echo "---------------------------------------------"
  797.        
  798.         printf "\n"
  799. }
  800. #-------------------------------------------------------------------------------------------------------------------------------------------------------
  801. ############################################################################################################
  802. #############################################   start script   #############################################
  803. ############################################################################################################
  804. #-------------------------------------------------------------------------------------------------------------------------------------------------------
  805.  
  806. #       check_input_options $@  #TODO if expressions does execute even if expressions match "-m" == -m"
  807.  
  808. #-------------------------------------------------------------------------------------------------------------------------------------------------------
  809.  
  810.         check_config
  811.  
  812. #-------------------------------------------------------------------------------------------------------------------------------------------------------
  813.  
  814.         check_execution_user
  815.  
  816. #-------------------------------------------------------------------------------------------------------------------------------------------------------
  817.  
  818.         # check for monochrome output
  819.         Reset='\033[0m'
  820.         if [[ -z $Monochrome ]]; then
  821.                 load_color_codes
  822.         fi
  823.  
  824. #-------------------------------------------------------------------------------------------------------------------------------------------------------
  825.  
  826.         # check help dialog
  827.         if [[ -z "$1" ]] || [[ -n "$HelpDialog" ]] ; then usage "help dialog" ; fi
  828.  
  829. #-------------------------------------------------------------------------------------------------------------------------------------------------------
  830.  
  831.         # check for script information
  832.         if [[ -n $ScriptInformation ]]; then script_information ; fi
  833.  
  834. #-------------------------------------------------------------------------------------------------------------------------------------------------------
  835. ############################################################################################################
  836. #############################################   check config   #############################################
  837. ############################################################################################################
  838.  
  839.         # load virtual machine specs to var
  840.         VmSpecList=$(cat "$VmSpecListFile")
  841.  
  842. #-------------------------------------------------------------------------------------------------------------------------------------------------------
  843.  
  844.         # processing main configuration options
  845.         if [[ -n $NewConfig ]]; then
  846.                 printf "$(tput setaf 3) Create new configuration$(tput sgr0)"
  847.                 rm $ConfigFile &> /dev/null
  848.                 sleep 0.2
  849.                 $0 -
  850.                 exit
  851.         fi
  852.  
  853.         if [[ -n $Reconfigure ]]; then
  854.                 printf "$(tput setaf 3) Start reconfiguration:$(tput sgr0)"
  855.                 echo "Reconfigure=true" >> $ConfigFile
  856.                 check_config
  857.         fi
  858.  
  859.         if [[ -n $ShowConfig ]]; then
  860.                 printf "$(tput setaf 3) Configfile ($ConfigFile) content: \n$(tput sgr0)"
  861.                 cat $ConfigFile
  862.                 printf "\n"ConfigLine
  863.         fi
  864.  
  865.         if [[ -n $CheckConfig ]] || [[ -n $(grep h <<< $ListVMStatus) ]]; then
  866.                 printf "$(tput setaf 3) Start host connection check: \n$(tput sgr0)"
  867.                 host_connection_check ALL
  868.         fi
  869.  
  870.         if [[ -n $UpdateVMSpecs ]]; then
  871.                 printf "$(tput setaf 3) Start virtual machine spec update: $(tput sgr0)"
  872.                 update_vm_specifications
  873.         fi
  874.  
  875.         if [[ -n $ShowVMSpecs ]]; then
  876.                 show_vm_specifications
  877.         fi     
  878.        
  879.         if [[ -n $ListVMStatus ]] && [[ ! $(grep h <<< $ListVMStatus) ]]; then
  880.                 list_VM_status
  881.         fi
  882.  
  883. #-------------------------------------------------------------------------------------------------------------------------------------------------------
  884.  
  885.         # processing VM specific configuration options
  886.         if [[ -n $VMConfigSet ]]; then
  887.  
  888.                 printf "$(tput setaf 3) Start virtual machine configuration mode\n$(tput sgr0)"
  889.  
  890.                 # check for correct input VM Number
  891.                 VMConfigSetCommand=$(echo "$VMConfigSet" | tr " " "\n" | grep -v [[:digit:]] )
  892.                 VMConfigSetProcessingHosts=$(echo "$VMConfigSet" | sed 's/'$VMConfigSetCommand'//' 2> /dev/null)
  893.        
  894.                 # check for input command
  895.                 if   [[ $? == 1 ]]; then
  896.                         usage " missing command: $(echo "$@") <cmd> "
  897.                 elif [[ -z $VMConfigSetProcessingHosts ]]; then
  898.                         usage " missing virtual machine: -vmcfg <hostnumber.vmnumber> $VMConfigSetCommand "    
  899.         fi
  900. ConfigLine
  901.         # parse multi inputs for single processing and load var for each selected VM number for further proccessing | clear for 0.0 input
  902.         #for HostVMNumner in $(parse_numeric_processing_input "$VMConfigSetProcessingHosts" | sed '/.0/d' | sed '/0./d') ; do
  903.         for HostVMNumner in $(parse_numeric_processing_input "$VMConfigSetProcessingHosts" ) ; do
  904.                 set_vm_vars_from_configfileline $(parse_numeric_processing_input "$HostVMNumner")
  905.  
  906.                 # skip proccessing loop when error occurs
  907.                 if [[ $VarLoadError = 1 ]]; then
  908.                         continue
  909.                 fi
  910.  
  911.                 # get virtual machines status - one/first time run
  912.                 if [[ -z $GetVMStatus ]]; then
  913.                         printf "$(tput setaf 3) Check virtual machine status \n$(tput sgr0)"
  914.                         VMStatusList=$(list_VM_status 2>&1)
  915.                         GetVMStatus=done
  916.                 fi
  917.  
  918.                 ProcccessingVMLine=$(echo "$VMStatusList" | grep $VMID)
  919.  
  920.                 # check for running virtual machine
  921.                 if [[ -n $(echo "$ProcccessingVMLine" | grep Online) ]]; then
  922.                         printf "\n$(tput setaf 1) Virtual machine is running, shutdown for configuration \n$(tput sgr0)"
  923.                         printf "$ProcccessingVMLine\n"
  924.                         continue
  925.                 fi
  926.  
  927.                 # configure vm remote desktop paramerter | infosource : https://www.virtualbox.org/manual/ch07.html
  928.                 if [[ $VMConfigSetCommand == rdp ]]; then
  929.  
  930.                         # configure vrdp parameter
  931.                         RDPport=$(($(echo 10"$(echo $HostVMNumner | cut -d "." -f1)00")+$(echo $HostVMNumner | cut -d "." -f2)))
  932.  
  933.                         RDPServerListenAddress=$(ssh $User@$Host hostname -I)                   # 0.0.0.0 to accept all connections, security risk
  934.                         Executelist_VM_status=true
  935.  
  936.                         ConfigLine="--vrde on --vrdeauthtype external --vrdeaddress $RDPServerListenAddress --vrdeport $RDPport"
  937.  
  938.                         printf "\n$(tput setaf 3) Set configuration:$(tput sgr0) vboxmanage modifyvm $VMID $ConfigLine\n"
  939.                         printf "$ProcccessingVMLine\n"
  940.                        
  941.                         # enable / set vrdp functions
  942.                         printf " execute => ssh $User@$Host -t -t vboxmanage modifyvm $VMID $ConfigLine 2> /dev/null \n"
  943.                         ssh $User@$Host -t -t vboxmanage modifyvm $VMID $ConfigLine 2> /dev/null
  944.                 fi
  945.         done
  946.  
  947.         printf "\n update virtual machines specifications to apply changes ( $(basename $0) -us )"
  948.  
  949.         fi
  950.  
  951. #-------------------------------------------------------------------------------------------------------------------------------------------------------
  952.  
  953.         proccessing_vm_control_options $@  # to do -b 2.3 4.4 doenst work => works -b2.3 4.4
  954.  
  955. #------------------------------------------------------------------------------- ------------------------------------------------------------------------
  956.  
  957. exit 0
  958.  
  959. #------------------------------------------------------------------------------------------------------------
  960. ############################################################################################################
  961. ##########################################   changelog / notice  ###########################################
  962. ############################################################################################################
  963. #------------------------------------------------------------------------------------------------------------
  964.  
  965. # Configure Virtualbox Host for RDP connection, execute directly on host:
  966. #####################################################################################################
  967. # => ConfigLine="--vrde on --vrdeauthtype external --vrdeaddress 192.168.1.80 --vrdeport 10000"
  968. # => vboxmanage list vms
  969. # => vboxmanage modifyvm 6047df41-7c93-43f2-9dac-0de61e35f16d $ConfigLine
  970. #####################################################################################################
  971.  
  972.  
  973. # known issues :
  974. #
  975. # RDP connection failure solutions :
  976. # - Is RDP connection in VM configs enabled  ?
  977. # - Is RDP connection for non local connecntion available ( Display => Remote desktop server port => LAN IP instead 127.0.0.1 )
  978. #       => VBoxManage modifyvm "[VM_NAME]" --vrdeaddress [0.0.0.0|LAN_IP|$(hostname -I)]
  979.  
  980.  
  981. # create vbox internal rdp user => https://www.marcomadeit.com/2017/02/26/set-user-authentication-in-virtualbox-remote-desktop-rdp/
  982. #                               => https://docs.oracle.com/en/virtualization/virtualbox/6.1/admin/vrde.html
  983. #                               => https://github.com/phpvirtualbox/phpvirtualbox/issues/31
  984.  
  985.  

Quellcode

Hier kannst du den Code kopieren und ihn in deinen bevorzugten Editor einfügen. PASTEBIN_DOWNLOAD_SNIPPET_EXPLAIN