简体   繁体   中英

Unable to switch user to root using Node.js ssh2 module

Trying to SSH to a server and able to. However, when I try to run sudo command it prompts me to enter the password of the userid mentioned. How do I prevent any keyboard interruption and hardcode the password so it doesn't prompt for password.

server.js

 const { Client } = require('ssh2');
    const conn = new Client();
    conn.on('ready', () => {
      console.log('Client :: ready');
      conn.exec('sudo ls -lrt',{ stdin: 'password\n', pty: true }, (err, stream) => {
        if (err) throw err;
        stream.on('close', (code, signal) => {
          console.log('Stream :: close :: code: ' + code + ', signal: ' + signal);
          conn.end();
        }).on('data', (data) => {
          console.log('STDOUT: ' + data);
        }).stderr.on('data', (data) => {
          console.log('STDERR: ' + data);
        });
      });
    }).connect({
      host: 'hostname',
      port: 22,
      username: 'user1',
      password: 'password'
    });

The prompt I get:

STDOUT: [sudo] password for user1: 
STDOUT: 
sudo: timed out reading password

The below code works for me.

const Client = require('ssh2').Client;
const conn = new Client();
const encode = 'utf8';
const connection = {
  host: 'hostname.foo.com',
  username: 'iamgroot',
  password: 'root@1234'
}
conn.on('ready', function() {
  // Avoid the use of console.log due to it adds a new line at the end of
  // the message
  process.stdout.write('Connection has been established');
  let password = 'root@1234';
  let command = '';
  let pwSent = false;
  let su = false;
  let commands = [
    `ls -lrt`,
    `sudo su - root`,
    `ls -lrt`
  ];
  conn.shell((err, stream) => {
    if (err) {
      console.log(err);
    }
    stream.on('exit', function (code) {
      process.stdout.write('Connection closed');
      conn.end();
    });
    stream.on('data', function(data) {
      process.stdout.write(data.toString(encode));
      // Handles sudo su - root password prompt
      if (command.indexOf('sudo su - root') !== -1 && !pwSent) {
         if (command.indexOf('sudo su - root') > -1) {
            su = true;
         }
         if (data.indexOf(':') >= data.length - 2) {
            pwSent = true;
            stream.write(password + '\n');
         }
      } else {
        // Detect the right condition to send the next command
        let dataLength = data.length > 2;
        let commonCommand = data.indexOf('$') >= data.length - 2;
        let suCommand = data.indexOf('#') >= data.length - 2;
        console.log(dataLength);
        console.log(commonCommand);
        console.log(suCommand);
        if (dataLength && (commonCommand || suCommand )) {
          if (commands.length > 0) {
            command = commands.shift();
            stream.write(command + '\n');
          } else {
            // su requires two exit commands to close the session
            if (su) {
               su = false;
               stream.write('exit\n');
            } else {
               stream.end('exit\n');
            }
          }
        }
      }
    });
    // First command
    command = commands.shift();
    stream.write(command + '\n');
  });
}).connect(connection);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM