cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
5101
Views
0
Helpful
5
Replies

Posh-SSH script on Cisco devices

Russel
Level 1
Level 1

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?

 

 

MIBridges Login

5 Replies 5

Jon Marshall
Hall of Fame
Hall of Fame

 

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

balaji.bandi
Hall of Fame
Hall of Fame

may worth use print each step for diag purpose, so you know where it hangs.

 

BB

***** Rate All Helpful Responses *****

How to Ask The Cisco Community for Help

Dan Frey
Cisco Employee
Cisco Employee

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.   

 

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

@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.