Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#10 Issue -- Ssh values Iteration from the array #11

Closed
kgrvamsi opened this issue Nov 21, 2014 · 2 comments
Closed

#10 Issue -- Ssh values Iteration from the array #11

kgrvamsi opened this issue Nov 21, 2014 · 2 comments

Comments

@kgrvamsi
Copy link

Hi Mcluck,

Sorry to post this issue again and in the previous issue there wer some errors in the code which i have share .here is the exact code which i'm trying to do

Private.find().exec(function(err,succ){

    if(err) throw err.message;

    console.log(succ);

    for (var i = 0; i < succ.length; i++) {

        /*sails.log(succ.length, m);
         var host = succ[m].hostname,
         user = succ[m].username;
         sails.log(host, user);*/

        var host = succ[i].hostname,
            user = succ[i].username;

        sails.log(host, user);

        var ssh = new SSH({
            host: succ[i].hostname,
            port: succ[i].hostport,
            user: succ[i].hostuser,
            pass: succ[i].hostpassword
        });
        console.log(ssh.host);

        ssh.exec('nproc', {
            out: function (cpucore) {
                console.log(cpucore)
                ssh.exec('cat /proc/meminfo | grep MemTotal | cut -d":" -f2| cut -d"k" -f1|tr -d " "', {
                    out: function (memtotal) {
                        console.log(memtotal);
                        ssh.exec('cat /proc/meminfo | grep MemFree | cut -d":" -f2| cut -d"k" -f1|tr -d " "', {
                            out: function (memfree) {
                                console.log(memfree);
                                ssh.exec('uname -i', {
                                    out: function (arc) {
                                        console.log(arc);

                                        Resource.create({username: user, hostip: host, cpu: cpucore, totalram: memtotal, leftram: memfree, arctype: arc}).exec(function (err, succc) {
                                            if (err) {

                                            } else {
                                                sails.log("Data Created for ip", host);
                                            }
                                        })
                                    }
                                })
                            }
                        })
                    }
                })
            }
        })



}
})
@MCluck90
Copy link
Owner

Again, this is a problem with your JavaScript, not with simple-ssh. JavaScript doesn't have block scope like other languages, it has function scope so you're replacing the value for ssh as you move through the loop. Consider the following:

var ssh = null;
for (var i = 0; i < succ.length; i++) {
    ssh = new SSH({
        host: succ[i].hostname,
        port: succ[i].hostport,
        user: succ[i].hostuser,
        pass: succ[i].hostpassword
    });
    ssh.exec('nproc', {
        out: function(cpucore) {
            // This is asynchronous so `ssh` has been re-assigned i.e. it's the last instance of ssh
            ssh.exec(...);
        }
    }).start();
}

To get around this, you should be creating these in a separate function (I would also suggest chaining .exec calls instead of nesting them):

function createConnection(config) {
    var ssh = new SSH({
            host: config.hostname,
            port: config.hostport,
            user: config.hostuser,
            pass: config.hostpassword
        });
    ssh.exec('nproc', {
        out: function(cpucore) {
           console.log(cpucore); 
        }
    }).exec('cat /proc/meminfo | grep MemTotal | cut -d":" -f2| cut -d"k" -f1|tr -d " "', {
        ...
    }).exec({
        ...
    }).start();
}

for (var i = 0; i < succ.length; i++) {
    createConnection(succ[i]);
}

@kgrvamsi
Copy link
Author

Thanks Mcluck ....its working..!!!!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants