0

SharpSShとSshExecクラスを使用すると、RunCommandを機能させることができず、常に空の文字列が返されます。SharpSshライブラリをデバッグすると、ストリームからコマンドを読み取ろうとすると-1が返されます。同じライブラリでsftpクラスを使用すると機能しますが、そのクラスは必要なすべてのftpコマンドをサポートしているわけではありません。

これが標準的な例です。これを取得して正しい結果を生成することもできません

SshConnectionInfo input = Util.GetInput();
SshExec exec = new SshExec(input.Host, input.User);
if(input.Pass != null) exec.Password = input.Pass;
if(input.IdentityFile != null) exec.AddIdentityFile( input.IdentityFile );

Console.Write("Connecting...");
exec.Connect();
Console.WriteLine("OK");
while(true)
{
    Console.Write("Enter a command to execute ['Enter' to cancel]: ");
    string command = Console.ReadLine();
    if(command=="")break;
    string output = exec.RunCommand(command);                
    Console.WriteLine(output);
}
Console.Write("Disconnecting...");
exec.Close();
Console.WriteLine("OK");

RunCommand関数でいくつかのコマンドを実行する方法について何かアイデアはありますか?助けてくれてありがとう:)

4

1 に答える 1

1

.RunCommand から標準出力とエラー ストリームを取得するには、投稿した回答を繰り返します: SharpSSH - SSHExec、コマンドを実行し、データを 5 秒間待ちます!

次のオーバーロードを試してみてください。

SshExec exec = new SshExec("192.168.0.1", "admin", "haha");
exec.Connect();
string stdOut = null;
string stdError = null;
exec.RunCommand("interface wireless scan wlan1 duration=5", ref stdOut, ref stdError);

Console.WriteLine(stdOut);
exec.Close();

それらの API が名前が意味することを行う場合、コマンドの標準出力を stdOut に、標準エラーを stdError に入れる必要があります。

標準ストリームの詳細については、http: //en.wikipedia.org/wiki/Standard_streamsをご覧ください。

于 2010-10-25T06:07:00.783 に答える