Shell multiple commands
See original GitHub issueHi
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:
- Created 9 years ago
- Comments:6 (1 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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.
I used ‘stream.write’ to write commands out seperated with ‘;’ to execute together.