小编典典

使用Paramiko时环境变量的差异

python

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

这是我运行的代码:

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

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

我该如何解决?


阅读 454

收藏
2021-01-20

共1个答案

小编典典

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

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

有关更多详细信息,请参见文档

2021-01-20