Sijo M Cyril
Sijo M Cyril

Reputation: 780

How can I get environment variables from remote Ubuntu server while executing a script on the remote server from local ubuntu system using sshpass?

How can I get environment variables from remote Ubuntu server while executing a script on the remote server from local Ubuntu system? I have configured lots of environment variables for vagrant user in different files.

I use the following command to execute the script from local Ubuntu system

sshpass -f .passwd ssh -o StrictHostKeyChecking=no -t [email protected] "sh script.sh"

The .passwd file contains password

While executing this script, configured environment variables not available for script execution. I have sourced profile, .bash_profile , /etc/profile etc. with this command e.g.

sshpass -f .passwd ssh -o StrictHostKeyChecking=no -t [email protected] "source .bash_profile; sh script.sh"

Please help me to fix this issue.

Note: Environment variables are working fine on a direct ssh session and script too.

Upvotes: 1

Views: 1327

Answers (2)

Sijo M Cyril
Sijo M Cyril

Reputation: 780

-i option with bash and sh solved the issue. Now I am able to get interactive ssh session in a noninteractive ssh connection.

@marcolz @tripleee Thanks for your help

sshpass -f .passwd ssh -o StrictHostKeyChecking=no -t [email protected] "bash -i  script.bash"

Upvotes: 1

marcolz
marcolz

Reputation: 2970

If you pass -l to sh it will behave as if it were a login shell and will source your shell resource files, so try:

sshpass -f .passwd ssh -o StrictHostKeyChecking=no -t [email protected] "sh -l script.sh"

Upvotes: 0

Related Questions