7

I have built sshpass on Cygwin. It works perfectly from within a Cygwin terminal window:

sshpass -p password ssh -o StrictHostKeyChecking=no user@host

However, the exact same command from a Windows command prompt produces no output.

I tried a non-interactive command to see if that worked but it didn't:

sshpass -p password ssh -o StrictHostKeyChecking=no user@host ls

Both work fine from the Cygwin environment but not from a Windows command prompt. They do work from the Windows command prompt but there is no output. I know this because I can issue commands from a cmd.exe window and see the effects on the remote host. Also, plain ssh (the Cygwin one) works fine from the Windows command prompt.

How can I get output from sshpass when used in a Windows command prompt ?

starfry
  • 1,647
  • I've found that I can change my directory to the location of the built sshpass.exe. It was somewhere like: C:\cygwinInstall\usr\local\bin\sshpass.exeAlthough I haven't had luck beyond getting the sshpass.exe to print out the help. Beyond that, everything else I run seems to error out.

    Have you tried this?

    – Andrew Apr 01 '17 at 04:51
  • Ensure you have both c:\cygwin\usr\bin and c:\cygwin\usr\local\bin in your %PATH%. Mine works, but exhibits the behaviour described in this question. I use StrictHostKeyChecking to avoid the prompt offering to accept an unknown key, which you won't see if using sshpass - it'll just fail silently. I suggest you build a plain-old ssh command-line and ensure that works cleanly before prepending that command-line with sshpass -p password. I've checked mine from both within a Cygwin terminal (which works fine) and from a Windows command prompt (which exhibits the problem I described). – starfry Apr 01 '17 at 14:17

2 Answers2

5

The reason that this does not work is due to an incompatibility in stdin/stdout redirection between cygwin and native Win32 programs but there is a wrapper program called cygnative which solves the problem. It allows the desired command-line to be used like this:

C:> cygnative sshpass -p password ssh -o StrictHostKeyChecking=no user@host ls

(this works for non-interactive commands but not for interactive terminal sessions)

The original author's links to cygnative.exe are dead, but it is available as C source in this gist or as source and executable in this zip.


I discovered this solution when trying to solve another problem, this time trying to use rsync over PuTTY's plink program. I was getting an error:

Unable to read from standard input: The parameter is incorrect.

which led me to discover cygnative, but tracking it down was difficult due to the original links being dead.

The problem is described here and presents the solution, cygnative, here, with an updated version 1.2 here.

starfry
  • 1,647
1

Thanks to starfry for an explanation of the problem.

I didn't want to use cygnative. Most of the links on internet are dead. The program is not maintained, and you'd have to compile the code yourself.

Your command under Cygwin works because Cygwin is using mintty as a terminal which handles the IO redirection properly.

The workaround I used is to call mintty in non-interactive mode. On windows command prompt run something like:

C:\Development\temp> mintty.exe -w hide bash -c "sshpass -p a ssh kshar@localhost ls 2>&1 > /tmp/mintty.log" && sh -c 'cat /tmp/mintty.log; rm /tmp/mintty.log'

How it works:

  1. -w hide tells mintty to not show any ui window
  2. Mintty runs sh
  3. sh runs sshpass and ssh
  4. mintty does not return the stdout to windows cmd. So the output is redirected to a temp file
  5. temp file is then printed using sh run outside mintty

Awkward, but then so would be hunting down cygnative source and compiling it.