I'm trying to spawn an ssh child process using subprocess.
I'm working on Python 2.7.6 on Windows 7
here is my code:
from subprocess import *
r=Popen("ssh sshserver@localhost", stdout=PIPE)
stdout, stderr=r.communicate()
print(stdout)
print(stderr)
The outputs:
None
stdout should contain: sshserver@localhost's password:
Here's an example of working SSH code that handles the promt for yes/no on the certificate part and also when asked for a password.
The reason (and correct me if i'm wrong here) for you not being able to read the
stdin
straight away is because SSH runs as a subprocess under a different process ID which you need to read/attach to.Since you're using windows,
pty
will not work. there's two solutions that would work better and that's pexpect and as someone pointed out key-based authentication.In order to achieve a key-based authentication you only need to do the following: On your client, run:
ssh-keygen
Copy yourid_rsa.pub
content (one line) into/home/user/.ssh/authorized_keys
on the server.And you're done. If not, go with pexpect.