0

I'm using SSH.NET in order to get some informations of an external tape drive. Connecting and executing some basic commands works perfectly fine for me:

ConnectionInfo connNfo = new ConnectionInfo("10.12.2.97", 22, "loginuser",
new AuthenticationMethod[]
{
// Password based Authentication
new PasswordAuthenticationMethod("loginuser","password")
}
);

using (var client = new SshClient(connNfo))
{
    client.Connect();

    //This command works fine
    using (var cmd = client.CreateCommand("ls"))
    {
        string result = cmd.Execute();
    }
}

But now i have to switch to root user in order to execute extended commands i need. Initially logging in with root user is not possible because of security restrictions. Referring to this post How to run commands by sudo and enter password by ssh .net c# i tried this code block:

using (var cmd = ssh.RunCommand("echo 'rootpassword' | sudo -u root -S fsstate"))
{
if (cmd.ExitStatus == 0)
    Console.WriteLine(cmd.Result);
else
    Console.WriteLine(cmd.Error);
}

But then i always receive this error message:

sudo: unknown user: root
sudo: unable to initialize policy plugin

Update 1: Working PuTTY commands

What i'm doing wrong here?

Eduard S
  • 1
  • 2
  • 2
    Have you tried running `sudo` and `sudo -u root` on the server in question without ssh.net? – ikkentim Feb 18 '19 at 15:41
  • @ikkentim Yes. I tried it with Putty without any issues. Just typed in 'sudo root' and 'rootpassword' and it worked. – Eduard S Feb 19 '19 at 07:57
  • But does `echo 'rootpassword' | sudo -u root -S fsstate` work in PuTTY? – Martin Prikryl Feb 19 '19 at 08:10
  • I'm guessing ssh.net's `.RunCommand` might not be able to handle pipes ( the | ). I'm not sure if this is true, you'd have to consult the manual. I'm afraid I can't help you much further than that. – ikkentim Feb 19 '19 at 08:34
  • @ikkentim `RunCommand` does not process the command in any way. If there's problem with a pipe, it's on the remote side, not on SSH.NET side. Though the error message OP is getting does not suggest any problem with "pipe". – Martin Prikryl Feb 19 '19 at 08:46
  • Ok i checked `echo 'rootpassword' | sudo -u root -S fsstate` with PuTTY. I get the same error message like described in my post. But when i perform `sudo rootsh` on SSH.NET (which works fine with PuTTY) i got following error message: **sudo: no tty present and no askpass program specified**. I dont have much experience with SSH Terminal, sorry :) – Eduard S Feb 19 '19 at 09:12
  • And if you do `sudo -u root -S`? – Martin Prikryl Feb 19 '19 at 09:22
  • @MartinPrikryl `sudo -u root -S` doesn't work both on PuTTY and SSH.NET. Igot following error message: **sudo: unknown user: root sudo: unable to initialize policy plugin**. In fact only `sudo root` works with PuTTY. – Eduard S Feb 19 '19 at 09:29
  • `sudo root` is a strange syntax. It says `sudo` to execute `root` "command" (not to switch to `root` user). Is this what you are actually doing? Then you can try `sudo root -S`. – Martin Prikryl Feb 19 '19 at 09:41
  • `sudo root -S` returns invalid option by PuTTY. I update my post with the commands i successfully performned on PuTTY. Hopefully that helps. `rootsh` is my `root` user. – Eduard S Feb 19 '19 at 10:37
  • OK, than your `sudo` is not the common Linux `sudo`. It's difficult to help you, unless you give us some reference for your version of `sudo`. – Martin Prikryl Feb 19 '19 at 10:49
  • I think i struggle with the problem described here: [link](https://stackoverflow.com/questions/21659637/how-to-fix-sudo-no-tty-present-and-no-askpass-program-specified-error) I'm gonna try fix this like that. Thank you for helping me. – Eduard S Feb 19 '19 at 12:35

0 Answers0