Posh-SSH icon indicating copy to clipboard operation
Posh-SSH copied to clipboard

Running "terminal length 0" in SSHShellStream does not return any output

Open loralavandrel opened this issue 1 year ago • 7 comments

In version 3.1.3 once connected to Cisco Switches in SSHShellStream running "terminal length 0" with bellow commands to display multiple lines was working fine. Since 3.2.3 same commands are failing to generate any output.

$shellStream = New-SSHShellStream -SessionId $SessionID.SessionId $shellStream.WriteLine("terminal length 0") $shellStream.WriteLine("show interfaces status | include disabled.* 99 ")

I was able to force to import version 3.1.3 as a workaround.

loralavandrel avatar Sep 11 '24 07:09 loralavandrel

This is at the ssh.net library level. When you create the stream and do a read don you get output?

darkoperator avatar Sep 11 '24 10:09 darkoperator

In version 3.1.3 running mentioned commands in output we have a response from the devices, so the output looks like:

[switch_name]#terminal length 0
[switch_name]#show interfaces status | include disabled.* 99
table of interfaces with specific condition

Running the same in 3.2.3 does return only below output:

[switch_name]#terminal length 0
[switch_name]#show interfaces status | include disabled.* 99

If I remove "terminal length 0" command in 3.2.3 I do have an output of interfaces however they have been truncated as the output is bigger than default 24 lines.

loralavandrel avatar Sep 11 '24 23:09 loralavandrel

what would happen if you added a

Start-Sleep -Seconds 2

before the

$shellStream.WriteLine("terminal length 0")

It could be it takes now longer for the shellstream to be created with all the updates the library did and we are hitting a race condition

darkoperator avatar Sep 12 '24 00:09 darkoperator

Adding "Start-Sleep -Seconds 3" did not resolve it. I have output generated however I do not see all of the output that is generated like in 3.1.3 version. In 3.2.3 I can only see the commands that were provided without the generated output from "show interfaces status | include disabled.* 99" command.

loralavandrel avatar Sep 12 '24 02:09 loralavandrel

Wonder if it is related to this, since you are using the .Net objects it seems to be more of a library issue StreamReader on ShellStream runs into a lock · Issue #1485 · sshnet/SSH.NETgithub.com

darkoperator avatar Sep 12 '24 09:09 darkoperator

I've got the same issue and was able to find some more details.

@loralavandrel It seems like the 1st command after the "terminal length 0" is not treated right. My testing results:

  • If you issue a command with short output, it works fine
  • If you issue a command with much output (where terminal length 0 is needed for) it skips allot of the output. If the output is long enough you'll see the executed command + last part of the output. For example "show run" or "show int".
  • After the first failure of a command with much output, all commands from that point on are treated correctly. So if i issue "show run" or "show int" again, all output is displayed now, no matter how many times i try. A command with short output won't fix it, it needs to fail with a long output first.

The issue is not present in version 3.1.3. Issue present starting from version 3.2.0

So in conclusion: the 1st command with much output after "terminal length 0" skips allot of output when reading it out. This must be related to a SSH.NET change/bug.

Patrick92x avatar Jan 28 '25 19:01 Patrick92x

The following option (which uses the "CreateShellStream" in place of the "New-SSHShellStream" Cmdlet and "Write" in place of "WriteLine") seems to provide a decent Workaround for Cisco iOS Devices.

$SSHSession = New-SSHSession -ComputerName "Router01" -AcceptKey -Credential $Credentials
$shellStream = $SSHSession.session.CreateShellStream("dumb", 0, 0, 0, 0, 10000)

$shellStream.Write("terminal length 0`n")
Start-Sleep -Seconds 1
$shellStream.Read() | Out-Null

$shellStream.Write("show interfaces status | include disabled.* 99`n")
Start-Sleep -Seconds 2
$shellStream.Read()

Please refer to the following Post, for additional Information. https://www.reddit.com/r/PowerShell/s/5AuJZHjOHc

mrmattipants avatar Mar 27 '25 16:03 mrmattipants