2013-10-02 12:04:01 +00:00
|
|
|
var util = require('util');
|
|
|
|
var EventEmitter = require('events').EventEmitter;
|
2013-10-20 22:04:58 +00:00
|
|
|
var exec = require('child_process').exec;
|
2016-01-08 16:47:28 +00:00
|
|
|
|
2013-10-20 22:04:58 +00:00
|
|
|
|
2022-09-04 13:11:02 +00:00
|
|
|
var IpPoll = function(target) {
|
2013-11-26 20:11:05 +00:00
|
|
|
var self = this;
|
2013-10-02 12:04:01 +00:00
|
|
|
|
2022-09-04 13:11:02 +00:00
|
|
|
var regexp = /([0-9]+)% packet loss/m;
|
|
|
|
var command = "LANG=C ping -c 2";
|
2013-10-20 22:04:58 +00:00
|
|
|
|
2013-11-26 20:11:05 +00:00
|
|
|
this.pollState = function() {
|
2022-09-04 13:11:02 +00:00
|
|
|
try {
|
|
|
|
exec(command + " " + target, function (error, stdout, stderr) {
|
|
|
|
// console.log(stdout);
|
|
|
|
|
2013-11-26 20:11:05 +00:00
|
|
|
var matches = regexp.exec(stdout);
|
2022-09-04 13:11:02 +00:00
|
|
|
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");
|
|
|
|
}
|
2013-11-26 20:11:05 +00:00
|
|
|
}
|
2022-09-04 13:11:02 +00:00
|
|
|
});
|
|
|
|
} catch(err) {
|
|
|
|
self.emit('doneState', "unknown");
|
|
|
|
}
|
2013-11-26 20:11:05 +00:00
|
|
|
};
|
2013-10-21 23:40:57 +00:00
|
|
|
|
2013-10-20 22:04:58 +00:00
|
|
|
};
|
2013-10-02 12:04:01 +00:00
|
|
|
|
|
|
|
util.inherits(IpPoll, EventEmitter);
|
|
|
|
module.exports = IpPoll;
|