[英]NOT getting the full output from ssh.net SshCommand
我在 Visual Studio 2019 中使用 Renci.SshNet nuget 包(版本 2020.0.1)。
我要做的是在 SSH 上执行命令并返回整个结果文本。
这种方法有效。 但我没有得到全文结果。
/// <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;
}
如果我例如。 将此作为命令发送
command = "\r\r";
我不会在控制台上得到任何东西。
但是如果我在我的 linux 或 windows 终端中通过 ssh 向客户端发送相同的命令,我会得到这个。
RMC3>
这向我表明客户端设备进行了回车。 (正如它应该)。
但是为什么我在结果字符串中看不到这个
string result = sshCommand.Result;
方法里面?
好的。 正如@MartinPrikryl 在上面的评论中指出的那样。
SshCommand() 方法不访问 shell。 (它使用 SSH.NET API 中的“exec”通道)。
如果您想使用 SSH.NET 访问“shell”,则需要使用“ShellStream”。
这里有 27 个不同示例的链接,说明其他人如何在现实世界中使用 ShellStream。 https://csharp.hotexamples.com/examples/Renci.SshNet/ShellStream/-/php-shellstream-class-examples.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.