C#连接并运行戴尔交换机命令
我想连接到Dell X1008交换机并运行一些命令。我使用了C#Tamir.SharpSsh和Renci.SshNet库C#连接并运行戴尔交换机命令,c#,.net,networking,ssh,putty,C#,.net,Networking,Ssh,Putty,我想连接到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"); e
using Tamir.SharpSsh;
SshExec exec = new SshExec("ip address", "username", "password");
exec.Connect();
var output = exec.RunCommand("show vlan");
exec.Close();
但我的代码冻结在“exec.RunCommand(“showVLAN”)行上
这里我的代码冻结在“var command=client.CreateCommand(cmd)”行
有人知道这件事吗
仅供参考:以上代码适用于Cisco交换机。我可以通过Putty软件连接到Dell和Cisco交换机,并且可以运行Putty的命令。我的要求是从C#应用程序运行命令
问候
拉维是更好的选择
检查此方法的响应,命令有一个超时
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;
}
是更好的选择
检查此方法的响应,命令有一个超时
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;
}
1) 甚至不要尝试SharpSSh,这是一个失败的项目。2) 您确定它挂起在
CreateCommand
上吗?这几乎没有什么作用。我希望它挂起命令。执行。3) 能否使用plink hostname show vlan执行该命令?(PuTTY软件包中的PLink)可能相关:.1)甚至不要尝试SharpSSh,这是一个死项目。2) 您确定它挂起在CreateCommand
上吗?这几乎没有什么作用。我希望它挂起命令。执行。3) 能否使用plink hostname show vlan执行该命令?(油灰包装中的滴答声)可能相关:。这如何回答问题?您发布的代码与OP使用的代码相同。+你说的“Renci.SshNet是更好的SSH.NET库”是什么意思?OP有两个库Tamir.SharpSsh和Renci。我的示例中有一个命令超时。有了超时,就可以显示响应了。这是如何回答问题的?您发布的代码与OP使用的代码相同。+你说的“Renci.SshNet是更好的SSH.NET库”是什么意思?OP有两个库Tamir.SharpSsh和Renci。我的示例中有一个命令超时。通过超时可以显示响应。