简体   繁体   English

没有从 ssh.net SshCommand 获得完整的 output

[英]NOT getting the full output from ssh.net SshCommand

I am using the Renci.SshNet nuget pacget (version 2020.0.1) in visual studio 2019.我在 Visual Studio 2019 中使用 Renci.SshNet nuget 包(版本 2020.0.1)。
What I am trying to do is to execute a command over SSH and return the whole result text.我要做的是在 SSH 上执行命令并返回整个结果文本。

this method works.这种方法有效。 but I am not getting the full-text result back.但我没有得到全文结果。

/// <summary>
    /// Connects to a remote device via SSH and sends it a single command
    /// </summary>
    /// <param name="hostIp">Ip address or hostname for the targer device</param>
    /// <param name="command">A string formated command</param>
    /// <param name="username">username for target remote device login</param>
    /// <param name="password">password for target remote device login</param>
    /// <returns>Text responce from the given command</returns>
    public string SendCommand(string hostIp, string command, string username, string password)
    {
        try
        {
            // Create a SSH client object
            SshClient client = new SshClient(hostIp, PortNumber, username, password);

            #region connect to remote device

            client.Connect();

            // Shecks if there is a valid connection 
            if (client.IsConnected == false) { return string.Empty; }

            #endregion

            // Send and execute a given command on the target remote device
            SshCommand sshCommand = client.RunCommand(command);

            var test = sshCommand.OutputStream;

            string result = sshCommand.Result;

            #region close and dispose the connection

            client.Disconnect();
            client.Dispose();

            #endregion

            return result;
        }
        catch (SocketException socket)
        {
            // Debug and log a socket error
            Debug.WriteLine($"SocketException: {socket}");
            throw;
        }

If I eg.如果我例如。 send this as the command将此作为命令发送

command = "\r\r";

I will get nothing on the console.我不会在控制台上得到任何东西。
but if I send the same command to the client over ssh in my linux or windows terminal I will get this.但是如果我在我的 linux 或 windows 终端中通过 ssh 向客户端发送相同的命令,我会得到这个。

RMC3>

This indicates to me that the client device made a carriage return.这向我表明客户端设备进行了回车。 (as it should). (正如它应该)。
But why can't I see this in the result string但是为什么我在结果字符串中看不到这个

string result = sshCommand.Result;

inside of the method?方法里面?

Okay.好的。 As @MartinPrikryl pointed out in the comments above.正如@MartinPrikryl 在上面的评论中指出的那样。

The SshCommand() method does not access the shell. SshCommand() 方法不访问 shell。 (it uses the "exec" channel in the SSH.NET API). (它使用 SSH.NET API 中的“exec”通道)。

If you want to access the "shell" using SSH.NET, then you will need to use the "ShellStream".如果您想使用 SSH.NET 访问“shell”,则需要使用“ShellStream”。

here is a link to 27 different examples of how others have used ShellStream in the real world.这里有 27 个不同示例的链接,说明其他人如何在现实世界中使用 ShellStream。 https://csharp.hotexamples.com/examples/Renci.SshNet/ShellStream/-/php-shellstream-class-examples.html https://csharp.hotexamples.com/examples/Renci.SshNet/ShellStream/-/php-shellstream-class-examples.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM