Mol*_*ole 5 ssh powershell plink batch-processing winforms
我需要向多个设备发送命令列表。对于每个 IP,使用用户和密码文本框中给定的凭据打开 SSH 连接,运行所有命令并将输出返回到输出文本框。

通常我会使用
plink.exe -ssh admin@172.16.17.18 -pw PassW0rd "command"
Run Code Online (Sandbox Code Playgroud)
不幸的是,远程主机不允许我这样做:
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
Run Code Online (Sandbox Code Playgroud)
但是,如果我在不传递命令的情况下进行连接:
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>#
Run Code Online (Sandbox Code Playgroud)
现在,我可以输入命令并执行它们。我尝试了 PoshSSH,它可以让我连接,但任何命令都会超时。
我将 IP 框和命令框的行分解为字符串数组并进行 for 循环。然后我尝试了几种方法,有成功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
}
}
Run Code Online (Sandbox Code Playgroud)
编辑:感谢 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
}
Run Code Online (Sandbox Code Playgroud)
返回:
~~~~~ 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
Run Code Online (Sandbox Code Playgroud)
现在我需要实现两件事:
从相应的输入字段中获取凭据(目前,我需要为每个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
Run Code Online (Sandbox Code Playgroud)让它等待命令完成,然后再发送下一个命令。(目前只等待1秒)
不过,我很高兴至少远程主机现在可以与我交谈。谢谢。
如果我需要脚本的进一步帮助或继续在此处记录进度,我是否应该提出新问题?
显然,您正在连接到某个“设备”,而不是完整的服务器。嵌入式 SSH 实现通常不实现 SSH“exec”通道。您必须使用“shell”通道(否则不建议用于命令自动化)。
使用 Plink,您可以通过将“命令”写入 Plink 输入来实现这一点,而不是使用-m开关。
另请参阅使用 Plink 执行命令不起作用,但在 PuTTY 中可以。
尽管您不应该运行外部应用程序来实现 SSH。使用本机 .NET SSH 实现,例如SSH.NET。在“shell”通道中使用 SSH.NET 执行命令的伪代码:
var client = new SshClient(...);
client.Connect();
var shell = client.CreateShellStream(...);
shell.WriteLine("command");
var output = shell.Read();
Run Code Online (Sandbox Code Playgroud)
“外壳”通道是一个带有输入和输出的黑匣子。没有可靠的方法可以使用它来执行命令、读取其输出以及执行其他命令。您没有 API 可以判断命令执行何时结束。这就是为什么我在上面写到不建议使用“shell”通道进行命令自动化。您所能做的就是解析 shell 输出并查找设备的命令提示符:System_20>。
| 归档时间: |
|
| 查看次数: |
10199 次 |
| 最近记录: |