msys2 / MSYS2-packages

Package scripts for MSYS2.
https://packages.msys2.org
BSD 3-Clause "New" or "Revised" License
1.28k stars 486 forks source link

sshpass does not work with Windows Terminal app and msys2 console #3579

Open brendan-simon-indt opened 1 year ago

brendan-simon-indt commented 1 year ago

Description / Steps to reproduce the issue

sshpass works with ssh if I run from a stock standard msys2 terminal (or mingw64, clang, ucrt, ...).

sshpass works with Windows Terminal app, if using sshpass with windows ssh, but not with msys2 ssh.

sshpass does not work with Windows Terminal app with a msys2, mingw64, ... terminal in a tab. I get absolutely no output. I don't know if this is an issue with sshpass or Windows Terminal app.

NOTE: the ssh command after the sspass does work if given by itself (I have ssh rsa keys setup)

Expected behavior

Output to be displayed in terminal.

Actual behavior

absolutely no output

Verification

Windows Version

MSYS_NT-10.0-19044

Are you willing to submit a PR?

unlikely

sskras commented 1 year ago

Where did you get / how have you built the sshpass binary?

brendan-simon-indt commented 1 year ago

Where did you get / how have you built the sshpass binary?

It's an msys2 package.

$ pacman -Ss sshpass
msys/sshpass 1.10-1 (net-utils) [installed]
    Fool ssh into accepting an interactive password non-interactively
pinyht commented 1 year ago

same problem

Biswa96 commented 1 year ago

sshpass works with ssh if I run from a stock standard msys2 terminal

If sshpass works with msys2's default terminal program (mintty), why is this not an issue of Windows Terminal?

sskras commented 1 year ago

@brendan-simon-indt commented 3 weeks ago:

  • Open up an msys terminal from the drop down menu.
  • Issue sspass command. e.g. sshpass -puserpassword user@host ls

@brendan-simon-indt, @pinyht:

What do you get if you run where ssh.exe after the last step (in the same environment)? Maybe your combo picks up the wrong ssh binary (the Windows-native one).

brendan-simon-indt commented 1 year ago

If I run from an msys2 terminal/shell (Windows launcher icon from start menu), it works with direct ssh and manual password entry and with sshpass. Here is the output.

$ ssh brendansimon@192.168.56.101 "uname -a"
brendansimon@192.168.56.101's password:
Linux osboxes 4.19.0-23-amd64 #1 SMP Debian 4.19.269-1 (2022-12-20) x86_64 GNU/Linux

# with wrong password
$ sshpass -p "BAD" ssh brendansimon@192.168.56.101 "uname -a"
Permission denied, please try again.

# with correct password
$ sshpass -p "GOOD" ssh brendansimon@192.168.56.101 "uname -a"
Linux osboxes 4.19.0-23-amd64 #1 SMP Debian 4.19.269-1 (2022-12-20) x86_64 GNU/Linux

$ where ssh
C:\msys64\usr\bin\ssh.exe

Doing the same things from Windows Terminal app with msys2 shell, works using ssh and manual password, but I get no output if running via sshpass.

$ ssh brendansimon@192.168.56.101 "uname -a"
brendansimon@192.168.56.101's password:
Linux osboxes 4.19.0-23-amd64 #1 SMP Debian 4.19.269-1 (2022-12-20) x86_64 GNU/Linux

# wrong password - there is a noticeable delay before prompt returns (no output)
$ sshpass -p "XXX" ssh brendansimon@192.168.56.101 "uname -a"

# correct password - prompt returns quickly (no output)
$ sshpass -p "GOOD" ssh brendansimon@192.168.56.101 "uname -a"

$ where ssh
C:\msys64\usr\bin\ssh.exe

It could be a Windows Terminal app issue, but why does ssh work ok, but not with sshpass?

sskras commented 1 year ago

I would try running sshpass under strace in both setups. Comparing the outputs might give some insight. Beware that they would be over 1000-2000 lines each.

Toxsch commented 1 year ago

Encountered the same problem, add a little verbose information:

$ sshpass -v -e ssh user@host
SSHPASS: searching for password prompt using match "assword"
user@host's password:
SSHPASS: detected prompt. Sending password.
SSHPASS: read:
pinyht commented 1 year ago

I found sshpass only work with mintty and conemu, and if use conemu, must start with conemu-msys2-64.exe, I can use conemu-msys2-64.exe with bash.exe in windows terminal, sshpass will work, but it can't enable xterm-256color,maybe have some bugs, if direct use msys2_shell.cmd or bash.exe, sshpass can't work, not just windows terminal, i have already tried windows terminal, conemu, cmder, tabby, hyper.js, all can't work

