【问题标题】:SSH.NET passwd not executingSSH.NET 密码未执行
【发布时间】:2014-03-17 01:04:28
【问题描述】:

我似乎无法让这个库工作。 我正在做一些非常简单的事情,但我仍然无法做到。

            client.Connect();
            client.RunCommand("sudo passwd test");
            Thread.Sleep(1000);
            client.RunCommand("testtest");
            Thread.Sleep(1000);
            client.RunCommand("12345");
            Thread.Sleep(1000);
            client.RunCommand("12345");
            bool primera = true;
            client.Disconnect();

如果我尝试使用刚刚发布的凭据进行测试登录,则会失败。但是,如果我通过终端通过普通 SSH 执行相同的命令,我可以登录。 为什么 SSH.NET 不执行这些命令?

【问题讨论】:

    标签: c# visual-studio-2012 ssh ssh.net


    【解决方案1】:

    问题是因为在你运行client.RunCommand("sudo passwd test"); 之后,一个程序正在运行,等待输入。 RunCommand 方法直到程序返回后才会真正运行命令。

    我在尝试运行su - <login> 时遇到了同样的问题,并找到了以下解决方法...

    Shell = Client.CreateShellStream("xterm", 80, 24, 800, 600, 1024);
    
    Reader = new StreamReader(Shell);
    Writer = new StreamWriter(Shell);
    
    Writer.AutoFlush = true;
    
    Writer.Write("su - " + login2 + "\n");
    
    while (true)
    {
        var output = Reader.ReadLine();
    
        if (output.EndsWith("Password: "))
        {
            break;
        }
    }
    
    Writer.Write(password2 + "\n");
    

    【讨论】:

    • 非常感谢!最重要的是使用.Write(... +"\n") 而不是.WriteLine(...)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多