我正在尝试使用节点SSH2包。这是我的代码
var Client = require('ssh2').Client;
var password = 'xxxxxx'
var conn = new Client();
conn.on('ready', function() {
console.log('Client :: ready');
conn.exec('sudo ls -la', { pty: true }, function(err, stream) {
if (err) throw err;
stream.on('close', function(code, signal) {
console.log('Stream :: close :: code: ' + code + ', signal: ' + signal);
conn.end();
}).on('data', function(data) {
if (data.indexOf(':') >= data.length - 2) {
stream.write(password + '\n');
}
else {
console.log('STDOUT: ' + data);
}
}).stderr.on('data', function(data) {
console.log('STDERR: ' + data);
});
});
}).connect({
host: 'xxx.xxx.xxx.xxx',
port: 22,
username: 'user',
password: 'xxxxx',
});
// example output:
// Client :: ready
// STDOUT: 17:41:15 up 22 days, 18:09, 1 user, load average: 0.00, 0.01, 0.05
//
// Stream :: exit :: code: 0, signal: undefined
// Stream :: close
当我运行代码时,它会提示输入密码,如下所示:
Client :: ready
STDOUT: [sudo] password for venafi:
如何将密码写入密码提示?
答案 0 :(得分:0)
好的,所以我解决了,并更新了我的示例,以帮助其他人遇到此问题。这是我需要的代码。
if (data.indexOf(':') >= data.length - 2) {
stream.write(password + '\n');
}
else {
console.log('STDOUT: ' + data);
}