darkoperator / Posh-SSH

PowerShell Module for automating tasks on remote systems using SSH
BSD 3-Clause "New" or "Revised" License
980 stars 227 forks source link

Invoke-SSHCommandStream only printing the first line #552

Open x3ntrix opened 11 months ago

x3ntrix commented 11 months ago

I noticed that Invoke-SSHCommandStream does not work correctly for some commands. For example:

apt list --upgradable

It will only print a part of the first line ("Listing...") and finish, but the output should be:

Listing... Done
docker-ce-cli/buster 5:24.0.7-1~debian.10~buster amd64 [upgradable from: 5:24.0.6-1~debian.10~buster]
docker-ce-rootless-extras/buster 5:24.0.7-1~debian.10~buster amd64 [upgradable from: 5:24.0.6-1~debian.10~buster]
docker-ce/buster 5:24.0.7-1~debian.10~buster amd64 [upgradable from: 5:24.0.6-1~debian.10~buster]
kubeadm/kubernetes-xenial 1.26.0-00 amd64 [upgradable from: 1.22.2-00]
kubectl/kubernetes-xenial 1.26.0-00 amd64 [upgradable from: 1.22.2-00]
kubelet/kubernetes-xenial 1.26.0-00 amd64 [upgradable from: 1.22.2-00]

I assume that it has something to do with how the apt command prints its output. If the behavior of Invoke-SSHCommandStream cannot be improved, it should be documented as a limitation.

Silex commented 3 months ago

Same problem here.

A simple Invoke-SSHCommandStream -Session $session -Command 'echo 1; echo 2; echo 3' fails (only displays the 1).

Wrapping in sh -c does not help.

darkoperator commented 3 months ago

Interesting I'm not able to replicate this

image image
Silex commented 3 months ago

I'm talking to a https://teltonika-networks.com/products/routers/rut241 device.

Maybe it's a busybox issue or some issue with how the terminal is setup. I'll try to check the env/pty diffs with a normal ubuntu server.

Silex commented 3 months ago

Behavior wise, what happens with -Command 'echo 1; sleep 3; echo 2; sleep 3; echo 3' is that it immediatly prints the 1, then waits 6 seconds, then quits. The 2 & 3 are never printed, but the sleep happened.

It looks like it is confused by newlines (or stream flushes?), like it correctly gets the first one but not subsequent ones.

Rob-Hague commented 3 months ago

I expect that might be improved on the develop branch of SSH.NET, pending the next release. The logic behind the output stream was pretty wonky and has been rewritten