masterzen / winrm

Command-line tool and library for Windows remote command execution in Go
Apache License 2.0
425 stars 129 forks source link

winrm-cli does not return from commands issued #101

Closed rhoughton-pivot closed 4 years ago

rhoughton-pivot commented 4 years ago

Since the change to go-modules, winrm-cli does not return when issuing commands to remote Windows machines. Following the sample command on the winrm-cli README yields expected ipconfig /all results, but then hangs as though expecting more output.

% ./winrm -hostname 1.1.1.1 -username rhoughton-pivot -password 'password' -https -insecure -port 5986 'ipconfig /all'

Windows IP Configuration

   Host Name . . . . . . . . . . . . : winrm-test
   Primary Dns Suffix  . . . . . . . : 
   Node Type . . . . . . . . . . . . : Hybrid
   IP Routing Enabled. . . . . . . . : No
   WINS Proxy Enabled. . . . . . . . : No
   DNS Suffix Search List. . . . . . : c.apachegeode-ci.internal

Ethernet adapter Ethernet:

   Connection-specific DNS Suffix  . : c.apachegeode-ci.internal
   Description . . . . . . . . . . . : Google VirtIO Ethernet Adapter
   Physical Address. . . . . . . . . : 42-01-0A-80-0F-DC
   DHCP Enabled. . . . . . . . . . . : Yes
   Autoconfiguration Enabled . . . . : Yes
   Link-local IPv6 Address . . . . . : fe80::c3d:2fd1:186b:5b65%3(Preferred) 
   IPv4 Address. . . . . . . . . . . : 10.128.15.220(Preferred) 
   Subnet Mask . . . . . . . . . . . : 255.255.240.0
   Lease Obtained. . . . . . . . . . : Thursday, February 13, 2020 9:30:48 PM
   Lease Expires . . . . . . . . . . : Friday, February 14, 2020 9:30:48 PM
   Default Gateway . . . . . . . . . : 10.128.0.1
   DHCP Server . . . . . . . . . . . : 169.254.169.254
   DHCPv6 IAID . . . . . . . . . . . : 54657290
   DHCPv6 Client DUID. . . . . . . . : 00-01-00-01-25-D7-78-11-42-01-0A-80-0F-DC
   DNS Servers . . . . . . . . . . . : 10.128.0.1
   NetBIOS over Tcpip. . . . . . . . : Enabled

Tunnel adapter isatap.c.apachegeode-ci.internal:

   Media State . . . . . . . . . . . : Media disconnected
   Connection-specific DNS Suffix  . : c.apachegeode-ci.internal
   Description . . . . . . . . . . . : Microsoft ISATAP Adapter
   Physical Address. . . . . . . . . : 00-00-00-00-00-00-00-E0
   DHCP Enabled. . . . . . . . . . . : No
   Autoconfiguration Enabled . . . . : Yes

Tunnel adapter Teredo Tunneling Pseudo-Interface:

   Connection-specific DNS Suffix  . : 
   Description . . . . . . . . . . . : Teredo Tunneling Pseudo-Interface
   Physical Address. . . . . . . . . : 00-00-00-00-00-00-00-E0
   DHCP Enabled. . . . . . . . . . . : No
   Autoconfiguration Enabled . . . . : Yes
   IPv6 Address. . . . . . . . . . . : 2001:0:34f1:8072:c3d:27a2:f57f:f023(Preferred) 
   Link-local IPv6 Address . . . . . : fe80::c3d:27a2:f57f:f023%8(Preferred) 
   Default Gateway . . . . . . . . . : ::
   DHCPv6 IAID . . . . . . . . . . . : 134217728
   DHCPv6 Client DUID. . . . . . . . : 00-01-00-01-25-D7-78-11-42-01-0A-80-0F-DC
   NetBIOS over Tcpip. . . . . . . . : Disabled
atxviking commented 4 years ago

Noticing the same behavior. It's not limited or specific to ipconfig. Basically any command sent exhibits the same behavior.

go version go1.14 darwin/amd64

Have tried on darwin and linux (via a Docker container) with the same results.

atxviking commented 4 years ago

Tested using example from the README:

client.RunWithInput("ipconfig", os.Stdout, os.Stderr, os.Stdin)

Read through some of the tests and tried a modified version based on the test config.
The below worked, which indicates that I didn't understand the meaning of the input. My fault there.
Posting this for anyone else who comes across it.

client.RunWithInput("ipconfig", os.Stdout, os.Stderr, strings.NewReader("this is the input"))
masterzen commented 4 years ago

Hi @atxviking,

I'm not sure I understand what you meant. Do you mean you were able to make it not block waiting for input?

The original report was about the winrm-cli command that was stuck. Indeed winrm-cli tries to read os.Stdin. Theoritically the shell shouldn't give anything to read to the process, but I think it is possible that the code blocks while waiting for os.Stdin to give some bytes that don't exist.

In which case something like:

echo | ./winrm ...

should work and not block.

BTW, @rhoughton-pivot , can you give me a bit more information about the running context (like OS, shell, go version etc).

rhoughton-pivot commented 4 years ago

You should be able to build it yourself:

Working, non-blocking dockerfile using a pinned SHA to build locally Non-working, blocking dockerfile from before we pinned

masterzen commented 4 years ago

Hi, I believe I fixed the issue in the latest winrm-cli version (see masterzen/winrm-cli#9). It's what I thought, winrm-cli was always telling winrm to wait for stdin. But when the command is run from a terminal, then there's nothing to wait for.

Please @rhoughton-pivot and @atxviking , can you check the problem is fixed for your use cases?

Thanks,

atxviking commented 4 years ago

Thanks for looking into this. Working on my end.

rhoughton-pivot commented 4 years ago

Thanks for the fix. Works for us as well.