Question

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?

Was it helpful?

Solution

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");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top