簡體   English   中英

Posh-SSH 向 Cisco 交換機發送多個命令

[英]Posh-SSH to send multiple commands to Cisco Switch

我正在嘗試自動化一個過程,其中我有一個 powershell 腳本 SSH 進入我們的 cisco 交換機並打開和關閉一個 Vlan 接口。 當我嘗試運行它時,我得到“使用“1”參數調用“EndExecute”的異常:“服務器中止了一個已建立的連接。””

我的腳本缺少什么導致它像這樣出錯? 謝謝。

Install-Module -Name Posh-SSH

#Set Creds
[ValidateNotNullOrEmpty()]$secpasswd = "myPass"
$secpasswd = ConvertTo-SecureString -String $secpasswd -AsPlainText -Force
$mycreds = New-Object Management.Automation.PSCredential ("myUser", $secpasswd)

#Build SSH session
New-SSHSession "MyIP" -Port 22 -Credential $mycreds
#Assign current session to Var.
$s = Get-SSHSession | Select -ExpandProperty SessionID -First 1

#Send SSH Commands
Invoke-SSHCommand -Command "show log" -SessionId $s
Invoke-SSHCommand -Command "conf t" -SessionId $s
Invoke-SSHCommand -Command "interface vlan 310" -SessionId $s
Invoke-SSHCommand -Command "shut" -SessionID $s
Invoke-SSHCommand -Command "no shut" -SessionID $s
Invoke-SSHCommand -Command "exit" -SessionId $s
Invoke-SSHCommand -Command "exit" -SessionId $s

#Close SSH-Session
Remove-SSHSession -SessionId $s

嘗試使用 SSHShellStream > SSHStreamShellCommand

$SSHsession=New-SSHSession -ComputerName $YourSwitchIP -Credential $Cred -AcceptKey 
$SSHStream = New-SSHShellStream -Index $SSHsession.SessionId
#---> Enter conf
$cmd="conf"
Invoke-SSHStreamShellCommand -ShellStream $SSHStream -Command $cmd

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM