我正在尝试在 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 .


    评论关闭
    IT序号网

    微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!