8

I successfully open an ssh connection with paramiko.

As i have want to use pexpect for automatic interaction with the system on the remote end, I would like to to pass the paramiko connection to pexpect.fdpexpect.fdspawn, but the two don't fit together.

Paramiko gives me a file descriptor, but the documentation is explicit about the fact that it cannot be used for reading or writing. What I need is a bidirectional file-descriptor for reading and writing, but I have a hard time figuring out how to connect the dots.

I understand why this piece of code doesn't work, but I don't know how to create something that does work.

#!/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('')

1 Answers1

0

The answer is to use Fabric which defines itself as:

Fabric is a high level Python (2.7, 3.4+) library designed to execute shell commands remotely over SSH, yielding useful Python objects in return. It builds on top of Invoke (subprocess command execution and command-line features) and Paramiko

Timothy C. Quinn
  • 3,739
  • 1
  • 35
  • 47
  • 1
    Fabric has competitors trying to do the same thing on top of the same underlying tools (f/e, [`paramiko-expect`](https://github.com/fgimian/paramiko-expect)). I'd be hesitant to call it "the answer", especially somewhere library recommendation requests are explicitly off-topic. – Charles Duffy Apr 04 '22 at 19:14