question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Shell multiple commands

See original GitHub issue

Hi

I cannot get the output from the remote console when I run these commands within shell session. Is there any way to see errors so they can be handled.

Thanks Simon

var ssh2 = require('ssh2');
var conn = new ssh2();

var commands = [
   'cd /home/user/project',
   'sudo stop service',
   'git pull',
   'npm install',
   'sudo start service',
   'curl https://localhost -k'
  ];
var command = "";
var pwSent = false;
var password = process.argv[4];

  conn.on('ready', function () {
       console.log('Connection :: ready');
         conn.shell(function(err, stream) {
            if (err) throw err;
              stream.on('close', function () {
                console.log('Stream :: close');
                conn.end();
            }).on('data', function (data) {
               if (command.indexOf('sudo') !== - 1 && !pwSent) {
                   if (data.toString().indexOf(':') >= data.toString().length - 2) {
                       pwSent = true;
                       stream.write(password + '\n');
                   }
               } else {
                   var dataLength = data.toString().length;
                   if (dataLength > 2 && (data.toString().indexOf('$') >= dataLength -2)) {
                       if (commands.length > 0) {
                           command = commands.shift();
                           stream.write(command + '\n');
                       } else {
                           stream.end('exit\n');
                       }
                       console.log('STDOUT: ' + data);
                   }
               }
           }).stderr.on('data', function(data) {
                   console.log('STDERR: ' + data);
           });
           command = commands.shift();
           stream.write(command + '\n');
       });
    }).connect({
    host: hostname,
    port: 22,
    username: 'user',
    privateKey: fs.readFileSync('path/to/ssh/key')
});

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
cmp-202commented, Aug 30, 2014

I have taken the code we used and turned it into a class wrapper for ssh2 providing the multi command functionality with sudo support and command response testing. The npm package is called ssh2shell and a working example is in the readme. Hope this helps.

0reactions
simon-p-rcommented, Aug 26, 2014

I used ‘stream.write’ to write commands out seperated with ‘;’ to execute together.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Running multiple commands in one line in shell - Stack Overflow
Another option is typing Ctrl+V Ctrl+J at the end of each command.
Read more >
How to Run Multiple Commands in Linux at Once - MakeUseOf
1. Using the Semicolon (;) Operator ... Segmenting a chain of commands with the semicolon is the most common practice when you want...
Read more >
Running Multiple Commands in the Background - Baeldung
Learn how to run multiple commands in the background using the Linux shell.
Read more >
How to Run Two or More Terminal Commands at Once in Linux
The semicolon (;) operator allows you to execute multiple commands in succession, regardless of whether each previous command succeeds. For ...
Read more >
4.10. Using Multiple Commands
4.10. Using Multiple Commands ... Linux allows you to enter multiple commands at one time. The only requirement is that you separate the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found