SSH.NET passwd not executing

久未见 提交于 2019-12-06 13:44:36

问题


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?


回答1:


The problem is because after you run 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.

I encountered the same problem when trying to run su - <login> and found the following workaround...

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


来源:https://stackoverflow.com/questions/22445316/ssh-net-passwd-not-executing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!