0

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

loganathan
  • 240
  • 2
  • 13

2 Answers2

1

My original answer

ssh VD_PASSWORD="$VD_PASSWORD" ...

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

ssh ... user@remote VD_PASSWORD="$VD_PASSWORD" <<'EOSSH' ...

It does not work because of the heredoc.

I got this to work

VD_PASSWORD="$VD_PASSWORD"
ssh -o StrictHostKeyChecking=no -T user@remote <<EOSSH
echo $VD_PASSWORD
EOSSH

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

ssh ... VD_PASSWORD="$VD_PASSWORD" /path/to/remote/script
user9517
  • 115,471
  • 20
  • 215
  • 297
  • sshpass -p "$APP_PASSWORD" ssh -o StrictHostKeyChecking=no -T root@$APP_FQDN VD_PASSWORD="$VD_PASSWORD"<< 'EOSSH' This is not working, do I need to follow any specific format? – loganathan Jun 30 '16 at 08:47
0

To pass environment variables it may be needed to change the entries for AcceptEnv in sshd_config(5) and SendEnv in ssh_config(5).

eKKiM
  • 1,540
  • 9
  • 23