From nodejs I have been trying to execute linux commands on remote server and get the output in stream for further processing. For connecting to remote linux server , I have all necessary details like serverip, username and password. I searched a lot on internet and found that this can be achieved by ssh.
Can Nodejs ui page run shell cmd to remote machine and run script
But this answer a bit confusing and I didn't get how to use password in connection.
Pointer to any working example would be great help.
I solved the problem myself. There is one npm package (ssh-exec) available for ssh command execution. Below is the code I used.
var exec = require('ssh-exec')
var v_host = 'XX.XX.XX.XXX'
exec('ls -lh', {
user: 'root',
host: 'XX.XX.XX.XXX',
password: 'password'
}).pipe(process.stdout , function (err, data) {
if ( err ) { console.log(v_host); console.log(err); }
console.log(data)
})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With