0

I've a shell script that, when started, asks for credentials.

I would like to start my script with Node.js using exec() and send the credentials when asked.

Currently my code is this:

var sys = require('sys')
var exec = require('child_process').exec;
function puts(error, stdout, stderr) { sys.puts(stdout) }
exec("myscript", puts);

Is it possible?

n.b. I can't pass the credentials as arguments of the script, it doesn't support them.

Fez Vrasta
  • 14,110
  • 21
  • 98
  • 160
  • What kind of credentials? Credentials for OS or credentials for some service? Is it because you don't have rights for running the script as current user and you need elevation? What kind of OS are you using? – Mihai Feb 10 '14 at 13:32
  • Are credentials specific for the script (actually it's "git"). It should work on Window/OSx/Linux (but it doesn't matter because git has its own shell). – Fez Vrasta Feb 10 '14 at 13:34
  • One idea to overcome this issue would be to use a ssh key with your git account. This way you wouldn't need the credentials at all. – Mihai Feb 10 '14 at 13:54
  • Unfortunately I can't proceed in this way, this is why I've not talked about GIT in my question, to prevent this kind of reply. – Fez Vrasta Feb 10 '14 at 13:59

1 Answers1

1

You should write to the stdin of the process. This is an example with exec:

var exec = require('child_process').spawn;
var child = spawn('script');

child.stdin.setEncoding('utf8');

child.stdout.on('data', function (data) {
  if (data.toString() === 'enter the password:'){
    child.stdin.write('pass');
    return child.stdin.end();
  }
  console.log('got some other data:');
  console.log(data.toString())
});

Similar to this question.

Community
  • 1
  • 1
José F. Romaniello
  • 13,866
  • 3
  • 36
  • 38