本文介绍了使用 Paramiko 时的环境变量差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过终端(在 Mac 上)连接到 SSH 并运行 Paramiko Python 脚本,但出于某种原因,这两个会话的行为似乎不同.PATH 环境变量在这些情况下是不同的.

I am connecting to SSH via terminal (on Mac) and run a Paramiko Python script and for some reason, the two sessions seem to behave differently. The PATH environment variable is different in these cases.

这是我运行的代码:

import paramiko
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect('host', username='myuser',password='mypass')
stdin, stdout, stderr =ssh.exec_command('echo $PATH')
print (stdout.readlines())

知道为什么环境变量不同吗?

Any idea why the environment variables are different?

我该如何解决?

推荐答案

使用 Channel 对象而不是 SSHClient 对象解决了我的问题.

Working with the Channel object instead of the SSHClient object solved my problem.

chan=ssh.invoke_shell()
chan.send('echo $PATH
')
print (chan.recv(1024))

有关详细信息,请参阅文档

这篇关于使用 Paramiko 时的环境变量差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 13:24