没有从 ssh.net SshCommand 获得完整输出
NOT getting the full output from ssh.net SshCommand
我在 visual studio 2019 年使用 Renci.SshNet nuget packet(版本 2020.0.1)。
我想要做的是通过 SSH 和 return 整个结果文本执行命令。
这个方法有效。但是我没有得到全文结果。
/// <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>
这向我表明客户端设备进行了回车 return。 (正如它应该)。
但是为什么我在结果字符串中看不到这个
string result = sshCommand.Result;
方法内部?
好的。正如@MartinPrikryl 在上面的评论中指出的那样。
SshCommand() 方法不访问 shell。 (它使用 SSH.NET API 中的“exec”通道)。
如果要使用 SSH.NET 访问“shell”,则需要使用“ShellStream”。
这里有一个 link 到 27 个不同的例子,说明其他人如何在现实世界中使用 ShellStream。
https://csharp.hotexamples.com/examples/Renci.SshNet/ShellStream/-/php-shellstream-class-examples.html
我在 visual studio 2019 年使用 Renci.SshNet nuget packet(版本 2020.0.1)。
我想要做的是通过 SSH 和 return 整个结果文本执行命令。
这个方法有效。但是我没有得到全文结果。
/// <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>
这向我表明客户端设备进行了回车 return。 (正如它应该)。
但是为什么我在结果字符串中看不到这个
string result = sshCommand.Result;
方法内部?
好的。正如@MartinPrikryl 在上面的评论中指出的那样。
SshCommand() 方法不访问 shell。 (它使用 SSH.NET API 中的“exec”通道)。
如果要使用 SSH.NET 访问“shell”,则需要使用“ShellStream”。
这里有一个 link 到 27 个不同的例子,说明其他人如何在现实世界中使用 ShellStream。 https://csharp.hotexamples.com/examples/Renci.SshNet/ShellStream/-/php-shellstream-class-examples.html