如何将paramiko与pexpect一起使用?

2024-05-23 22:27:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我成功地打开了与paramiko的ssh连接

由于我希望使用pexpect与远程端的系统进行自动交互,因此我希望将paramiko连接传递到pexpect.fdpexpect.fdspawn,但这两个连接不匹配

Paramiko给了我一个文件描述符,但是documentation is explicit about the fact that it cannot be used for reading or writing。我需要的是一个用于读写的双向文件描述符,但我很难弄清楚如何连接这些点

我理解为什么这段代码不起作用,但我不知道如何创建起作用的东西

#!/usr/bin/env python3

import pexpect
import pexpect.fdpexpect
import paramiko

sshc = paramiko.client.SSHClient()
sshc.set_missing_host_key_policy(paramiko.AutoAddPolicy())
sshc.connect('192.0.2.1', username='redacted', password='redacted',
             look_for_keys=False, allow_agent=False)
io = pexpect.fdpexpect.fdspawn(sshc.invoke_shell().fileno())

io.sendline('')

Tags: 文件ioimportfalseparamikofor远程系统