Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

.shell command "su" loses interaction #661

Closed
FrancoAguilera opened this issue Jan 19, 2018 · 9 comments
Closed

.shell command "su" loses interaction #661

FrancoAguilera opened this issue Jan 19, 2018 · 9 comments
Labels

Comments

@FrancoAguilera
Copy link

FrancoAguilera commented Jan 19, 2018

I could login as root on a ssh connection like this :

    conn.on('ready', () => {
      conn.exec('su', { pty: true }, (err, stream) => { // root command
        if (err) throw err;
        
        stream.write('rootPassword\n'); // enter root password

        stream.on('close', (code, signal) => {
          console.log('Stream :: close :: code: ' + code + ', signal: ' + signal);
          conn.end();
        }).on('data', (data) => {
          console.log('STDOUT: ' + data.toString('utf8')); // STDOUT: bash-4.1#  (login successful)
        }).stderr.on('data', (data) => {
          console.log('STDERR: ' + data);
        });
      });
    }).connect({
      host: '192.168.100.100',
      username: 'admin',
      password: 'password'
    });

but... how should I perform a second command (e.g. :$ touch /media/test_file.txt') once I'm on my root session, after the login is successful ?.

@mscdex
Copy link
Owner

mscdex commented Jan 20, 2018

Why not prefix your commands with 'sudo'?

@FrancoAguilera
Copy link
Author

I been asking for an assignment in which I have to login as a root user instead of running commands with sudo due to that some commands it might take long to perform, but despite that, is this the correct path to perform what I'm aiming for ?, I mean... it is possible to execute command in a procedural way
[this is just a silly example just to show what I'm looking for), e.g.

  1. login as root
  2. create a file (let's say 'touch /media/empty_test_file.txt')
  3. compress that file
  4. delete the uncompressed one
  5. exit root session

@FrancoAguilera
Copy link
Author

FrancoAguilera commented Jan 21, 2018

Update

I've managed to perform the "several commands" part in a more clean and readable way I think, (I know there are streams errors and end missing, but this is only for practical purposes).
As you can see on the commands array, there is a "su" command, but when that command it's fired in the stream nothing seems to happen, it's like if there where nothing there, do you know why the "su" command don't asks for his password and activates a "session" lets say ?.

    const Client = require('ssh2').Client;
    const conn = new Client();

    const encode = 'utf8';
    const commands = [
      'ls -a',
      'su',
      'rootP455word',
      'uptime',
      'cd /tmp && ls'
    ];

    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));
        });

        commands.forEach((cmd) => {
          stream.write(`${cmd}\n`);
        });
      });
    })
    .connect({
      host: "192.168.100.100",
      username: "username",
      password: "usernamePassword"
    });

@FrancoAguilera FrancoAguilera changed the title .exec seccond command after root login .shell command "su" loses interaction Jan 22, 2018
@andrewgremlich
Copy link

Seems to work fairly well what you did! I came across a similar issue where the .exec command wouldn't su me in. I used your idea. Thanks!

@FrancoAguilera
Copy link
Author

thanks to you @andrewgremlich and your reply, here also I made a workaround to perform the root change without loosing the interaction and executing some commands. stack overflow

@andrewgremlich
Copy link

andrewgremlich commented Feb 13, 2018

@FrancoAguilera thats a good post you shared! I didn't need to use it because my su command in this case isn't linked with a password, so all I needed was to just execute the su command.

@andrewgremlich
Copy link

@FrancoAguilera per circumstance, do you know how to close the session?

@mscdex
Copy link
Owner

mscdex commented Feb 14, 2018

You could .write('exit\n') or .end() on the interactive session stream object.

@andrewgremlich
Copy link

@mscdex thanks dude! I tried doing that, but it wouldn't work where I put it.

So what I ended up doing is calling .end() inside the callback of the .on('data') and seeing of the returned data (with trim) is equal to the last command of the array of commands.

It worked then! Here is what I got...

conn.on('ready', () => {                                                     
    conn.shell(false, { pty: true }, (err, stream) => {                      
                                                                             
        if (err) throw err                                                   
                                                                             
        stream                                                               
            .on('data', data => {                                            
                global.logger.write(data.toString('utf8'))                   
                                                                             
                if (data.toString('utf8').trim() === 'Move Files complete') {
                    console.log("OVER!")                                     
                    stream.end('exit \n')                                    
                }                                                            
            })                                                               
            .on('close', () => {                                             
                global.logger.write('Connetion is closing')                  
                conn.end()                                                   
            })                                                               
                                                                             
        commands.forEach(cmd => {                                            
            stream.write(`${cmd}\n`)                                         
            iter++                                                           
        })                                                                   
    })                                                                       
})                                                                           

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants