Josh Josh - 5 months ago 23
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 but we use an env variable to launch it, run

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.connect('testserver' )

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

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


@ 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 .


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

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