PowerShell / Win32-OpenSSH

Win32 port of OpenSSH
7.43k stars 760 forks source link

GetConsoleMode error #856

Closed havalli closed 5 years ago

havalli commented 7 years ago

Please answer the following

"OpenSSH for Windows" version 0.0.18.0

Server OperatingSystem 0.0.18.0

Server 2016 Core

What is failing ssh user@server remoteComand

Expected output no Error

Actual output GetConsoleMode on STD_INPUT_HANDLE failed with 6

The command is executed in a CI/CD Pipeline by a gitlab-runner. If I execute the command manually inside a cmd window there is no error. The error don't occure with previous version 0.0.17.0.

bagajjal commented 7 years ago

Have you used -n option (https://linux.die.net/man/1/ssh)?

image

bagajjal commented 7 years ago

@havalli - Any update on this? can this be closed?

bagajjal commented 7 years ago

Please reopen if required.

havalli commented 7 years ago

@bagajjal - I teste with -n option. Still same error. How do i reopen this issue?

nellfr commented 7 years ago

Hello, I am experiencing same issue when using -n option with OpenSSH Client version v0.0.21 and OpenSSH Server version v0.0.20 (both on Windows 2012 servers). The error is raised after ssh client times out (5 minutes in my tests).

bagajjal commented 7 years ago

It would be great if you can provide the detailed reproduction steps. Please share the ssh client (ssh -vvv user@ip) and sshd (DEBUG3) logs.

nellfr commented 7 years ago

The ssh command is initiated from CHEF client, I will try to grab the client output with -vvv. Meanwhile I was able to collect the DEBUG3 logs on server side.

nellfr commented 7 years ago

11476 15:38:13:105 debug3: socket:356, io:0000001F2FA85CE0, fd:5 11476 15:38:13:105 debug3: fd 5 is not O_NONBLOCK 11476 15:38:13:105 debug3: pipe - r-h:336,io:0000001F2FA85D90,fd:6 w-h:396,io:0000001F2FA78470,fd:7 11476 15:38:13:105 debug3: spawning "C:\Program Files\OpenSSH\sshd.exe" 11476 15:38:13:105 debug3: Register child 000000000000013C pid 9132, 0 zombies of 0 11476 15:38:13:105 debug3: close - io:0000001F2FA85CE0, type:1, fd:5, table_index:5 11476 15:38:13:105 debug1: Forked child 9132. 11476 15:38:13:105 debug3: close - io:0000001F2FA78470, type:2, fd:7, table_index:7 9132 15:38:13:120 debug1: sshd version OpenSSH_7.5, LibreSSL 2.5.3 9132 15:38:13:120 debug3: socket:0, socktype:1, io:000000347A65FC20, fd:3 9132 15:38:13:120 debug3: close - io:000000347A65FC20, type:2, fd:3, table_index:3 9132 15:38:13:120 debug3: open - handle:00000000000000F0, io:000000347A660750, fd:3 9132 15:38:13:120 debug3: ReadFileEx() ERROR:38, io:000000347A660750 9132 15:38:13:120 debug3: read - no more data, io:000000347A660750 9132 15:38:13:120 debug3: ReadFileEx() ERROR:38, io:000000347A660750 9132 15:38:13:120 debug3: read - no more data, io:000000347A660750 9132 15:38:13:120 debug3: close - io:000000347A660750, type:2, fd:3, table_index:3 9132 15:38:13:120 debug1: private host key #0: ssh-rsa SHA256:l/79rJLA22Ufg3r3REYmNcnoPG3t/oFsATfvSDYNDpQ 9132 15:38:13:120 debug3: open - handle:00000000000000F0, io:000000347A660750, fd:3 9132 15:38:13:120 debug3: ReadFileEx() ERROR:38, io:000000347A660750 9132 15:38:13:120 debug3: read - no more data, io:000000347A660750 9132 15:38:13:120 debug3: ReadFileEx() ERROR:38, io:000000347A660750 9132 15:38:13:120 debug3: read - no more data, io:000000347A660750 9132 15:38:13:120 debug3: close - io:000000347A660750, type:2, fd:3, table_index:3 9132 15:38:13:120 debug1: private host key #1: ssh-dss SHA256:hgueF2cofwkLFpfxgfqhfPLkDvYV600H26rzUflBpcI 9132 15:38:13:120 debug3: open - handle:00000000000000F0, io:000000347A660750, fd:3 9132 15:38:13:120 debug3: ReadFileEx() ERROR:38, io:000000347A660750 9132 15:38:13:120 debug3: read - no more data, io:000000347A660750 9132 15:38:13:120 debug3: ReadFileEx() ERROR:38, io:000000347A660750 9132 15:38:13:120 debug3: read - no more data, io:000000347A660750 9132 15:38:13:120 debug3: close - io:000000347A660750, type:2, fd:3, table_index:3 9132 15:38:13:120 debug1: private host key #2: ecdsa-sha2-nistp256 SHA256:ihIIAATwL+ZaE9iRPDr0NvQguyPuXnqBfKAyAUQ3wmc 9132 15:38:13:120 debug3: open - handle:00000000000000F0, io:000000347A660750, fd:3 9132 15:38:13:120 debug3: ReadFileEx() ERROR:38, io:000000347A660750 9132 15:38:13:120 debug3: read - no more data, io:000000347A660750 9132 15:38:13:120 debug3: ReadFileEx() ERROR:38, io:000000347A660750 9132 15:38:13:120 debug3: read - no more data, io:000000347A660750 9132 15:38:13:120 debug3: close - io:000000347A660750, type:2, fd:3, table_index:3 9132 15:38:13:120 debug1: private host key #3: ssh-ed25519 SHA256:OJRDcYTRtNk98QuhZPP37yZT9lBVtHXXUQNNmPTeVmY 9132 15:38:13:136 debug1: child socket: 356 9132 15:38:13:136 debug1: child startup_pipe: 396 9132 15:38:13:136 Connection from 10.0.168.48 port 51524 on 10.0.10.37 port 22 9132 15:38:13:136 debug1: Client protocol version 2.0; client software version OpenSSH_7.5 9132 15:38:13:136 debug1: match: OpenSSH_7.5 pat OpenSSH* compat 0x04000000 9132 15:38:13:136 debug1: Local version string SSH-2.0-OpenSSH_7.5 9132 15:38:13:136 debug2: fd 3 setting O_NONBLOCK 9132 15:38:13:136 debug3: socket:0, socktype:1, io:000000347A6622A0, fd:5 9132 15:38:13:136 debug3: list_hostkey_types: ssh-dss key not permitted by HostkeyAlgorithms 9132 15:38:13:136 debug1: list_hostkey_types: ssh-rsa,rsa-sha2-512,rsa-sha2-256,ecdsa-sha2-nistp256,ssh-ed25519 9132 15:38:13:136 debug3: send packet: type 20 9132 15:38:13:136 debug1: SSH2_MSG_KEXINIT sent 9132 15:38:13:136 debug3: receive packet: type 20 9132 15:38:13:136 debug1: SSH2_MSG_KEXINIT received 9132 15:38:13:136 debug2: local server KEXINIT proposal 9132 15:38:13:136 debug2: KEX algorithms: curve25519-sha256,curve25519-sha256@libssh.org,ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521,diffie-hellman-group-exchange-sha256,diffie-hellman-group16-sha512,diffie-hellman-group18-sha512,diffie-hellman-group14-sha256,diffie-hellman-group14-sha1 9132 15:38:13:136 debug2: host key algorithms: ssh-rsa,rsa-sha2-512,rsa-sha2-256,ecdsa-sha2-nistp256,ssh-ed25519 9132 15:38:13:136 debug2: ciphers ctos: chacha20-poly1305@openssh.com,aes128-ctr,aes192-ctr,aes256-ctr 9132 15:38:13:136 debug2: ciphers stoc: chacha20-poly1305@openssh.com,aes128-ctr,aes192-ctr,aes256-ctr 9132 15:38:13:136 debug2: MACs ctos: umac-64-etm@openssh.com,umac-128-etm@openssh.com,hmac-sha2-256-etm@openssh.com,hmac-sha2-512-etm@openssh.com,hmac-sha1-etm@openssh.com,umac-64@openssh.com,umac-128@openssh.com,hmac-sha2-256,hmac-sha2-512,hmac-sha1 9132 15:38:13:136 debug2: MACs stoc: umac-64-etm@openssh.com,umac-128-etm@openssh.com,hmac-sha2-256-etm@openssh.com,hmac-sha2-512-etm@openssh.com,hmac-sha1-etm@openssh.com,umac-64@openssh.com,umac-128@openssh.com,hmac-sha2-256,hmac-sha2-512,hmac-sha1 9132 15:38:13:136 debug2: compression ctos: none 9132 15:38:13:136 debug2: compression stoc: none 9132 15:38:13:136 debug2: languages ctos: 9132 15:38:13:136 debug2: languages stoc: 9132 15:38:13:136 debug2: first_kex_follows 0 9132 15:38:13:136 debug2: reserved 0 9132 15:38:13:136 debug2: peer client KEXINIT proposal 9132 15:38:13:136 debug2: KEX algorithms: curve25519-sha256,curve25519-sha256@libssh.org,ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521,diffie-hellman-group-exchange-sha256,diffie-hellman-group16-sha512,diffie-hellman-group18-sha512,diffie-hellman-group-exchange-sha1,diffie-hellman-group14-sha256,diffie-hellman-group14-sha1,ext-info-c 9132 15:38:13:136 debug2: host key algorithms: ecdsa-sha2-nistp256-cert-v01@openssh.com,ecdsa-sha2-nistp384-cert-v01@openssh.com,ecdsa-sha2-nistp521-cert-v01@openssh.com,ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521,ssh-ed25519-cert-v01@openssh.com,ssh-rsa-cert-v01@openssh.com,ssh-ed25519,rsa-sha2-512,rsa-sha2-256,ssh-rsa 9132 15:38:13:136 debug2: ciphers ctos: chacha20-poly1305@openssh.com,aes128-ctr,aes192-ctr,aes256-ctr 9132 15:38:13:136 debug2: ciphers stoc: chacha20-poly1305@openssh.com,aes128-ctr,aes192-ctr,aes256-ctr 9132 15:38:13:136 debug2: MACs ctos: umac-64-etm@openssh.com,umac-128-etm@openssh.com,hmac-sha2-256-etm@openssh.com,hmac-sha2-512-etm@openssh.com,hmac-sha1-etm@openssh.com,umac-64@openssh.com,umac-128@openssh.com,hmac-sha2-256,hmac-sha2-512,hmac-sha1 9132 15:38:13:136 debug2: MACs stoc: umac-64-etm@openssh.com,umac-128-etm@openssh.com,hmac-sha2-256-etm@openssh.com,hmac-sha2-512-etm@openssh.com,hmac-sha1-etm@openssh.com,umac-64@openssh.com,umac-128@openssh.com,hmac-sha2-256,hmac-sha2-512,hmac-sha1 9132 15:38:13:136 debug2: compression ctos: none 9132 15:38:13:136 debug2: compression stoc: none 9132 15:38:13:136 debug2: languages ctos: 9132 15:38:13:136 debug2: languages stoc: 9132 15:38:13:136 debug2: first_kex_follows 0 9132 15:38:13:136 debug2: reserved 0 9132 15:38:13:136 debug1: kex: algorithm: curve25519-sha256 9132 15:38:13:136 debug1: kex: host key algorithm: ecdsa-sha2-nistp256 9132 15:38:13:136 debug1: kex: client->server cipher: chacha20-poly1305@openssh.com MAC: compression: none 9132 15:38:13:136 debug1: kex: server->client cipher: chacha20-poly1305@openssh.com MAC: compression: none 9132 15:38:13:136 debug1: expecting SSH2_MSG_KEX_ECDH_INIT 9132 15:38:13:136 debug3: receive packet: type 30 9132 15:38:13:151 debug3: send packet: type 31 9132 15:38:13:151 debug3: send packet: type 21 9132 15:38:13:151 debug2: set_newkeys: mode 1 9132 15:38:13:151 debug1: rekey after 134217728 blocks 9132 15:38:13:151 debug1: SSH2_MSG_NEWKEYS sent 9132 15:38:13:151 debug1: expecting SSH2_MSG_NEWKEYS 9132 15:38:13:151 debug3: send packet: type 7 9132 15:38:13:151 debug3: receive packet: type 21 9132 15:38:13:151 debug1: SSH2_MSG_NEWKEYS received 9132 15:38:13:151 debug2: set_newkeys: mode 0 9132 15:38:13:151 debug1: rekey after 134217728 blocks 9132 15:38:13:151 debug1: KEX done 9132 15:38:13:167 debug3: receive packet: type 5 9132 15:38:13:167 debug3: send packet: type 6 9132 15:38:13:167 debug3: receive packet: type 50 9132 15:38:13:167 debug1: userauth-request for user test_auto_usr service ssh-connection method none 9132 15:38:13:167 debug1: attempt 0 failures 0 9132 15:38:13:167 debug2: parse_server_config: config reprocess config len 271 9132 15:38:13:167 debug2: input_userauth_request: setting up authctxt for test_auto_usr 9132 15:38:13:167 debug2: input_userauth_request: try method none 9132 15:38:13:167 Failed none for test_auto_usr from 10.0.168.48 port 51524 ssh2 9132 15:38:13:167 debug3: userauth_finish: failure partial=0 next methods="publickey,password,keyboard-interactive" 9132 15:38:13:167 debug3: send packet: type 51 9132 15:38:13:183 debug3: receive packet: type 50 9132 15:38:13:183 debug1: userauth-request for user test_auto_usr service ssh-connection method publickey 9132 15:38:13:183 debug1: attempt 1 failures 0 9132 15:38:13:183 debug2: input_userauth_request: try method publickey 9132 15:38:13:183 debug3: userauth_pubkey: have signature for RSA SHA256:Amf1vjWBG/ngcq4/aQRpvaS2CJAel03pS0THK5SSabo 9132 15:38:13:183 debug1: trying public key file C:\Users\test_auto_usr\.ssh/authorized_keys 9132 15:38:13:183 debug1: matching key found: file C:\Users\test_auto_usr\.ssh/authorized_keys, line 1 RSA SHA256:Amf1vjWBG/ngcq4/aQRpvaS2CJAel03pS0THK5SSabo 9132 15:38:13:183 debug3: socket:0, socktype:1, io:000000347A692B80, fd:6 9132 15:38:13:183 debug3: test_auto_usr authenticated via pubkey 9132 15:38:13:183 debug2: userauth_pubkey: authenticated 1 pkalg rsa-sha2-512 9132 15:38:13:183 Accepted publickey for test_auto_usr from 10.0.168.48 port 51524 ssh2: RSA SHA256:Amf1vjWBG/ngcq4/aQRpvaS2CJAel03pS0THK5SSabo 9132 15:38:13:183 debug3: send packet: type 52 9132 15:38:13:183 debug3: close - io:000000347A6621F0, type:2, fd:4, table_index:4 9132 15:38:13:183 debug3: notify_hostkeys: key 0: ssh-rsa SHA256:l/79rJLA22Ufg3r3REYmNcnoPG3t/oFsATfvSDYNDpQ 11476 15:38:13:183 debug3: close - io:0000001F2FA85D90, type:2, fd:6, table_index:6 9132 15:38:13:183 debug3: notify_hostkeys: key 1: ssh-dss SHA256:hgueF2cofwkLFpfxgfqhfPLkDvYV600H26rzUflBpcI 9132 15:38:13:183 debug3: notify_hostkeys: key 2: ecdsa-sha2-nistp256 SHA256:ihIIAATwL+ZaE9iRPDr0NvQguyPuXnqBfKAyAUQ3wmc 9132 15:38:13:183 debug3: notify_hostkeys: key 3: ssh-ed25519 SHA256:OJRDcYTRtNk98QuhZPP37yZT9lBVtHXXUQNNmPTeVmY 9132 15:38:13:183 debug3: notify_hostkeys: sent 4 hostkeys 9132 15:38:13:183 debug3: send packet: type 80 9132 15:38:13:183 debug1: Entering interactive session for SSH2. 9132 15:38:13:183 debug3: pipe - r-h:296,io:000000347A6621F0,fd:4 w-h:396,io:000000347A691F00,fd:7 9132 15:38:13:183 debug2: fd 4 setting O_NONBLOCK 9132 15:38:13:183 debug2: fd 7 setting O_NONBLOCK 9132 15:38:13:183 debug1: server_init_dispatch 9132 15:38:13:183 debug3: receive packet: type 90 9132 15:38:13:183 debug1: server_input_channel_open: ctype session rchan 0 win 2097152 max 32768 9132 15:38:13:183 debug1: input_session_request 9132 15:38:13:183 debug1: channel 0: new [server-session] 9132 15:38:13:183 debug2: session_new: allocate (allocated 0 max 10) 9132 15:38:13:183 debug3: session_unused: session id 0 unused 9132 15:38:13:183 debug1: session_new: session 0 9132 15:38:13:183 debug1: session_open: channel 0 9132 15:38:13:183 debug1: session_open: session 0: link with channel 0 9132 15:38:13:183 debug1: server_input_channel_open: confirm session 9132 15:38:13:183 debug3: send packet: type 91 9132 15:38:13:183 debug3: receive packet: type 80 9132 15:38:13:183 debug1: server_input_global_request: rtype no-more-sessions@openssh.com want_reply 0 9132 15:38:13:214 debug3: receive packet: type 98 9132 15:38:13:214 debug1: server_input_channel_req: channel 0 request exec reply 1 9132 15:38:13:214 debug1: session_by_channel: session 0 channel 0 9132 15:38:13:214 debug1: session_input_channel_req: session 0 req exec 9132 15:38:13:214 Starting session: command for test_auto_usr from 10.0.168.48 port 51524 id 0 9132 15:38:13:214 debug3: pipe - r-h:324,io:000000347A691CE0,fd:8 w-h:320,io:000000347A691D90,fd:9 9132 15:38:13:214 debug3: pipe - r-h:332,io:000000347A691E40,fd:10 w-h:328,io:000000347A692C30,fd:11 9132 15:38:13:214 debug3: pipe - r-h:340,io:000000347A692CE0,fd:12 w-h:336,io:000000347A692D90,fd:13 9132 15:38:13:214 debug2: fd 8 setting O_NONBLOCK 9132 15:38:13:214 debug2: fd 9 setting O_NONBLOCK 9132 15:38:13:214 debug2: fd 10 setting O_NONBLOCK 9132 15:38:13:214 debug2: fd 11 setting O_NONBLOCK 9132 15:38:13:214 debug2: fd 12 setting O_NONBLOCK 9132 15:38:13:214 debug2: fd 13 setting O_NONBLOCK 9132 15:38:14:384 debug1: Executing command: C:\Program Files\OpenSSH\ssh-shellhost.exe -nopty cG93ZXJzaGVsbC5leGUgLUZpbGUgQzpcVFNDTV9BdXRvbWF0aW9uXFRTQ01fd3JhcHBlci5wczEgcmVnIGljb21yc2R3MDAwNDggdzJrMTIgMTAuMC4xNjguNDg= 9132 15:38:14:384 debug3: Register child 000000000000015C pid 14696, 0 zombies of 0 9132 15:38:14:384 debug2: fd 3 setting TCP_NODELAY 9132 15:38:14:384 debug3: close - io:000000347A691CE0, type:2, fd:8, table_index:8 9132 15:38:14:384 debug3: close - io:000000347A692C30, type:2, fd:11, table_index:11 9132 15:38:14:399 debug3: close - io:000000347A692D90, type:2, fd:13, table_index:13 9132 15:38:14:399 debug3: fd 10 is O_NONBLOCK 9132 15:38:14:399 debug3: fd 9 is O_NONBLOCK 9132 15:38:14:399 debug3: fd 12 is O_NONBLOCK 9132 15:38:14:399 debug3: send packet: type 99 9132 15:39:50:221 debug2: channel 0: read<=0 rfd 10 len 0 9132 15:39:50:221 debug2: channel 0: read failed 9132 15:39:50:221 debug2: channel 0: close_read 9132 15:39:50:221 debug3: close - io:000000347A691E40, type:2, fd:10, table_index:10 9132 15:39:50:221 debug2: channel 0: input open -> drain 9132 15:39:50:221 debug2: channel 0: read 0 from efd 12 9132 15:39:50:221 debug2: channel 0: closing read-efd 12 9132 15:39:50:221 debug3: close - io:000000347A692CE0, type:2, fd:12, table_index:12 9132 15:39:50:221 debug2: channel 0: ibuf empty 9132 15:39:50:221 debug2: channel 0: send eof 9132 15:39:50:221 debug3: send packet: type 96 9132 15:39:50:221 debug2: channel 0: input drain -> closed 9132 15:39:50:221 debug3: zombie'ing child at index 0, 0 zombies of 1 9132 15:39:50:221 debug1: Received SIGCHLD. 9132 15:39:50:221 debug3: Unregister child at index 0, 1 zombies of 1 9132 15:39:50:221 debug1: session_by_pid: pid 14696 9132 15:39:50:221 debug1: session_exit_message: session 0 channel 0 pid 14696 9132 15:39:50:221 debug2: channel 0: request exit-status confirm 0 9132 15:39:50:221 debug3: send packet: type 98 9132 15:39:50:221 debug1: session_exit_message: release channel 0 9132 15:39:50:221 debug2: channel 0: write failed 9132 15:39:50:221 debug2: channel 0: close_write 9132 15:39:50:221 debug3: close - io:000000347A691D90, type:2, fd:9, table_index:9 9132 15:39:50:221 debug2: channel 0: send eow 9132 15:39:50:221 debug3: send packet: type 98 9132 15:39:50:221 debug2: channel 0: output open -> closed 9132 15:39:50:221 debug2: channel 0: send close 9132 15:39:50:221 debug3: send packet: type 97 9132 15:39:50:221 debug3: channel 0: will not send data after close 9132 15:39:50:237 debug2: notify_done: reading 9132 15:39:50:237 debug3: channel 0: will not send data after close

bagajjal commented 7 years ago

@nellfr - Please let us know how to reproduce this issue..

nellfr commented 7 years ago

From the Windows system, the CHEF client is issuing this command : cmd /c "C:\PROGRA~1\OpenSSH-Win64\ssh.exe -vvv -n -o StrictHostKeyChecking=no -i C:\key_path\id_rsa user@10.0.168.48 powershell.exe -File C:\script_path\script_wrapper.ps1 param1 param2 param3 > C:\ssh_output.txt 2>&1"

nellfr commented 6 years ago

Hello, were you able to reproduce this issue? I can provide additional information or logs whenever needed.

rjpackito commented 6 years ago

Same problem, -n does not help

rjpackito commented 6 years ago

You can help to solve the problem, since you need to deploy projects to the Windows server using ssh?

rjpackito commented 6 years ago

@bagajjal

bagajjal commented 6 years ago

@nellfr - I tried the command you gave me but couldn't reproduce "GetConsoleMode on STD_INPUT_HANDLE failed with 6".

image

rjpackito commented 6 years ago

@bagajjal @havalli @nellfr Can there be a problem in Gitlab? I wrote C # code that executes ssh and it normally works.

nellfr commented 6 years ago

@bagajjal , I am executing the ssh command with "-n" can you try with that parameter please? Also I observed that the "GetConsoleMode on STD_INPUT_HANDLE failed with 6" is raised after my target script (script_wrapper.ps1 on SSH server) runs during several minutes. I would suggest to sleep your script "getdate.ps1" for 5 minutes.

nellfr commented 6 years ago

hello @rjpackito , I'm not sure to understand the problem you are facing with ssh. Do you observe "GetConsoleMode on STD_INPUT_HANDLE failed with 6" message while running an ssh command ?

rjpackito commented 6 years ago

@nellfr Yes, I see the same error during the execution of the script on Gitlab Runner CI/CD . I can not understand what the problem is. Because the SCP command works fine. I really need the SSH to deploy on my remote machine my dotnet core application

bagajjal commented 6 years ago

@nellfr - adding "-n", sleep of 5 minutes works perfectly... I didn't see any issue.

@rjpackito - This is a ssh client side issue. You can try other ssh clients (putty/Cygwin) if you are blocked. Unfortunately I couldn't reproduce this issue. It would be great if you can provide reproduction steps.

rjpackito commented 6 years ago

@bagajjal that is, you can not fix the problem in OpenSSH? Can you install the Gitlab Runner?

bagajjal commented 6 years ago

Have you tried other ssh clients? If it works in putty then we can fix in openssh... Right now I can't repro the issue.. I will try the gitlab runner.. Meanwhile try using different ssh client like putty

rjpackito commented 6 years ago

@bagajjal I do not know, can putty do xcopy through cmd?

bagajjal commented 6 years ago

I didn't understand your question.. All the ssh commands get executed on the server side.. ssh client will not execute any command..

rjpackito commented 6 years ago

@bagajjal Hey. I used plink.exe and it works in gitlab-runner.

bagajjal commented 6 years ago

Please reopen if required.

manjeet24feb commented 6 years ago

I am facing the same issue. Tried almost everything -n etc. It is getting stuck at ssh. scp is working fine.

bagajjal commented 6 years ago

Try using -T (no-pty) in ssh.exe.

manjeet24feb commented 6 years ago

Still the same. ssh is not closing the connection. Therefore it is stuck.

bagajjal commented 6 years ago

can you share the ssh client logs (ssh.exe -vvv user@ip) and sshd.log (DEBUG3 enabled) server side logs

anrose00 commented 6 years ago

Encountered this problem myself. It happens when you run SSH as a child process via CreateProcess and stdhandles are being redirected.

I executed this command as an example: ssh -n -T user@server ls

The return code signals, the command was successful, the output to stdout is like you would expect it, but you get output to stderr.

If you put a binary zero into the hStdInput of STARTUPINFO, you will get the following output on StdError: GetConsoleMode on STD_INPUT_HANDLE failed with 6 fcntl(-2, F_GETFL): Bad file descriptor

If you put a handle to a file or /dev/null into the hStdInput of STARTUPINFO, you will get only GetConsoleMode on STD_INPUT_HANDLE failed with 6

It seems that with the option "-T" some code still tries to get the console capabilities using GetConsoleMode, which fails on file handles and triggers this error being written to stderr.

https://serverfault.com/questions/593399/what-is-the-benefit-of-not-allocating-a-terminal-in-ssh

https://docs.microsoft.com/en-us/windows/console/getconsolemode

Hope this helps to fix the problem.

manojampalam commented 6 years ago

@anrose00 can you share a snippet of your code doing CreateProcess(ssh) with io redirection ?

anrose00 commented 6 years ago

Created a simple wrapper that simply passes the first argument to CreateProcess. This way you can write in a shell "sshwrap "ssh -n -T user@server cmd". You will find two files in temp directory: ssh-stdout, ssh-stderr.

   #include <windows.h>
   #include <TCHAR.h>

   HANDLE GetFileHandleForRedirection(LPTSTR sName, BOOL bTmp, INT iRwMode)
   {
      TCHAR sFnBuffer[MAX_PATH];
      SECURITY_ATTRIBUTES stSecAttr;
      HANDLE hFile;

      if (bTmp)
      {
         GetTempPath(MAX_PATH,sFnBuffer);
         strcat(sFnBuffer,sName);
      }
      else
      {
         strcpy(sFnBuffer,sName);
      }

      memset(&stSecAttr,0,sizeof(SECURITY_ATTRIBUTES));
      stSecAttr.nLength=sizeof(SECURITY_ATTRIBUTES);
      stSecAttr.bInheritHandle=TRUE;

      hFile = CreateFile(sFnBuffer,iRwMode,0,&stSecAttr,OPEN_ALWAYS,0,NULL);
      if (hFile == INVALID_HANDLE_VALUE)
      {
         return NULL;
      }
      SetFilePointer(hFile,0,NULL,FILE_END);
      return hFile;
   }

   BOOL SShExecute(LPTSTR path, BOOL wait, BOOL hidden, LPDWORD lastError, LPDWORD returnCode)
   {
      STARTUPINFOA startUpInfo;
      PROCESS_INFORMATION pInfo;
      BOOL res;
      HANDLE hStdIn,hStdOut,hStdErr;

      memset(&startUpInfo,0, sizeof(startUpInfo));
      startUpInfo.cb = sizeof(startUpInfo);
      if (hidden) 
      {
         startUpInfo.wShowWindow = 0;
         startUpInfo.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES | STARTF_USESIZE;
         startUpInfo.dwXSize = 0;  
         startUpInfo.dwYSize = 0;  

         hStdIn = GetFileHandleForRedirection("nul", FALSE, GENERIC_READ );
         if (hStdIn)
            startUpInfo.hStdInput = hStdIn;

         hStdOut = GetFileHandleForRedirection("ssh-stdout", TRUE, GENERIC_WRITE );
         if (hStdOut)
            startUpInfo.hStdOutput = hStdOut;

         hStdErr = GetFileHandleForRedirection("ssh-stderr", TRUE, GENERIC_WRITE );
         if (hStdErr)
            startUpInfo.hStdError = hStdErr;

      }
      res = CreateProcess(NULL,path,NULL,NULL,TRUE,NORMAL_PRIORITY_CLASS,NULL,NULL,&startUpInfo,&pInfo);
      if (res && wait) {
         WaitForSingleObject( pInfo.hProcess, INFINITE );
      }

      if (hStdIn) CloseHandle(hStdIn);
      if (hStdOut) CloseHandle(hStdOut);
      if (hStdErr) CloseHandle(hStdErr);

      if (res) {
         GetExitCodeProcess(pInfo.hProcess,returnCode);
         CloseHandle( pInfo.hProcess );
         CloseHandle( pInfo.hThread );
      }
      else {
         *lastError = GetLastError();
      }
      return res;
   }

   int _tmain(int argc, TCHAR *argv[])
   {
      DWORD dwLastError,dwReturnCode=0;  

      if (argc == 2)
      {
         SShExecute(argv[1], TRUE, TRUE, &dwLastError, &dwReturnCode);
      }
      return dwReturnCode;
   }
anrose00 commented 5 years ago

Issue ist still present in Release 7.9.0.0 - please reopen.

manojampalam commented 5 years ago

Looked into this. It seems that GetFileType on hStdIn (that's pointing to a nul device) is returning FILE_TYPE_CHAR - that's making ssh.exe think that its a console handle, but when the actual console IO is preformed on this handle, it fails resulting in that log. We'll need to investigate if this behavior is by design and accommodate if necessary.

To unblock, you could use a pipe handle (from CreatePipe()) for hStdIn whose other end of the pipe is closed.

anrose00 commented 5 years ago

Quoting from the ssh man pages regarding the "-n" option:

-n' Redirects stdin from /dev/null (actually, prevents reading from stdin). This must be used when ssh is run in the background.

Perhaps it's possible to make console IO for stdin dependent on this option?

anrose00 commented 5 years ago

I changed the minimal example to give the proposal with pipes a try.

#include <windows.h>
#include <TCHAR.h>

HANDLE GetFileHandleForRedirection(LPTSTR sName, BOOL bTmp, INT iRwMode)
{
   TCHAR sFnBuffer[MAX_PATH];
   SECURITY_ATTRIBUTES stSecAttr;
   HANDLE hFile;

   if (bTmp)
   {
      GetTempPath(MAX_PATH,sFnBuffer);
      strcat(sFnBuffer,sName);
   }
   else
   {
      strcpy(sFnBuffer,sName);
   }

   memset(&stSecAttr,0,sizeof(SECURITY_ATTRIBUTES));
   stSecAttr.nLength=sizeof(SECURITY_ATTRIBUTES);
   stSecAttr.bInheritHandle=TRUE;

   hFile = CreateFile(sFnBuffer,iRwMode,0,&stSecAttr,OPEN_ALWAYS,0,NULL);
   if (hFile == INVALID_HANDLE_VALUE)
   {
      return NULL;
   }
   SetFilePointer(hFile,0,NULL,FILE_END);
   return hFile;
}

HANDLE GetReadHandleForRedirection()
{
   SECURITY_ATTRIBUTES stSecAttr;
   HANDLE hPipeRead, hPipeWrite;
   BOOL bRes;

   memset(&stSecAttr,0,sizeof(SECURITY_ATTRIBUTES));
   stSecAttr.nLength=sizeof(SECURITY_ATTRIBUTES);
   stSecAttr.bInheritHandle=TRUE;

   bRes = CreatePipe(&hPipeRead,&hPipeWrite,&stSecAttr,0);
   if (!bRes)
      return NULL;

   CloseHandle(hPipeWrite);
   return hPipeRead;
}

BOOL SShExecute(LPTSTR path, BOOL wait, BOOL hidden, LPDWORD lastError, LPDWORD returnCode)
{
   STARTUPINFOA startUpInfo;
   PROCESS_INFORMATION pInfo;
   BOOL res;
   HANDLE hStdIn,hStdOut,hStdErr;

   memset(&startUpInfo,0, sizeof(startUpInfo));
   memset(&pInfo,0, sizeof(pInfo));

   startUpInfo.cb = sizeof(startUpInfo);
   if (hidden) 
   {
      startUpInfo.wShowWindow = 0;
      startUpInfo.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES | STARTF_USESIZE;
      startUpInfo.dwXSize = 0;  
      startUpInfo.dwYSize = 0;  

      hStdIn = GetReadHandleForRedirection();

      // printf("FileType stdin: %i\r\n",GetFileType(hStdIn));
      if (hStdIn)
         startUpInfo.hStdInput = hStdIn;

      hStdOut = GetFileHandleForRedirection("ssh-stdout.txt", TRUE, GENERIC_WRITE );
      if (hStdOut)
         startUpInfo.hStdOutput = hStdOut;

      hStdErr = GetFileHandleForRedirection("ssh-stderr.txt", TRUE, GENERIC_WRITE );
      if (hStdErr)
         startUpInfo.hStdError = hStdErr;

   }
   res = CreateProcess(NULL,path,NULL,NULL,TRUE,NORMAL_PRIORITY_CLASS,NULL,NULL,&startUpInfo,&pInfo);
   if (res && wait) {
      WaitForSingleObject( pInfo.hProcess, INFINITE );
   }

   if (hStdIn) CloseHandle(hStdIn);
   if (hStdOut) CloseHandle(hStdOut);
   if (hStdErr) CloseHandle(hStdErr);

   if (res) {
      GetExitCodeProcess(pInfo.hProcess,returnCode);
      CloseHandle( pInfo.hProcess );
      CloseHandle( pInfo.hThread );
   }
   else {
      *lastError = GetLastError();
   }
   return res;
}

int _tmain(int argc, TCHAR *argv[])
{
   DWORD dwLastError,dwReturnCode=0;  

   if (argc == 2)
   {
      SShExecute(argv[1], TRUE, TRUE, &dwLastError, &dwReturnCode);
   }
   return dwReturnCode;
}

This seems to work for Windows 7 systems, but not for Windows 10. On Windows 10 I'm still getting the "GetConsoleMode on STD_INPUT_HANDLE failed with 6" although the file type for STD_INPUT_HANDLE is now "FILE_TYPE_PIPE". So either the error message is wrong here, or SSH is using ConsoleAPI for pipe handles. I've detected another more critical issue with Win10, but I will open an extra issue for this.

manojampalam commented 5 years ago

I tried your code on a Windows 10 box and it works fine. I also tried startUpInfo.hStdInput = GetStdHandle(STD_INPUT_HANDLE); with "ssh -n" and that worked fine too.

darkmattercoder commented 5 years ago

I get the same error within a ci job with gitlab runner, executed manually:

PS F:\> $TARGET_DEPLOY_DIR="ssh target mktemp -d"
PS F:\> echo $TARGET_DEPLOY_DIR
/tmp/tmp.uv3oMZ

when executed in the pipeline:

Running with gitlab-runner 11.9.1 (de08a4bb)
  on runner
Using Shell executor...
Running on host...
Fetching changes...
Clean repository
From url
   79f1b1b..50641bc  master     -> origin/master
Checking out 50641bc8 as master...
git-lfs/2.7.1 (GitHub; windows amd64; go 1.11.5; git 6b7fb6e3)

Skipping Git submodules setup
$ $TARGET_DEPLOY_DIR=ssh target mktemp -d
GetConsoleMode on STD_INPUT_HANDLE failed with 6

No difference when I add the -n or -T switches

kainwei commented 5 years ago

Hi All, I met the same problem. And I have downloaded OpenSSH 8 (https://www.mls-software.com/opensshd.html). The problem has been fixed. Without any option(-T or -n) and never see "GetConsoleMode on STD_INPUT_HANDLE failed with 6" again. Thanks for you all. Hope it can help others.

rolandma commented 4 years ago

I installed OpenSSH 8, used -n option, add 5 minutes timeout, switched to git ssh, still stuck at "GetConsoleMode on STD_INPUT_HANDLE failed with 6" and the job will hung for long time.

petr-motejlek commented 4 years ago

Related to #1330 ?

rolandma commented 3 years ago

Related to #1330 ?

If you are doing these in Windows, there's this file permission issue, that you need to disable inheritance then add the current user in the permission of the ssh config file. After that I think I got that problem solved. Somehow if your computer name is the same as user name, it also has problem.

rcarmich commented 3 years ago

Just to throw another data point out there, I'm having this issue with OpenSSH 7.9p1 - getting the 'STD_INPUT_HANDLE failed with 6' issue from a Jenkins runner. It executes the remote command I ask it to, but hangs forever. If I run the same command, same user, from outside of Jenkins, there are no issues. I have not been able to try with 8.x yet.

If I add -vvv to ssh, it appears to be hanging during the cleanup of the socket.

bagajjal commented 3 years ago

@rcarmich - For interactive ssh session we need to get the stdin handle. Are you trying to get interactive ssh session inside jenkins runner? If not then try non-pty session (ssh -T user@ip).

rcarmich commented 3 years ago

@bagajjal Yeah, have tried -T without success. Also making things more complicated, the same ssh command works inside the Jenkins runner in a development environment but not on our production Jenkins setup - same Jenkins version, same OpenSSH version, etc. Very bizarre.

sungakim816 commented 2 years ago

I'm having the same issue as well. I'm using circle ci to deploy my .Net Core console app to a windows server (a bare-metal server) With circle ci 2.1, I'm using windows orb version 2.4.1. I tried to use all the available shell, cmd, powershell and bash and still no luck (Here's the list of all pre installed software on the orb https://circleci.com/docs/2.0/hello-world-windows/#software-pre-installed-in-the-windows-image ). The ssh connection is getting stuck.