我想连接到Dell X1008交换机并运行一些命令。我使用过C#Tamir.SharpSsh和Renci.SshNet库。
Using Tamir.SharpSsh;
SshExec exec = new SshExec("ip address", "username", "password");
exec.Connect();
var output = exec.RunCommand("show vlan");
exec.Close();
但我的代码冻结了" exec.RunCommand(" show vlan")"线。
using Renci.SshNet;
using (var client = new SshClient(Host, UserName, Password))
{
try
{
client.Connect();
}
catch (Exception ex)
{
throw;
}
var command = client.CreateCommand("show vlan");
return command.Execute();
}
这里我的代码冻结了" var command = client.CreateCommand(cmd)"线。
任何人都可以了解它吗?
仅供参考:以上代码适用于思科交换机。我可以通过Putty软件连接到戴尔和思科交换机,我可以运行来自putty的命令。我的要求是从C#应用程序运行命令。
此致
拉维
答案 0 :(得分:0)
Renci.SshNet是更好的选择。
检查此方法的响应,该命令有超时。
public string ExecuteCommandSsh(string host, int port, string username, string password, string[] commands)
{
var returnMessage = string.Empty;
try
{
using (var client = new SshClient(host, port, username, password))
{
//Create the command string
var fullCommand = string.Empty;
foreach (var command in commands)
{
fullCommand += command + "\n";
}
client.Connect();
var sshCommand = client.CreateCommand(fullCommand);
sshCommand.CommandTimeout = new TimeSpan(0, 0, 10);
try
{
sshCommand.Execute();
returnMessage = sshCommand.Result;
}
catch (SshOperationTimeoutException)
{
returnMessage = sshCommand.Result;
}
client.Disconnect();
}
}
catch (Exception e)
{
//other exception
}
return returnMessage;
}