sskras commented 1 year ago

@brendan-simon-indt commented on Feb 18:

I would like to reproduce this. Just installed Windows Terminal version 1.16.10261.0: image

But MSYS2 terminal isn't here: image

Did you configure it by hand or was it added there automatically on your machine?

mmuetzel commented 1 year ago

@sskras: Instructions for setting up the Windows Terminal for the MSYS2 environments are here: https://www.msys2.org/docs/terminals/

brendan-simon-indt commented 1 year ago

@sskras I can't remember now. This is what is in my settings. I guess I may have set it up myself.

image

image

image

sskras commented 1 year ago

Thanks, it comes probably from here: https://www.msys2.org/docs/terminals/#windows-terminal

// This makes UCRT64 the default shell
"defaultProfile": "{17da3cac-b318-431e-8a3e-7fcdefe6d114}",
"profiles": {
  "list":
  [
    // ...
    {
      "guid": "{17da3cac-b318-431e-8a3e-7fcdefe6d114}",
      "name": "UCRT64 / MSYS2",
      "commandline": "C:/msys64/msys2_shell.cmd -defterm -here -no-start -ucrt64",
      "startingDirectory": "C:/msys64/home/%USERNAME%",
      "icon": "C:/msys64/ucrt64.ico",
      "font": 
      {
        "face": "Lucida Console",
        "size": 9
      }
    },
    {
      "guid": "{71160544-14d8-4194-af25-d05feeac7233}",
      "name": "MSYS / MSYS2",
      "commandline": "C:/msys64/msys2_shell.cmd -defterm -here -no-start -msys",
      "startingDirectory": "C:/msys64/home/%USERNAME%",
      "icon": "C:/msys64/msys2.ico",
      "font": 
      {
        "face": "Lucida Console",
        "size": 9
      }
    },
    // ...
  ]
}
sskras commented 1 year ago

I can confirm that the issue is present in MSYS2. The tested WT profile:

            {
                "commandline": "C:/msys64/msys2_shell.cmd -defterm -here -no-start -msys",
                "font": 
                {
                    "face": "Lucida Console",
                    "size": 9.0
                },
                "guid": "{71160544-14d8-4194-af25-d05feeac7233}",
                "icon": "C:/msys64/msys2.ico",
                "name": "MSYS / MSYS2",
                "startingDirectory": "C:/msys64/home/%USERNAME%"
            },

This can also be reproduced without Windows Terminal by simply running MSYS2 from Start > Run as follows:

Without the -defterm the script opens the standalone MinTTY window where the I/O works fine.

This also affects Cygwin. The tested WT profile:

            {
                "commandline": "C:/cygwin64/bin/bash.exe --login -i",
                "guid": "{cdaa0fd5-e104-4561-9b7c-23177717bc48}",
                "hidden": false,
                "icon": "C:/cygwin64/Cygwin-Terminal.ico",
                "name": "Cygwin",
                "startingDirectory": "C:/cygwin64/home/%USERNAME%"
            }

Tested command lines:

sskras commented 1 year ago

I tested printing version of the SSH client while running under sshpass:

SSHPASS=. sshpass -e ssh -V

If I run this from MinTTY, version is shown:

saukrs@DESKTOP-O7JE7JE MSYS ~
$ SSHPASS=. sshpass -e ssh -V
OpenSSH_9.3p1, OpenSSL 3.1.0 14 Mar 2023

saukrs@DESKTOP-O7JE7JE MSYS ~
$

Now if run this from console I/O based environment (cmd.exe, Windows Terminal), the stdout is lost:

saukrs@DESKTOP-O7JE7JE MSYS ~
$ SSHPASS=. sshpass -e ssh -V

saukrs@DESKTOP-O7JE7JE MSYS ~
$

Not sure if it counts as a reduced test case.

But clearly all this is related to the initial terminal I/O setup.

sskras commented 1 year ago

I tried stracing the sshpass in both cases, unifying (normalizing) some identifactors/addresses in the content, and then compared both. I noticed that:

I lack the exact understanding of inner Cygwin/MSYS2 mechanisms. It still looks to me like its' PTY/PTMX emulation logic is to blame given that sshpass works on a variety of unices:

We need more qualified expertise to narrow the bug down.

After that it might get clearer if the issue really belongs to this repo (instead of msys2/msys2-runtime) or IOW, what's the upstream of the issue: SSHPass or Cygwin itself.

