#!/bin/bash # # Description : This is used to update the Server details in the SeverInfo.json file. # In order to ensure the communication between the Agent & the Server. # # Usage : configurePMPAgentServerCommunication.sh [server_name] [server_ip] [http_port] ['http'/'https'] # # Returns : (Nothing) # product_name="PMP" settings_file="/etc/desktopcentralagent/dcagentsettings.json" agent_dir_name="pmpagent" # Function to check if the passed argument is a valid IP # function is_valid_ip { local ip=$1 local isValidIp=1 if [[ $ip =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]] then OIFS=$IFS IFS='.' ip=($ip) IFS=$OIFS if [[ ${ip[0]} -le 255 && ${ip[1]} -le 255 && ${ip[2]} -le 255 && ${ip[3]} -le 255 ]] then isValidIp=$? fi fi return $isValidIp } # Usage validation, ie, arguments # if [ $# -eq 4 ] then # Server Name validation # # Not validated since it can have a few special characters as well. # #if [[ $1 =~ '\w+' ]] # then server_name=$1 # else # echo "Invalid Server Name : '$1'." # echo "Enter only valid characters." # exit 2 #fi # Server IP Validation # is_valid_ip $2 if [ $? -eq 0 ] then server_ip=$2 else echo "Inavlid Server IP : '$2'." echo "Enter valid IP '(0-255).(0-255).(0-255).(0-255)'." exit 2 fi # Server Port Validation # if [[ $3 =~ [0-9]+ ]] then server_port=$3 else echo "Invalid Server Port Number : '$3'." echo "Enter valid number." exit 2 fi # Server Protocol Validation # if [[ $4 = 'http' || $4 = 'https' ]] then server_protocol=$4 else echo "Invalid Server Protocol : '$4'." echo "Enter either 'http' or 'https'." exit 2 fi else # Number of arguments not equal to 4 # echo "Usage : [sudo] ./configurePMPAgentServerCommunication.sh [server_name] [server_ip] [http_port] ['http'/'https']" exit 2 fi # Ensure the Script is Run as Root. # if [ "$EUID" -ne 0 ] then echo "Failed to configure $product_name Agent - Server Communication! - Retry with \"Root Permission\"" exit 1 fi # Checking if $product_name Agent is installed # if [ -e /usr/bin/dcservice ] then echo "$product_name Agent Installed in this machine. Configuring Server Communication." else echo "$product_name Agent Not Installed in this machine. Exiting script." exit 1 fi # Determine the agent installed directory # if [ -e $settings_file ] then agent_install_directory=$(cat $settings_file | grep "DIRECTORY_PATH" | cut -f 2 -d ":" | cut -f 2 -d "\"" | rev | cut -f 1 -d "/" --complement | rev) fi if [[ ! -e $agent_install_directory ]] then if [ -e /usr/bin/dcservice ] then agent_install_directory=$(ls -l /usr/bin/ | grep dcservice | cut -d ">" -f 2 | rev | tail -c +35 | rev | cut -d " " -f 2) fi fi if [[ ! -e $agent_install_directory ]] then agent_install_directory="/usr/local" # Default Agent Install Directory fi server_info_file="$agent_install_directory/$agent_dir_name/conf/ServerInfo.json" meta_data_file="$agent_install_directory/$agent_dir_name/data/meta-data.json" last_modified_file="$agent_install_directory/$agent_dir_name/data/LastModified.json" echo "Server File : $server_info_file" dcservice -p if [ $? -ne 0 ] then echo "Unable to stop the $product_name Agent Service. Failed to configure communication." else sleep 3 fi echo "New Server Name : $server_name" sed -i "s/\"serverflatname\":.*/\"serverflatname\": \"${server_name}\",/" "$server_info_file" sed -i "s/\"servername\":.*/\"servername\": \"\",/" "$server_info_file" echo "New Server IP : $server_ip" sed -i "s/\"serveripaddress\":.*/\"serveripaddress\": \"${server_ip}\",/" "$server_info_file" sed -i "s/\"serversecipaddress\":.*/\"serversecipaddress\": \"\",/" "$server_info_file" echo "New Server Protocol : $server_protocol" sed -i "s/\"serverprotocol\":.*/\"serverprotocol\": \"${server_protocol}\",/" "$server_info_file" if [ $server_protocol = 'http' ] then echo "New Server Port : $server_port" sed -i "s/\"serverport\":.*/\"serverport\": \"${server_port}\",/" "$server_info_file" else echo "New Server HTTPS Port : $server_port" sed -i "s/\"serversecureport\":.*/\"serversecureport\": \"${server_port}\",/" "$server_info_file" fi echo "Clearing Server Last Access" sed -i "s/\"serverlastaccessname\":.*/\"serverlastaccessname\": \"\",/" "$server_info_file" echo "Removing Old Meta Files : " echo "$meta_data_file" rm -f "$meta_data_file" echo "$last_modified_file" rm -f "$last_modified_file" echo "Restarting $product_name Agent" sleep 2 dcservice -t & if [ $? -eq 0 ] then echo "Successfuly configured $product_name Agent Server Communication. And, agent restarted" else echo "Failed to restart the Agent." fi '****************************************************************************************************** '******************************************************************************************************