What is the proper way to pass variable from one machine to another machine via bash script? In the below code I am trying to get the input from user and want to pass the same to remote machine, but the value is not getting accessed in remote machine, can anyone let me know the proper way to do it?
NEWLINE=$'\n'
read -s -p "Enter App FQDN:${NEWLINE}" APP_FQDN
read -s -p "Enter App Password:${NEWLINE}" APP_PASSWORD
read -s -p "Enter VD Password:${NEWLINE}" VD_PASSWORD
sshpass -p "$APP_PASSWORD" ssh -o StrictHostKeyChecking=no -T root@$APP_FQDN << 'EOSSH'
if true; then
su - presto << 'PRESTO'
source ~/.bash_profile
cd /opt/vm/vchs/presto_agent
pwd
KEY=$( cat /opt/vm/vchs/presto_agent/config/settings.yml |grep '^key:'|sed 's/^key: //')
echo $KEY
echo $VD_PASSWORD
PASSWORD=$(ruby /opt/vm/vchs/presto_agent/lib/obfuscation_util.rb encrypt $KEY $VD_PASSWORD)
echo $PASSWORD
PRESTO
else
echo "False"
fi
EOSSH
While executing the above script $VD_PASSWORD
is echo blanks
My original answer
was wrong because it did not take into account the heredoc and it placed the setting of the environment variable in the wrong place. Even if you put the environment variable in the correct place
It does not work because of the heredoc.
I got this to work
Note the lack of
'
around the EOSSH but that may not do what you want ( I really can't tell from the script).It may be better to have your script on the remote host and then call it
To pass environment variables it may be needed to change the entries for
AcceptEnv
in sshd_config(5) andSendEnv
in ssh_config(5).