1

I've been trying to run this command to ssh from a .py on a Windows platform:

child = winpexpect.spawn('ssh %s@%s' % ('myID','m.y.i.p'))

It should function similarly to pexpect, but I receive this error:

pexpect.ExceptionPexpect: The command was not found or was not executable: ssh.

I've confirmed that C:\rhcygwin\bin is in my Path. Any suggestions on how to instruct the .py file to locate the ssh command?

edit: I switched my approach: ran this code in my .py

    command = ['bash', '-c', './myssh.sh']
    proc = subprocess.Popen(command, stdout = subprocess.PIPE)

it's very rudimentary but it will connect successfully.

gortron
  • 147
  • 1
  • 3
  • 10

2 Answers2

1

As far as I'm aware, pexect does not actually work on windows. There was a partial port attempt, but it was broken the last time I checked it.

If you want to automate doing something over ssh with python on windows, you will probably have better luck with the paramiko library. There are good docs, but you will need to compile pycrypto, or else get a precompiled binary.

Spencer Rathbun
  • 14,510
  • 6
  • 54
  • 73
  • Thanks @SpencerRathbun, and for posterity: if you can get pycrypto/paramiko built, it's a very clean solution to this problem. I asked a similar question yesterday, and useful paramiko code was provided here: http://stackoverflow.com/questions/11147548/executing-a-bash-script-in-python. As for my part, I have had no luck building a pycrypto library on my machine, and I've tried many guides. – gortron Jun 22 '12 at 20:23
  • @gortron I ran into the same problem getting it to compile on windows, but I found a precompiled lib which worked. You'd think that compiling would be easier than it is... – Spencer Rathbun Jun 22 '12 at 20:27
1

You have to use the winspawn method, and specify the .exe extension:

child = winpexpect.winspawn('ssh.exe %s@%s' % ('myID','m.y.i.p'))
Labarbiche
  • 97
  • 1
  • 7