Reputation: 11
How can I properly su into root using SSH.Net? This question is similar to: this and this. I couldn't get any of the solutions to the answers to work for me.
Here is my code:
using (var client = new SshClient("ip", "username", "password"))
{
client.Connect();
if (client.IsConnected)
{
var shell = client.CreateShellStream("xterm", 80, 24, 800, 600, 1024);
var reader = new StreamReader(shell);
var writer = new StreamWriter(shell);
writer.AutoFlush = true;
writer.Write("su - root" + "\n");
var output = reader.ReadLine();
Console.WriteLine(output);
/*while (true)
{
var output = reader.ReadLine();
if (output.EndsWith("Password: "))
{
Console.WriteLine("YES");
}
}*/
}
else
{
Console.WriteLine("Connection Failed");
}
client.Disconnect();
}
The output is always "Last login: ..." Is there another way to do this?
Upvotes: 1
Views: 2451
Reputation: 43
The best way I found was this:
sudo = "echo \"" + rootPwd + "\" | sudo -S " + command;
You can even use it with RunCommand:
ssh.RunCommand("echo \"" + rootPwd + "\" | sudo -S " + command);
Upvotes: 4