SshNet命令执行 [英] SshNet Command Execution
问题描述
我正在尝试使用C#创建SSH客户端.我正在使用Renci.SshNet库.我在发送命令并从服务器获取适当的响应时遇到问题.
I am trying to create a SSH client using C#. I am using the Renci.SshNet library. I am having issues sending the command and getting an appropriate response from the server.
我一直在测试它的方式是发送 ls 命令,然后是 cd.. 命令,然后是 ls 命令 - 以查看 cd.. 命令是否被正确执行.但是,第一个ls命令和第二个ls命令之间没有区别.我不确定自己在做什么错.也许我使用了错误的Ssh类型.我已经提供了代码和得到的输出.我还添加了预期的输出.
The way I have been testing it is by sending over the ls command, followed by cd.. command, followed by ls command again -- to see whether or not the cd.. command is being executed properly. However, there is no difference between the first ls command and the second ls command. I am not entirely sure what I am doing wrong. Perhaps I am using the wrong Ssh type. I have provided the code and the output I am getting. I have also added the output that I expect.
SshClient ssh = new SshClient("apk0rhel01", "dwaseem", "Da_011235813");
ssh.Connect();
while (true)
{
string command = Console.ReadLine();
if (command == "exit")
{
break;
}
var result = ssh.RunCommand(command);
if (result.Result.Length < 10)
continue;
Console.Write(result.Result);
}
ssh.Disconnect();
输出:
预期输出:
推荐答案
答案与使用Tamir SSH库针对您的问题的答案相同-您正在使用用于执行单个命令的类,该类将登录并每次执行命令.您将无法像shell一样使用它.
The answer to this is the same as the one for your question using the Tamir SSH library - you are using a class intended for executing single commands, which logs in and executes the command each time. You will not be able to use this like a shell.
如果要实现交互式命令,则需要使连接保持打开状态的东西,并且需要在命令完成后进行处理(通常通过查找提示模式来完成).Shell类提供了解决此问题的方法-您可以在其网站上找到它的使用示例.
If you want to implement interactive commands, you'll need something which keeps the connection open, and you'll need to deal with working out when a command has finished (which is normally done by looking for the prompt pattern). The Shell class provides methods for dealing with this - you can find examples of its use on their website.
这篇关于SshNet命令执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!