ip-poll von nmap auf ping geändert

This commit is contained in:
Lucas Pleß 2022-09-04 15:11:02 +02:00
parent ec191271b0
commit 4a5cfe55ac
1 changed files with 20 additions and 11 deletions

View File

@ -3,23 +3,32 @@ var EventEmitter = require('events').EventEmitter;
var exec = require('child_process').exec; var exec = require('child_process').exec;
var IpPoll = function(switchaddr, hostsaddr) { var IpPoll = function(target) {
var self = this; var self = this;
var regexp = /\(([0-9]+) hosts* up\)/; var regexp = /([0-9]+)% packet loss/m;
var nmap = "nmap -n -sP "; var command = "LANG=C ping -c 2";
this.pollState = function() { this.pollState = function() {
exec(nmap + switchaddr, function (error, stdout, stderr) { try {
if(error == null) { exec(command + " " + target, function (error, stdout, stderr) {
// console.log(stdout);
var matches = regexp.exec(stdout); var matches = regexp.exec(stdout);
if(matches != null && matches.length === 2) { if (matches != null) {
self.emit('doneState', matches[1] === "1");
// 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");
}); }
}; };
}; };