Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

possibility to run sudo commands #90

Closed
solancer opened this issue Aug 10, 2017 · 10 comments
Closed

possibility to run sudo commands #90

solancer opened this issue Aug 10, 2017 · 10 comments
Labels

Comments

@solancer
Copy link

Is it possible to run sudo commands? sending in password to the pty?

@steelbrain
Copy link
Owner

Hi @solancer

You can either use a pty to send the password or you can provide the password in stdin to exec/execCommand for example

ssh.exec('sudo', ['ls /'], { stdin: 'mypassword\n' })

\n is important after the password

@solancer
Copy link
Author

That worked like a charm, thanks for taking time to develop such an awesome lib. If you ever come down to Bangalore beer's on me mate ;)

@steelbrain
Copy link
Owner

Thanks for the offer, glad you like it :)

@v-vostretsov
Copy link

Hi @steelbrain
I've tried your suggestion, however, I got "sudo: no tty present and no askpass program specified" error
What have I done wrong?

@steelbrain
Copy link
Owner

@SlavikVostretsov You have to pass pty: true in ExecOptions.options so it looks something like

ssh.exec('sudo', ['ls /'], { stdin: 'mypassword\n', pty: true })

@v-vostretsov
Copy link

v-vostretsov commented Nov 3, 2017

@steelbrain Still got the same error :(
Here is my code
ssh.exec("sudo", ["mv file /home/path/"], { pty: true, stdin: "mypass\n" } )

@steelbrain
Copy link
Owner

@SlavikVostretsov In that case, it's an ssh server config issue.

Try googling with that error message to see if that helps

@ewnd9
Copy link

ewnd9 commented Mar 14, 2018

There's a typo, pty should in the options property

ssh.exec("sudo", ["mv file /home/path/"], { options: { pty: true }, stdin: "mypass\n" } )

@syedhabib53
Copy link

As of now for version 10.0.2, the above solution is not working. Since changes in options keyword as execOptions. Below is working for me.

ssh.exec("sudo", ["mv file /home/path/"], { execOptions: { pty: true }, stdin: "mypass\n" } )

@larrybg
Copy link

larrybg commented Mar 28, 2023

I have tried this with execOptions and stdin as the password and still getting back the prompt to enter admin's password.

    ssh.exec("sudo", ["useradd -m -s /bin/rbash userID"],
        {
            execOptions: { pty: true }, stdin: "useradmin_password\n",
            onStdout(chunk) {
                console.log(`STDOUT: ${chunk.toString('utf8')}`);
            },
            onStderr(chunk) {
                console.log(`STDERR: ${chunk.toString('utf8')}`);
                ssh.getConnection().end();
            }
        });

The response is:
STDOUT: [sudo] password for useradmin:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

6 participants