As The last ship sailed towards the distant horizon I sat there watching on a rock My mind slowly drifting away Forming into my... Dreamtale
Analyze dnsmasq log with Node.js
2018-09-05 / 1 min read
node dnsmasq.log.js ~/dnsmasq.log
source code:
var fs = require("fs");
var readline = require('readline');
var ips = {};

function parse() {
    var arguments = process.argv.splice(2);
    console.log(arguments[0])
<!-- more -->

    var fRead = fs.createReadStream(arguments[0]);
    var objReadline = readline.createInterface({
        input: fRead,
    });


    var index = 1;
    objReadline.on('line', function (line) {
        var tmp = 'line' + index.toString() + ':' + line;
        // fWrite.write(tmp + os.EOL); // 下一行
        console.log(index, line);
        var fromIndex = line.indexOf("from")
        if (fromIndex > 0) {
            var ip = line.substr(fromIndex + 5, 16);
            if (ips[ip] > 0) {
                ips[ip] += 1;
            } else {
                ips[ip] = 1;
            }
        }
        index++;
    });

    objReadline.on('close', function () {
        console.log('readline close...\n' + JSON.stringify(ips));
        var keys = Object.keys(ips);

        console.log("--->\n" + keys.length);
        for (i = 0; i < keys.length; i++) {
            console.log(keys[i]);
        }
    });
}


parse();