【问题标题】:SharpSSh: RunCommand in SshExec is always returning an empty stringSharpSSh:SshExec 中的 RunCommand 总是返回一个空字符串
【发布时间】:2010-06-21 18:39:04
【问题描述】:

当使用 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 函数运行一些命令的任何想法? 感谢您的帮助:)

【问题讨论】:

    标签: upload ssh sftp sharpssh


    【解决方案1】:

    要从 .RunCommand 获取标准输出和错误流, 我将重复我发布到的答案:SharpSSH - SSHExec, run command, and wait 5 seconds for data!

    您可能想尝试以下重载:

    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

    【讨论】:

      猜你喜欢
      • 2018-11-18
      • 1970-01-01
      • 2022-01-22
      • 1970-01-01
      • 2013-10-09
      • 1970-01-01
      • 2016-12-16
      • 2019-07-25
      • 2013-04-01
      相关资源
      最近更新 更多