var moment = require("moment");
var snmp = require("net-snmp");


var baseoid = "1.3.6.1.2.1.17.7.1.2.2.1.3";
var regexp = /([0-9]+).([0-9]+).([0-9]+).([0-9]+).([0-9]+).([0-9]+)$/;

var session = snmp.createSession("juni.ctdo.de", "ctdo23", { version: snmp.Version2c } )


function getMacFromOID(oid, callback) {
    var matches = regexp.exec(oid);
    var mac = "";

    if(matches != null) {
        for(var i = 1; i < matches.length; i++) {
            var num = parseInt(matches[i]);
            if(num <= 15) mac += "0";
            mac += num.toString(16) + ":";
        }
        mac = mac.substr(0, mac.length-1);

        callback(mac);
    }
}

function doneCb(error) {
    if (error)
        console.error(error.toString ());

}

function feedCb(varbinds) {
    for (var i = 0; i < varbinds.length; i++) {
        if (snmp.isVarbindError(varbinds[i])) {
            console.error(snmp.varbindError (varbinds[i]));
        } else {
            if(varbinds[i].value == "3") {	// only valid arp entries
                getMacFromOID(varbinds[i].oid, function(mac) {
                    console.log(mac);

                });
            }
        }
    }
}


session.subtree(baseoid, 20, feedCb, doneCb);

