question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

handling sudo interactive password requests

See original GitHub issue

Any guidance on how to go about executing sudo on the remote server and dealing with interactive password prompts? Ideally I’d like to do the prompting on the client side and allow interactive authentication to work (basically shooting for how Fabric does this in python), but I’m not sure if A) I need to force tty creation during the connection (and if so, how would I do that as I don’t see any connection config option) or B) whether I can specify an “askpass” program on the remote side and somehow wire that up to the ssh2 session or C) something else entirely or D) this is sufficiently nontrivial that I should consider alternatives.

As I would mostly expect, currently running a remote sudo results in:

sudo: no tty present and no askpass program specified
Sorry, try again.
sudo: no tty present and no askpass program specified
Sorry, try again.
sudo: no tty present and no askpass program specified
Sorry, try again.
sudo: 3 incorrect password attempts

Issue Analytics

  • State:closed
  • Created 11 years ago
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
mscdexcommented, Mar 17, 2013

I’ve added the ability to allocate a pseudo-tty for exec() in f4b2aaa. Use it like so:

// use default pty settings
conn.exec('sudo uptime', { pty: true }, function(err, stream) {   });

// use specific pty settings
conn.exec('sudo uptime', { pty: { cols: 1000 } }, function(err, stream) {   });

As a result of this change, environments are now set by:

conn.exec('sudo uptime', { env: { foo: 'bar' } }, function(err, stream) {   });

You’ll also have to look for the sudo prompt. Example:

conn.exec('sudo ls /', { pty: true }, function(err, stream) {
  if (err) throw err;
  var b = '', pwsent = false;
  stream.on('data', function(data) {
    if (!pwsent) {
      b += data.toString();
      if (b.substr(-2) === ': ') {
        pwsent = true;
        stream.write('mypassword\n');
        b = '';
      }
    } else
     console.log(data.toString());
  });
  stream.on('exit', function(code, signal) {
    console.log('>> exited with ' + code + ', ' + signal);
    conn.end();
  });
});
0reactions
zhy1commented, Dec 2, 2019

very good.

Read more comments on GitHub >

github_iconTop Results From Across the Web

sudo with password in one command line? - Super User
Yes, use the -S switch which reads the password from STDIN: $echo <password> | sudo -S <command>. So for your case it would...
Read more >
Use sudo Command in Non-Interactive Mode - Baeldung
In this tutorial, we'll look at how to tell sudo to work in a non-interactive mode — for example, passing the password as...
Read more >
How to avoid being prompted for a password by sudo?
In terminal run the visudo command to edit the sudoers file: sudo visudo. and add the following line to the sudoers list username...
Read more >
Bash: automating ssh login and sudo that require interactive ...
Taking this to its logical conclusion, I would use the sshpass utility to avoid the password prompt for the initial ssh session. sshpass...
Read more >
run a sudo command, specifying the password on the same line
Run the command as a user other than the default target user (usually root). The user may be either a user name or...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found