SharpSSh:SshExec 中的 RunCommand 始终返回空字符串

发布于 2024-09-06 15:24:13 字数 870 浏览 3 评论 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 函数运行某些命令有什么想法吗? 感谢您的帮助:)

When Using SharpSSh and the SshExec class, I can't get the RunCommand to work, it always returns an empty string. When I debug the SharpSsh library it returns -1 when it tries to read the command from a stream. It works when I use the sftp class in the same library, but that class doesn't support all the ftp commands I need.

Here is a standard example, I can't get this to produce a correct result either

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");

Any ideas on how I can get the RunCommand function to run some commands?
Thanks for any help :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

濫情▎り 2024-09-13 15:24:13

要从 .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,将标准错误放入标准错误。

有关标准流的更多信息,请查看:http://en.wikipedia.org/wiki/Standard_streams

To get the standard output and error streams from .RunCommand,
I'll repeat the answer I posted to: SharpSSH - SSHExec, run command, and wait 5 seconds for data!

You may want to try the following overload:

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();

If their API does what the name implies, it should put the standard output of your command in stdOut and the standard error in stdError.

For more information about standard streams, check this out: http://en.wikipedia.org/wiki/Standard_streams

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文