Jtgrenz
Jtgrenz

Reputation: 601

winpexpect not behaving like pexpect

I've been having this problem for days now and I can't figure out whats going wrong.

I have a simple python script using pexpect running on OSX which lauches ssh-keygen and automates hitting return to generate a key pair and it works fine. I want to do the same thing on windows. To get ssh and ssh-keygen on windows, I installed OpenSSH for Windows which lets ssh run from the cmd prompt as a normal windows program using a portion of the CYGWIN support files. I ported the same script to windows and changed pexpect to winpexpect and used the coresponding winspawn to start the subprocess but it stalls out at the first prompt.

def generateKeys(self):
    print "GeneratingKeys..."
    gen = winpexpect.winspawn("ssh-keygen -t rsa")
    print gen.expect(":") #This is where it asks for a place to save the keys. retuns 0 if found
    print "before: " + gen.before
    print "after: " + gen.after
    print gen.sendline()
    #Next it should expect a prompt for a passphrase but it never reaches it
    print gen.expect(":")
    print "before: " + gen.before
    print "after: " + gen.after
    print gen.sendline()

the output I get is this:

GeneratingKeys...
0
before: Enter file in which to save the key (/home/user/.ssh/id_rsa)
after: :
2
before: Enter file in which to save the key (/home/user/.ssh/id_rsa)
after: :

and it's here that it sits for all eternity collecing digital dust bunnies.

I dont understand why its getting ":" after the expected colon either.

Is there perhaps another module I could use that would allow me to communicate better with OpenSSH for Windows?

Upvotes: 1

Views: 3550

Answers (2)

Jruv
Jruv

Reputation: 1598

Here is a fork project winpexpect: This version fixed many bugs. You can have a try.

Upvotes: 0

Spencer Rathbun
Spencer Rathbun

Reputation: 14910

Quite some time ago, I looked at winpexpect. I couldn't get it to run either, it just hung. Something to do with unsupported sections, and an incomplete port if I remember correctly.

Since I needed it for ssh, I ended up replacing it with paramiko. It requires pycrypto, which is a pain to set up on windows, unless you use a precompiled installer. For the key generation, see this section of the docs. It has loading and saving of keys. Also pay attention to the missing host key function.

To get up and running quickly, here is an overview. Please note it does not go over all of the function calls, such as why he is using AutoAddPolicy(). Check the docs for the pros/cons and more details.

Upvotes: 1

Related Questions