【发布时间】:2016-10-19 05:36:52
【问题描述】:
我正在尝试自动配置远程主机,我们有数百个这样的设备,我们通常通过 USB 编程来完成,但如果我可以得到一个脚本来连接到这些设备并以编程方式完成,它会腾出时间.
这些设备运行某种类型的 linux 操作系统,我不确定,但它们确实启用了 SSH,并在您第一次通过 PuTTY 等实用程序连接到它们时确认服务器主机密钥。
目前,我只是尝试启动与设备的 SSH 会话。我做了很多研究,并提出了这个:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Renci.SshNet;
using Renci.SshNet.Common;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
//Connection information
string user = "admin";
string pass = "********";
string host = "IP Address";
//Set up the SSH connection
using (var client = new SshClient(host, user, pass))
{
//Accept Host key
client.HostKeyReceived += delegate (object sender, HostKeyEventArgs e)
{
e.CanTrust = true;
};
//Start the connection
client.Connect();
var output = client.RunCommand("show device details");
client.Disconnect();
Console.WriteLine(output.ToString());
Console.ReadLine();
}
}
}
}
问题是这似乎没有执行列出的命令。控制台窗口出现,我可以通过 WebGUI 访问同一设备并查看日志文件,它显示正在建立连接,但是当我中断执行并查看变量值时,输出变量显示为 null。
如果我让执行坐下来,控制台窗口打开(左上角只显示一个闪烁的光标),连接在 10 分钟后超时并且连接丢失,我在设备日志中也看到了这种情况。
为什么这似乎没有执行 run 命令并将结果存储在输出变量中?
【问题讨论】:
-
这些设备可能不支持 ssh 执行请求。尝试从命令行运行
ssh admin@host 'show device details',看看你是否得到了预期的输出。