2017-09-25 12 views
0

Dell X1008スイッチに接続して、いくつかのコマンドを実行します。私はC#Tamir.SharpSshとRenci.SshNetライブラリを使用しています。C#でDellスイッチに接続してコマンドを実行する

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)"行でフリーズします。

誰もそれについて考えることができますか?

FYI:上記のコードは、Ciscoスイッチでうまくいきます。私はDellとCiscoのスイッチにPuttyソフトウェアで接続でき、私はputtyからコマンドを実行できます。私の要件は、C#アプリケーションからコマンドを実行することです。

よろしく

ラヴィ

+0

1)SharpSShを試しても、それは駄目なプロジェクトです。 2) 'CreateCommand'でハングアップしていますか?それはほとんど何もしません。私はそれが 'command.Execute'に掛かると期待しています。 3) 'plink hostname show vlan'を使ってコマンドを実行できますか? (PuTTYパッケージの一部でPLink) –

答えて

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; 
} 
+0

これはどうやって質問に答えますか?あなたは、OPが使用しているのと同じコードを投稿しました。 +また、「Renci.SshNetはSSH.NETより優れたライブラリです」*とはどういう意味ですか? –

+0

OPにはTamir.SharpSshとRenciの2つのライブラリがあります。私の例では、コマンドのタイムアウトがあります。タイムアウトを設定すると、応答を表示することができます。 – live2

関連する問題