Execute multiple commands via SSH and PowerShell

扶醉桌前 提交于 2019-12-19 08:54:26

问题


I successfully managed to connect to a Cisco IE-2000-L switch via SSH. I used the Renci SSH.NET library.

Starting guide: http://vwiki.co.uk/SSH_Client_(PowerShell)

My working code is

# Load SSH library (for .NET 4.0 and PowerShell 3)
$DllPath = "D:\temp\Renci.SshNet.dll"
[void][reflection.assembly]::LoadFrom( (Resolve-Path $DllPath) )

# Connect to switch (Cisco IE2000-L) with IP, port, username, password
$SshClient = New-Object Renci.SshNet.SshClient('172.20.91.30', 22, 'admin', 'mypassword')
$SshClient.Connect()

# execute one command on Cisco switch
$SshCommand = $SshClient.RunCommand('show arp')

# show result
$SshCommand.Result 

# close SSH connection
$SshCommand.Dispose()
$SshClient.Disconnect()
$SshClient.Dispose()

My problem is

The above code sends just one command. But I want to execute several commands consecutively without closing and reopening a session.

If I add a second command right after the first one

# execute one command on Cisco switch
$SshCommand = $SshClient.RunCommand('show arp')
$SshCommand = $SshClient.RunCommand('show start')

...the script hangs and never finishes. What am I doing wrong?


Minor relevant information

  • My main goal is to send multiple commands at once to a Cisco switch
  • I already tried Plink together with batch cmd input. It's not reliable enough. It works sometimes and sometimes not.
  • I already tried telnet scripting. Too awkward.

来源:https://stackoverflow.com/questions/24436507/execute-multiple-commands-via-ssh-and-powershell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!