04-06-2022 03:03 AM - edited 04-06-2022 09:41 PM
I made a Posh-SSH based script that will let me send commands to Cisco routers. It works great... except for a very specific case.
function Send-SSHCommand { Param( [Parameter(Mandatory=$true)] [String]$Command, [Parameter(Mandatory=$false)] [string]$DeviceName = "CiscoRouter" ) remove-variable Response -ErrorAction SilentlyContinue Get-SSHSession | select SessionId | Remove-SSHSession | Out-Null New-SSHSession -ComputerName $DeviceName -AcceptKey -Credential $Credential | Out-Null $session = Get-SSHSession -Index 0 $stream = $session.Session.CreateShellStream("dumb", 0, 0, 0, 0, 1000) $stream.Write("terminal Length 0`n") sleep -mill 60 $stream.Read() | Out-Null $stream.Write("$Command`n") $ResponseRaw = $stream.Read() $Response = $ResponseRaw -split "`r`n" | %{$_.trim()} while (($Response[$Response.Count -1]) -ne "$DeviceName#") { sleep -mill 60 $ResponseRaw = $stream.Read() $Response = $ResponseRaw -split "`r`n" | %{$_.trim()} } Return $Response }
Usage Examples that work correctly:
Send-SSHCommand "sh ip cache flow" -DeviceName "CiscoRouter1"
Send-SSHCommand "sho ip int bri | include Serial|NVRAM|Multilink1|manual" -DeviceName "CiscoRouter1"
Send-SSHCommand "sho bgp sum" -DeviceName "CiscoRouter1"
However, the script will hang indefinitely when I use a command like below on any device that doesn't return a respective result for the command. The script is expecting a result. So, when its executed on a device that doesn't return any information for a command, the script just hangs indefinitely:
Send-SSHCommand "sh flow monitor" -DeviceName "CiscoRouter2"
This is what it looks like on a device's console that doesn't have a respective result for sho flow monitor:
CiscoRouter2#sh flow monitor
CiscoRouter2#
The script works perfectly in cases like below where there is information returned like below:
CiscoRouter1#sh flow monitor
Flow Monitor Traffic_INPUT:
Description: DDR-FO NetFlow
Flow Record: SER-INPUT
CiscoRouter1#
Could someone please help me with the while logic so it doesn't hang in cases where no information is returned from the command?
04-06-2022 03:30 AM
What does $response[$response.count - 1] return when there is no output from the command ?
It looks like you are expecting just the prompt ?
Jon
04-06-2022 06:52 AM
may worth use print each step for diag purpose, so you know where it hangs.
04-06-2022 02:05 PM - edited 04-07-2022 11:24 AM
I would think its getting stuck in an infinite loop on the while command. Try adding the while loop under an "if Response length > 0, else return Response. Only enter the while loop if there is output in the Response variable.
04-07-2022 12:36 AM
Daniel
That was my thought as well ie. use an if statement but even if there is no output the while command should work as there is still the prompt that comes back.
If the prompt didn't come back then it would never break out of the loop even if there was output from the command.
Maybe it is just not getting read on the first $stream.Read() which is why I asked if he could print out the response.
Jon
Jon
06-02-2022 09:41 AM
@Jon Marshall, @Dan Frey, The reason the OP of this forum thread didn't reply to your questions is because he didn't write any of the code. It was copied from my posts below. I wrote this PowerShell function to return Cisco command queries as fast as possible using the Posh-SSH PowerShell module.
powershell - Posh-SSH script on Cisco devices - Stack Overflow
Assistance with waiting minimum time to grab full SSH stream response via $stream.Read() - bytemeta
Without the wait command, slower Cisco device responses will be truncated depending on how long it takes for the command to complete. NOTE: The end of each full response should always end with: $DeviceName# ($DeviceName being the hostname of the Cisco device). I'm sure there may be some exceptions where this may not work. However, it has worked great for all Cisco routers and switches I've tried so far; and, can be modified to meet specific cases.
Discover and save your favorite ideas. Come back to expert answers, step-by-step guides, recent topics, and more.
New here? Get started with these tips. How to use Community New member guide