+1 vote
by (220 points)

I need send password sshChannel.send() password to server. I want to write sudo command in my project. How can i use this method or another method for my project.

1 Answer

0 votes
by (144k points)
selected by
 
Best answer

In general, it's recommended to use the high-level Scripting object for these kind of tasks - see the sample code for this feature for details.

When using the low-level SshChannel object to run the sudo command, you have to request a pseudo-terminal by calling channel.RequestPseudoTerminal() method before calling channel.RequestExec() or channel.RequestSession().

The sudo command is generally supposed to only be run by users connected through a terminal and will complain if RequestPseudoTerminal has not been called.

by (220 points)
Thank you for speed response and your answer. Maybe another people need code that and i want post my code.

    string pass = "YOUR SUDO PASSWORD" +"\n\r" ;
            byte[] passBuff = Encoding.ASCII.GetBytes(pass);

            SshChannel channel = ssh.Session.OpenChannel(SshChannelType.Session, passBuff.Length);
            channel.RequestPseudoTerminal();
           
            channel.RequestExec("sudo ls");
            channel.Send(passBuff, 0, passBuff.Length);
            
            // process the command response
            var response = new StringBuilder();
            var buffer = new byte[4096];
            while (true)
            {
                int n = channel.Receive(buffer, 0, buffer.Length);
    
                if (n == 0)
                {
                    channel.Close();
                    break;
                }

                response.Append(Encoding.Default.GetString(buffer, 0, n));
            }
            Console.WriteLine("info: {0}", response);
by (144k points)
edited by
Hello, thanks for the sample code! I'm adding an equivalent code using the high-level Scripting API:

    string password = "YOUR SUDO PASSWORD";

    var scripting = ssh.StartScripting("sudo ls");
    scripting.Send(password + "\n");

    string response = scripting.ReadUntil(ScriptEvent.Closed);
    Console.WriteLine(response);

However, please be aware that both this code and mertercan83's code share two drawbacks:
1. Sudo does not always request password (for users who used public key authentication, for example), but the code above simply sends the password in any case.
2. The response will contain Sudo's output as well.

The following code which waits for Sudo's password prompt before actually sending the password may be more suitable in many scenarios:

    var scripting = ssh.StartScripting("sudo ls");

    var match = scripting.WaitFor(ScriptEvent.FromRegex(".*password for .*: "));
    if (match.Success)
    {
        scripting.Send(password + "\n");
        scripting.ReadLine();
    }
    else
    {
        throw new ApplicationException("Password prompt not encountered.");
    }

    string response = scripting.ReadUntil(ScriptEvent.Closed);
    Console.WriteLine(response);
...