c# - 在 C# 中显示完整的 SSH shell 输出,包括登录消息和提示

标签 c# .net ssh ssh.net

我正在尝试在 C# 中创建一个简单的 SSH 客户端。这是我现在的代码:

using Renci.SshNet;

public static void Main(string[] args)
{
    AuthenticationMethod method = new PasswordAuthenticationMethod("pi", "raspberry");
    ConnectionInfo connection = new ConnectionInfo("192.168.91.134", "pi", method);
    SshClient client = new SshClient(connection);

    if (!client.IsConnected)
    {
        Console.WriteLine("Not Connected...");
        client.Connect();
    }

    while (true)
    {
        string command = Console.ReadLine();
        SshCommand response = client.RunCommand(command);
        Console.WriteLine(response.Result);
    }

}

问题:
  • 像这样,它只显示发送命令的响应。我想要整个输出,包括用户和当前目录(如经典的 SSH shell)。
  • 如果我想启动 sudo su command给个密码就不行了。。。
    (将来我想将输出添加到列表框并从 winForms 应用程序中的 texbox 获取输入)

  • 预先感谢您的帮助

    最佳答案

    在你想实现一个 SSH 终端客户端(如 PuTTY)时,你需要使用 SSH“shell” channel 。
    在 SSH.NET 中,使用 SshClient.CreateShellSshClient.CreateShellStream ,而不是 SshClient.RunCommand .

    关于c# - 在 C# 中显示完整的 SSH shell 输出,包括登录消息和提示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61158899/

    相关文章:

    c# - 如何确定异步套接字服务器中的流结束

    c# - 查看 TreeView 中的最后一个节点时出现问题

    c# - Entity Framework 的悲观并发

    asp.net - HttpApplicationState.RemoveAll() 线程安全吗?

    c# - 在 parallel.foreach 范围之外递增计数值

    c# - 通过反射调用带有参数的泛型方法时,无法将 System.Int32 类型的对象转换为 System.Object[]

    c# - 对线程池输出进行排序以流式传输数据

    windows - 几个 SSH key 和用户帐户

    SVN远程仓库

    ssh - 'the agent has no identities' 是什么意思?