#!/usr/bin/env bash # Function to wait for SSH to become available wait_for_ssh() { local host_ip=$1 local user=$2 local password=$3 local retry_interval=${4:-5} # Default retry interval is 5 seconds local max_retries=${5:-20} # Default maximum retries is 20 (0 for infinite) echo "Waiting for SSH to become available on $host_ip..." local retry_count=0 while true; do # Try to connect via SSH sshpass -p "$password" ssh -o StrictHostKeyChecking=no "$user@$host_ip" "exit" # Check the exit status of the SSH command if [ $? -eq 0 ]; then echo "SSH is ready on $host_ip!" return 0 fi # Increment retry count ((retry_count++)) # Exit if maximum retries are reached if [ $max_retries -ne 0 ] && [ $retry_count -ge $max_retries ]; then echo "Maximum retries reached. SSH is not available." return 1 fi echo "SSH not ready. Retrying in $retry_interval seconds... (Attempt $retry_count)" sleep $retry_interval done } # Function to execute a script on a remote server using sshpass execute_remote_script() { local host="$1" local user="$2" local password="$3" local script_path="$4" local vnc_password="$5" local data_folder="$6" # Check if all required arguments are provided if [ -z "$host" ] || [ -z "$user" ] || [ -z "$password" ] || [ -z "$script_path" ] || [ -z "$vnc_password" ]; then echo "Usage: execute_remote_script [data_folder]" return 1 fi echo "VNC password exported to VM: $vnc_password" data_folder_path="$VM_SHARED_FILES_PATH/$data_folder" echo "Data folder path in VM: $data_folder_path" # Read the script content and prepend the shebang script_content="#!/usr/bin/env bash\n" if [ -n "$data_folder" ]; then script_content+="export VNC_PASSWORD='$vnc_password'\n" script_content+="export DATA_FOLDER_PATH='$data_folder_path'\n" fi script_content+="$(<"$script_path")" # Use a here-document to send the script content sshpass -p "$password" ssh -o StrictHostKeyChecking=no "$user@$host" "bash -s" <