Python-将远程主机的env变量与/SSH一起使用 [英] Python - using env variables of remote host with / SSH

查看:300
本文介绍了Python-将远程主机的env变量与/SSH一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在此问题上的任何帮助将不胜感激.

Any help on this issue would be greatly appreciated.

基本上,我正在编写一个python脚本,它将ssh推送到各种服务器上并执行脚本.问题在于这些脚本使用env变量来启动.即脚本是test.sh,但是我们使用env变量来启动它,运行test.sh.

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.

到目前为止,我所采用的路线(例如Paramiko模块)执行命令,但实际上并没有采用env变量.

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()

是否可以使用paramiko?还是我应该采取的另一种方式?

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

谢谢

@ Rob

我编辑了脚本进行测试. $ run变量变回空白.

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第2部分

登录到服务器,我能够回显$ run并返回正确的路径/脚本 我还检查了这是在.profile中设置的env变量.我觉得python没有调用.profile.

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不会解释远程.profile.试试这个:

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

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

这篇关于Python-将远程主机的env变量与/SSH一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