Josh Josh - 1 month ago 7
Linux Question

Python - using env variables of remote host with / SSH

Any help on this issue would be greatly appreciated.

Basically I'm writing a python script that will ssh onto various servers and to execute scripts. The problem is that these scripts use an env variable to start. Ie the script is test.sh but we use an env variable to launch it, run test.sh.

So far the routes I have taken, such as Paramiko module execute commands but do not actually take on the env variables.

import paramiko
ssh = paramiko.SSHClient()

ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())

ssh.connect('testserver' )

stdin, stdout, stderr = ssh.exec_command(cd /home/test/;$run ./test.sh')
print stdout.readlines()
print stderr.readlines()
ssh.close()


Is there a way to use paramiko? Or another way I should take?

Thanks

@ Rob

I edited the script to test. The $run variable is coming back blank.

stdin, stdout, stderr = ssh.exec_command('whoami;echo hello; echo $run ; echo goodbye')

['testuser\n', 'hello\n', '\n', 'goodbye\n']
[]


@ Rob part 2

Logging onto the server, I am able to echo $run and it returns the correct path/script
I also checked and this is an env variable that is set in the .profile. I feel like python is not invoking the .profile .

Answer

ssh.exec_command doesn't interpret the remote .profile. Try this:

ssh.exec_command('. .profile ; cd /home/test/;$run ./test.sh')