我需要将命令列表发送到多个设备。对于每个IP,从User和Password文本框中打开具有给定凭据的SSH连接,运行所有命令并将输出返回到Output文本框中。
<< img src =“ https://image.soinside.com/eyJ1cmwiOiAiaHR0cHM6Ly9pLnN0YWNrLmltZ3VyLmNvbS91UUlzTC5wbmcifQ==” alt屏幕截图,包含用于crecretial,IP,命令和输出的文本框>
通常我会用
plink.exe -ssh [email protected] -pw PassW0rd "command"
不幸的是,远程主机不允许我这样做:
Sent password Access granted Opening session as main channel Opened main channel Server refused to start a shell/command FATAL ERROR: Server refused to start a shell/command
但是,如果我没有交出命令就连接:
Sent password Access granted Opening session as main channel Opened main channel Allocated pty (ospeed 38400bps, ispeed 38400bps) Started a shell/command Welcome to XXX System_Name>#
现在,我可以键入命令并执行它们。我尝试了PoshSSH,它使我可以连接,但是任何命令都超时。
我将IP和Command框中的行分解为字符串数组,并进行了循环。然后,我尝试使用Start-Job
和SystemDiagnostics.Process*
的几种方法均未成功。现在我有点笨了,不胜感激:
for ($a=0; $a -lt $IPArray.Length; $a++){ # Open an interactive Session with plink like # plink.exe -ssh ' + $User + '@' + $IPArray[$a] + ' -pw ' + $Passwd for ($b=0; $b -lt $CommandArray.Length; $b++){ # Send $CommandArray[$b] to plink-Session # Wait for command to finish # Read output and send it to the textbox } }
编辑:感谢Martin Prikryl的回答,我进一步走了一步:
for ($a=0; $a -lt $IPArray.Length; $a++){ $User = $UserTextBox.text $IP = $IPArray[$a] # $Passwd = $PwdTextBox.Text $Outputtext= $Outputtext + "~~~~~ " + $IP + " ~~~~~" + "`r`n" $isSession = New-SSHSession -ComputerName $IP -Credential $User $isStream = $isSession.Session.CreateShellStream("PS-SSH", 0, 0, 0, 0, 1000) for ($b=0; $b -lt $CommandArray.Length; $b++){ $Command = $CommandArray[$b] $isStream.WriteLine("$Command") Start-Sleep -Seconds 1 } $isReturn = $isStream.Read() $Outputtext= $Outputtext + $isReturn + "`r`n" $outputBox.Text = $Outputtext }
返回:
~~~~~ 172.16.17.18 ~~~~~ Welcome to XXX System_18>#echo 1 1 System_18>#echo 2 2 System_18>#ping 8.8.8.8 PING 8.8.8.8 56 data bytes ~~~~~ 172.16.17.19 ~~~~~ Welcome to XXX System_19>#echo 1 1 System_19>#echo 2 2 System_19>#ping 8.8.8.8 PING 8.8.8.8 56 data bytes ~~~~~ 172.16.17.20 ~~~~~ Welcome to XXX System_20>#echo 1 1 System_20>#echo 2 2 System_20>#ping 8.8.8.8 PING 8.8.8.8 56 data bytes
现在我需要实现两件事:
从相应的输入字段中获取凭据(当前,我需要为每个IP输入一次密码)
完成:
$User = $UserTextBox.text
$Passwd = ConvertTo-SecureString $PwdTextBox.Text -AsPlainText -Force
$SSHCred = New-Object System.Management.Automation.PSCredential -ArgumentList ($User, $Passwd)
# ...
$isSession = New-SSHSession -ComputerName $IP -Credential $SSHCred
使它等待命令完成,然后再发送下一个命令。 (当前,它仅等待1秒钟)
但是,我很高兴,至少现在,远程主机正在与我对话。谢谢。
如果我需要进一步的脚本帮助或继续记录进度,我应该在这里提出新的问题吗?
我需要将命令列表发送到多个设备。对于每个IP,从“用户”和“密码”文本框中打开具有给定凭据的SSH连接,运行所有命令,然后将输出返回到...