Search code examples
powershellsshciscoposh-ssh

Posh-SSH and Cisco Gear


I've been working on doing some scripting with Powershell and the Posh-SSH module. I'm connecting to mainly Cisco gear, but have some other network gear as well. My issue seems to be that I can connect to the gear just fine, but my commands don't seem to run. I've attempted Invoke-SSHCommand as well as creating New-SSHShellStream. What is odd is if I open Powershell and step through each command manually, it appears to work just fine, but for some reason running in a script doesn't produce the results I'm looking for.

I have found that plink.exe works just fine, but I'd really rather code all this from Powershell if possible. Is there something I'm missing with these network devices that might be different than a Linux server?

Code:
New-SSHSession -ComputerName $fw-ip -Credential (Get-Credential) -Verbose<br/> $session = Get-SSHSession -Index 0<br/> $stream = $session.Session.CreateShellStream("dumb", 0, 0, 0, 0, 1000)<br/> $stream.Write("show ver")<br/> $stream.Read()


What I get back:
Type help or '?' for a list of available commands.<br/> FW/Admin>


Solution

  • So after working with you in the comments it looks like there were two problems, the first being that the SSH shell supported by the SSH.NET library does not seem to support partial commands, swapping "Show Ver" to "Show Version" corrected that. In addition the command was taking a bit longer to run than the script was waiting before calling its read() on the stream, which can be fixed by adding a start-sleep -seconds 5 between the write() and the read(). If you are planning to use this with commands that may take longer or where you are not sure how long they will take you may want to look into some additional handling that checks to see if the command is done by checking the dataavailable property or creating a listener for the DataReceived event but if your keeping things simple a basic timer will work great for you.