简体   繁体   English

SSH.NET passwd没有执行

[英]SSH.NET passwd not executing

I can't seem to make this library to work. 我似乎无法使这个库工作。 I'm doing something pretty straightforward but still I can't manage to do it. 我正在做一些非常简单的事情,但我仍然无法做到这一点。

            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();

If I try then to login with test using the credentials just posted it fails. 如果我尝试使用刚刚发布的凭据登录测试失败。 But if I do the same commands through normal SSH through my terminal I can log in. Why SSH.NET is not executing those commands? 但如果我通过终端通过普通SSH执行相同的命令,我可以登录。为什么SSH.NET不执行这些命令?

The problem is because after you run client.RunCommand("sudo passwd test"); 问题是因为在运行client.RunCommand("sudo passwd test"); a program is running, waiting for input. 程序正在运行,等待输入。 The RunCommand method won't actually run the command until after the program returns. 在程序返回之前, RunCommand方法实际上不会运行该命令。

I encountered the same problem when trying to run su - <login> and found the following workaround... 我在尝试运行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");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM