diff --git a/ip-poll.js b/ip-poll.js index cf051b5..7a43bd4 100644 --- a/ip-poll.js +++ b/ip-poll.js @@ -3,23 +3,32 @@ var EventEmitter = require('events').EventEmitter; var exec = require('child_process').exec; -var IpPoll = function(switchaddr, hostsaddr) { +var IpPoll = function(target) { var self = this; - var regexp = /\(([0-9]+) hosts* up\)/; - var nmap = "nmap -n -sP "; + var regexp = /([0-9]+)% packet loss/m; + var command = "LANG=C ping -c 2"; this.pollState = function() { - exec(nmap + switchaddr, function (error, stdout, stderr) { - if(error == null) { + try { + exec(command + " " + target, function (error, stdout, stderr) { + // console.log(stdout); + var matches = regexp.exec(stdout); - if(matches != null && matches.length === 2) { - self.emit('doneState', matches[1] === "1"); + if (matches != null) { + + // console.log("matches " + JSON.stringify(matches)); + // console.log("length: " + matches.length); + // console.log("matches 1: " + matches[1]); + + if (matches.length === 2) { + self.emit('doneState', matches[1] === "0"); + } } - } else { - self.emit('doneState', "unknown"); - } - }); + }); + } catch(err) { + self.emit('doneState', "unknown"); + } }; };