domotz-remote-pawn
Version:
Domotz Agent
81 lines (72 loc) • 2.43 kB
JavaScript
/** This file is part of Domotz Agent.
* Copyright (C) 2016 Domotz Ltd
*
* Domotz Agent is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Domotz Agent is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Domotz Agent. If not, see <http://www.gnu.org/licenses/>.
*
* Created by Tommaso Latini <tommaso@domotz.com> on 04/01/17.
*/
const PRINTABLE_CHAR = /[^\x20-\x7E]/g;
function parseDHCPDiscoverOutput(script, results) {
var data = {};
var lines = results.toString().split(/\n/);
lines.forEach(function (item) {
if (item.indexOf(script) > 0) {
var json = item.slice(item.indexOf(script) + script.length + 2).replace(PRINTABLE_CHAR, '');
try {
data = JSON.parse(json);
} catch (e) {
console.warn(e.message);
}
return data;
}
});
return data;
}
function parseResolvConf(data) {
var dnsServers = [];
data.split(/\r?\n/).every(function (item) {
var fields = item.split(/[ \t]+/);
if (fields[0] === "nameserver") {
dnsServers.push(fields[1]);
return false;
}
return true;
});
return dnsServers;
}
function parseWhoisOutput(data) {
var results = {};
var lines = data.split(/\r?\n/);
lines.every(function (item) {
var fields = item.split(/: +/);
if (fields[0] === "country") {
results.country = fields[1];
return false;
}
if (fields[0] === "inetnum") {
results.inetnum = fields[1];
}
if (fields[0] === "descr") {
results.descr = fields[1];
}
if (fields[0] === "netname") {
results.netname = fields[1];
}
return true;
});
return results;
}
module.exports.parseDHCPDiscoverOutput = parseDHCPDiscoverOutput;
module.exports.parseResolvConf = parseResolvConf;
module.exports.parseWhoisOutput = parseWhoisOutput;