i have 2 async functions defined in program; 1 of them pings given ip address(using net-ping module) , 'returns' (through callback) whether ping successful, , other creates ssh connection given ip address (using ssh2 module) , 'returns' whether connection successful or not.
my problem arises when try read in ip data data.txt
file using readline
functionality. use readline
module read in each line file, use callbacks call ping , ssh in blocking fashion, , place these return values array use later. i've been able verify functions executed in order expect, , return values returned.. however, when program reaches end of file - instead of terminating, reads file again, , again, , again.
the ssh function defined as:
function ssh(ipadress, callback) { connection.on('ready', function(err) { if(err) { throw err; returnvalue = "errorwithreadyconnection"; callback(null, returnvalue); } //runs uptime command once have ssh'd machine connection.exec('uptime', function(err, stream) { if(err) { throw err; returnvalue = "errorrunningcommandfromssh"; callback(null, returnvalue); } else { stream.on('close', function(code, signal) { //code holds response running 'uptime' console.log("stream on close. code : " + code + ". signal: " + signal); connection.end(); returnvalue = "success"; callback(null, returnvalue); }).on('data', function(data) { console.log("stdout: " + data); }).stderr.on('data', function(data) { console.log("stderr: " + data); }); } }); //parameters ssh connection }).connect({ host: ip, port: 22, username: username, privatekey: privatekey, passphrase: passphrase }); //connect //handle connection errors done whilst creating connection connection.on('error', function(err) { if(err) { returnstring = "errorwaitingforhandshake"; callback(null, returnstring); } }); //end connect }
the ping function defined as:
function pingfunc(ip, callback) { var returnstring; //create ping session, passing ip of machine ping sessions.pinghost(ip, function(error, target) { //if error, examine type of error has occured if(error) { if(error instanceof ping.requesttimedouterror) { returnstring = "requesttimedout"; } //else, different error - output error string. else { returnstring = "error"; } } //end error handling //else, ping successful else { returnstring = "alive"; } callback(null, returnstring); }); //return returnstring; }
and code call functions is:
var allmachines = new array(); var linereader = require('readline').createinterface({ input: require('fs').createreadstream('data.txt'); }); linereader.on('line', function(line) { pingfunc(line, function(err, pingresult) { ssh(line, function(err, sshresult) { var machinetemp = [{'ping': pingresult, 'ssh': sshresult }]; allmachines = allmachines.concat(machinetemp); }) }) }
i plan on later using allmachines
array create json file, infinite loop halting potential progress. i've tried move connection.on('error', ...)
in ssh function think causing infinite loop, has proved fruitless.
any appreciated - thanks!
p.s if knows how able register when readlines
has finished, , allmachines
array has been filled necessary data grateful! i've tried use readline.on('close', ...)
, gets called before ssh , ping finish executing, no use me! again
Comments
Post a Comment