su command

Jan 11, 2013 at 2:54 PM

Hi

how can I execute the su linux command? I've this code:

        public void sshTest()
        {
            using (var client = new SshClient("host", "myuser", "mypassword"))
            {
                client.Connect();
                var cmd.Execute("su - root");
                string errore = cmd.Error;
                cmd.Execute("rootpwd");
                result = cmd.Result;
                client.Disconnect();
               
            }
        }

after the command var cmd.Execute("su - root"); the command.Error contains "standard in must be a tty\n". Any suggestion?

thank you

Daniele

Coordinator
Jan 11, 2013 at 3:44 PM

Hi,

 

From what I found out you cannot use su command with command execution,

You will need to use Shell class which will open tty and allow this command to be executed.

Hope it helps,

Thanks,

Oleg

Jan 14, 2013 at 1:21 PM

thanks Olegkap, I roselved using a ShellStream, here is the code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Renci.SshNet;
using System.IO;
using System.Text.RegularExpressions;

namespace DBA95
{
    class ServerUtil
    {
        public void sshTest(Server server)
        {
            try
            {
                using (var client = new SshClient(server.Host, server.User, server.Password))
                {
                    ShellStream shells = client.CreateShellStream("test", 80, 24, 800, 600, 1024);
                    Regex regex = new Regex(@"\[.*@.*\][\$|\#]");

                    shells.WriteLine("su - root");
                    String outp = "";
                    var match = regex.Match(outp);
                    while (shells.DataAvailable || !match.Success || outp == "")
                    {
                        outp += shells.Read();
                        match = regex.Match(outp);
                    }
                    shells.WriteLine("rootpwd");
                    outp = "";
                    match = regex.Match(outp);
                    while (shells.DataAvailable || !match.Success || outp == "")
                    {
                        outp += shells.Read();
                        match = regex.Match(outp);
                    }
                    shells.WriteLine("pwd");
                    outp = "";
                    match = regex.Match(outp);
                    while (shells.DataAvailable || !match.Success || outp == "")
                    {
                        outp += shells.Read();
                        match = regex.Match(outp);
                    }
                    shells.WriteLine("pwd");
                    outp = "";
                    match = regex.Match(outp);
                    while (shells.DataAvailable || !match.Success || outp == "")
                    {
                        outp += shells.Read();
                        match = regex.Match(outp);
                    }
                    client.Disconnect();
                }
            }
            catch (Exception e)
            {
            }
        }
    }
}

Mar 5, 2013 at 10:09 PM
Edited Mar 5, 2013 at 10:11 PM
hi vaccaridaniele,

why do you use this regular expressions? Look at my code at the end of this posting. It is just an adapted copy of yours. ;) But it runs endless the while loop.
            var connectionInfo = new ConnectionInfo("server", 22, "user",
                new PrivateKeyAuthenticationMethod("user",
                     new PrivateKeyFile(File.OpenRead(@"C:\Path\to\file\file.key"), "")));

            using (var client = new Renci.SshNet.SshClient(connectionInfo))
          {
              try
              {
                  client.Connect();
                  ShellStream shells = client.CreateShellStream("test", 80, 24, 800, 600, 1024);
                  Regex regex = new Regex(@"\[.*@.*\][\$|\#]");

                  shells.WriteLine("su");
                  String outp = "";
                  var match = regex.Match(outp);

                  while ((shells.DataAvailable || !match.Success || outp == ""))
                  {

                      outp += shells.Read();
                      Console.WriteLine(outp);
                      match = regex.Match(outp); 
                      Console.WriteLine(shells.DataAvailable);
                      Console.WriteLine(match.Success);
                  }
                 shells.WriteLine("password");
                  outp = "";
                  match = regex.Match(outp);
                  while (shells.DataAvailable || !match.Success || outp == "")
                  {
                      outp += shells.Read();
                      Console.WriteLine(outp);
                      match = regex.Match(outp);
                  }
                  client.Disconnect();
              }
              catch (SshAuthenticationException ex)
              {
                  Console.WriteLine(ex.Message);
              }
Jan 30, 2015 at 6:52 PM
Edited Jan 30, 2015 at 6:54 PM
Hi I can't get it to work either .
can you please help below is my code. Thanks
 public void ServerConnexion()
        {
            try
            {
                KeyboardInteractiveAuthenticationMethod kauth = new KeyboardInteractiveAuthenticationMethod(this.username);
                PasswordAuthenticationMethod pauth = new PasswordAuthenticationMethod(this.username, this.password);

                kauth.AuthenticationPrompt += new EventHandler<AuthenticationPromptEventArgs>(HandleKeyEvent);

                this.Server = new SshClient(new ConnectionInfo(this.servername, this.port, this.username, pauth, kauth));

                this.Server.Connect();

                ShellStream shell = this.Server.CreateShellStream("xterm", 80, 24, 800, 600, 1024);

                System.IO.StreamReader reader = new StreamReader(shell);
                System.IO.StreamWriter writer = new StreamWriter(shell);

                writer.AutoFlush = true;

                //writer.Write("su - " + suUsr + "\n");
                shell.Write("su - " + this.suUsr + "\n");

                while (true)
                {
                    var output = reader.ReadLine();

                    if (output.EndsWith("Password: "))
                    {
                        break;
                    }
                }

                //writer.Write(suPass + "\n");
                shell.Write(this.suPass + "\n");

                KeyboardInteractiveAuthenticationMethod kAuth = new KeyboardInteractiveAuthenticationMethod(this.suUsr);
                PasswordAuthenticationMethod pAuth = new PasswordAuthenticationMethod(this.suUsr, this.suPass);

                kAuth.AuthenticationPrompt += new EventHandler<AuthenticationPromptEventArgs>(HandleKeyEvent);
                

            }
            catch (Exception)
            {
                
                throw;
            }

        }