sskras commented 1 year ago

Also ping @Shachar, the developer of sshpass.

jeremyd2019 commented 1 year ago

We need more qualified expertise to narrow the bug down.

For Cygwin tty/console, you can't get more qualified expertise than @tyan0. It would probably be interesting to know if this happens as described under upstream Cygwin (I expect it does), and then whether it still happens under a Cygwin 3.5 snapshot.

Shachar commented 1 year ago

There's a chance the problem isn't with MSYS or sshpass (exactly), but with the version of ssh you're using. See https://sourceforge.net/p/sshpass/bugs/27/ on the upstream sshpass package.

In a nutshell, some ssh versions close their stdout and stderr, and redirect it to their own terminal. Since sshpass doesn't copy between the TTY and stdout/err, the output never goes anywhere.

sskras commented 1 year ago

@jeremyd2019 commented 3 hours ago:

For Cygwin tty/console, you can't get more qualified expertise than @tyan0.

Thanks for inviting a skilled person! :)

It would probably be interesting to know if this happens as described under upstream Cygwin (I expect it does),

I commented 2 days ago that Cygwin fails in a similar manner. I think it's quite up to date:

$ uname -a
CYGWIN_NT-10.0-19044 DESKTOP-O7JE7JE 3.4.6-1.x86_64 2023-02-14 13:23 UTC x86_64 Cygwin

and then whether it still happens under a Cygwin 3.5 snapshot.

I would give it a shot on my machine should I know how to bootstrap this: http://mirror.internode.on.net/pub/cygwin/x86_64/release/cygwin/cygwin-3.5.0-0.298.g41fdb869f998.tar.xz

tyan0 commented 1 year ago

I have confirmed that this issue also occurs in cygwin 3.4.6, but not in the latest cygwin 3.5.0 (Test).

This issue seems to be due to a long-standing limitation of console implementation that the console is inaccessible from other terminals that are associated with other tty. Only console (command prompt, windows terminal etc.) has this limitation but pty (mintty, xterm etc.) does not.

A several months ago, I applied a relatively agressive patch (https://github.com/mirror/newlib-cygwin/commit/3721a756b0d8067cf070e2aedcf3ea5e32e47270) to the cygwin master branch so that GNU screen and tmux get working in console. It seems that this patch solves this issue as well.

Therefore, you can expect that sshpass will work also on console with upcoming v3.5.x.

Cygwin v3.5.x will be released within 2023.

brendan-simon-indt commented 1 year ago

Awesome. Can't come soon enough. Thanks for the amazing work 🙇

sskras commented 1 year ago

ping @lazka: should this be moved into msys2/msys2-runtime repo?

Also I would change the title:

-sshpass does not work with Windows Terminal app and msys2 console
+combination of `sshpass` and `ssh` from MSYS2 renders no output in Windows console
Biswa96 commented 1 year ago

should this be moved into msys2/msys2-runtime repo?

It's ok here, I guess. The issue with sshpass which belongs to this MSYS2-packages repository.

sskras commented 1 year ago

@Biswa96: no, the issue is with msys2-3.4.6/winsup/cygwin:

This issue seems to be due to a long-standing limitation of console implementation that the console is inaccessible from other terminals that are associated with other tty. Only console (command prompt, windows terminal etc.) has this limitation but pty (mintty, xterm etc.) does not.

A several months ago, I applied a relatively agressive patch (mirror/newlib-cygwin@3721a75) to the cygwin master branch so that GNU screen and tmux get working in console. It seems that this patch solves this issue as well.

Specifically with the winsup/cygwin/fhandler/console.cc file, eg. the fhandler_console::set_unit () method.

Because of this applications like tmux also fail: image

I expect GNU Screen to fail too (but MSYS2 doesn't build it).

RekaDowney commented 7 months ago

@tyan0 @sskras Hey guys, so now we just need to wait for the release of Cygwin 3.5, right? Could you please let me know where I can find the release roadmap for Cygwin? I tried searching for cygwin roadmap, cygwin release notes or cygwin 3.5.0, but I couldn't find any specific information about the release dates of 3.5.

MehdiChinoune commented 7 months ago

but I couldn't find any specific information about the release dates of 3.5.

https://cygwin.com/pipermail/cygwin-announce/2024-January/011483.html

RekaDowney commented 7 months ago

but I couldn't find any specific information about the release dates of 3.5.

https://cygwin.com/pipermail/cygwin-announce/2024-January/011483.html

Okay, I'll patiently wait for a while.