Python - using env variables of remote host with / SSH

我只是一个虾纸丫 提交于 2019-12-10 03:54:25

问题


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 .


回答1:


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

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



回答2:


Plumbum to the rescue.

Among other things, it provides a nifty interface, leveraging paramiko (see ParamikoMachine), and also lets you manipulate the remote environment.



来源:https://stackoverflow.com/questions/19938993/python-using-env-variables-of-remote-host-with-ssh

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!