【发布时间】:2016-10-14 17:52:11
【问题描述】:
对于这个问题的任何帮助将不胜感激。
基本上,我正在编写一个 python 脚本,它将 ssh 到各种服务器并执行脚本。问题是这些脚本使用环境变量来启动。即脚本是 test.sh 但我们使用环境变量来启动它,运行 test.sh。
到目前为止,我采取的路线,例如 Paramiko 模块执行命令,但实际上并没有采用 env 变量。
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?还是我应该采取的其他方式?
谢谢
@罗伯
我编辑了脚本进行测试。 $run 变量返回空白。
stdin, stdout, stderr = ssh.exec_command('whoami;echo hello; echo $run ; echo goodbye')
['testuser\n', 'hello\n', '\n', 'goodbye\n']
[]
@罗布第 2 部分
登录到服务器,我可以回显 $run 并返回正确的路径/脚本 我还检查了,这是在 .profile 中设置的环境变量。我觉得 python 没有调用 .profile 。
【问题讨论】:
-
假设你修复了语法错误,当你运行上面的程序时会发生什么?
-
“脚本是 test.sh,但我们使用环境变量来启动它,运行 test.sh” 我不明白这是什么意思。如何使用环境变量来启动脚本?
-
假设远程 shell 的
.profile或类似的启动设置了$run环境变量,那应该可以正常工作。您可以通过ssh.exec_command('echo hello ; echo $run ; echo goodbye')进行测试。 -
@JimGarrison - 线索在代码中。他的意思是
$run test.sh。 -
那你还没有在远程机器上设置
run环境变量。你想怎么设置run?