I am trying to log in as root on a remote Linux machine using mscdex / ssh2 , the steps I am trying to achieve are:
- connect via ssh to a remote device
- run the command as root user
but I can’t in the second part, I can’t enter the password correctly, here is my code.
conn.on('ready', function() { conn.exec('sudo -s ls /', { pty: true }, (err, stream) => { if (err) { res.send(err); } stream.on('exit', (code, signal) => { console.log(`Stream :: close :: code: ${code}, signal: ${signal}`); }); stream.on('data', data => { // here it where supposedly the password should be given stream.write('r00tpa$$word' + '\n'); console.log(data); }); }); }).connect({ host: '192.168.100.100', username: 'fakeAdmin', password: 'fakePassword' });
I already have the pty parameter set to true, but I get error messages on promt.
Update:
here is my new code snippet:
const Client = require('ssh2').Client; const conn = new Client(); const encode = 'utf8'; conn.on('ready', () => { conn.shell(false, { pty: true }, (err, stream) => { if (err) { console.log(err) } stream.on('data', (data) => { process.stdout.write(data.toString(encode)); }); stream.write('ls -a\n'); stream.write('uptime\n'); stream.write('su\n'); // here nothing seems to happen stream.write('rootPassword\n'); // here also stream.write('cd /tmp && ls\n'); }); }) .connect({ host: "192.168.100.100", username: "username", password: "usernamePassword" });
I managed to execute several commands in a purer form, I even raised the problem on the github page of the library . The “su” shell command is losing interaction , but now that this is happening, it’s strange, I can run as many commands as I do, but when I run the “su” command, nothing happens, does anyone come into this earlier ?, or what am I doing wrong? Sorry if I can’t explain correctly.
Sincerely.