help icon indicating copy to clipboard operation
help copied to clipboard

Help me to execute my code synchronously

Open leostereo opened this issue 5 years ago • 4 comments

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.

leostereo avatar Nov 04 '20 17:11 leostereo

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?

addaleax avatar Nov 04 '20 17:11 addaleax

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.

leostereo avatar Nov 05 '20 02:11 leostereo

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

marsonya avatar Mar 19 '21 18:03 marsonya

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.

github-actions[bot] avatar May 11 '24 01:05 github-actions[bot]

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.

github-actions[bot] avatar Jun 10 '24 01:06 github-actions[bot]