[英]C# connect and run commands to Dell Switch
我想連接到戴爾 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)”行上。
任何人都可以對此有想法嗎?
僅供參考:以上代碼適用於 Cisco 交換機。我可以通過 Putty 軟件連接到 Dell 和 Cisco 交換機,並且我能夠從 Putty 運行命令。 我的要求是從 C# 應用程序運行命令。
問候
拉維
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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.