Help me to execute my code synchronously
Hi guys , im trying to get some lines from my mikrotik router using simple-ssh module. This is code:
const SSH = require('simple-ssh');
module.exports = function(app) {
app.get('/get_ping_stats', function(req, res) {
console.log("starting");
var ssh = new SSH({
host: '192.168.89.1',
user: 'admin',
pass: ''
});
ssh.exec('system script run "bandwith result"', {
out: function (stdout) { console.log(stdout); },
err: function (stderr) { console.log(stderr); },
exit: function (code) { console.log(code); }
}).start();
console.log("aftera all")
});
}
I need somehow to wait for the ssh.exec call to finish before executing console.log("after all") statement. How can I achieve that ? Regards. Leandro.
That’s almost certainly not going to work – simple-ssh uses ssh2 which doesn’t provide a synchronous interface (and can’t do that, really, because network connections in Node.js are unconditionally asynchronous by design.)
Can you explain why it is that you need the exec call to finish first?
hi ,
Can you explain why it is that you need the exec call to finish first?
Thank for your response. Im executing a 60 seconds test on remote server. I need this test to finish so , I can parse the result and return it on api call. Is it clear ? Is there another way to achieve this ? Leandro.
Hi @leostereo - I might be wrong but, you can parse the result and return it in the out function of the exec call.
out: function (stdout) { console.log(stdout); },
Incase you are looking to wait until SSH is ready, you can do this:
ssh.on('ready', function(err) { console.log('SSH is ready!') });
It seems there has been no activity on this issue for a while, and it is being closed in 30 days. If you believe this issue should remain open, please leave a comment. If you need further assistance or have questions, you can also search for similar issues on Stack Overflow. Make sure to look at the README file for the most updated links.
It seems there has been no activity on this issue for a while, and it is being closed. If you believe this issue should remain open, please leave a comment. If you need further assistance or have questions, you can also search for similar issues on Stack Overflow. Make sure to look at the README file for the most updated links.