calmh/node-snmp-native

multiple OIDs on set

leo-combes opened this issue · 4 comments

Multiple OIDs on set is allowed?
For example, like this:

snmpset -v2c -c private HOSTNAME .1.3.6.1.4.1.48.9.1.1.0 i 8 .1.3.6.1.4.1.48.9.1.6.0 i 4 .1.3.6.1.4.1.48.9.1.7.0 i 78 .1.3.6.1.4.1.48.9.1.3.0 u 0

calmh commented

I don't think so, unless you create the packet yourself.

But we'd merge a PR for this ;-)

This is useful in some cases, for example when you have to configure properties for any device that need to send more OIDs in one packet (my case) or only for speed up the setting of many individual values.
Adding this code in snmp.js should work (I have problems with "code" tags):

Session.prototype.setAll = function (options, callback) {
var self = this, pkt;
defaults(options, self.options);
parseOids(options);

if (!options.oids) {
    throw new Error('Missing required option `oids`.');
} else if (options.values === undefined) {
    throw new Error('Missing required option `value`.');
} else if (!options.types) {
    throw new Error('Missing required option `type`.');
}

function setOne(c) {
    var oid, pkt, m, vb;

    pkt = new Packet();
    pkt.community = options.community;
    pkt.version = options.version;
    pkt.pdu.type = asn1ber.pduTypes.SetRequestPDU;
    pkt.pdu.varbinds = [];

    // Push up to 16 varbinds in the same message.
    // The number 16 isn't really that magical, it's just a nice round
    // number that usually seems to fit withing a single packet and gets
    // accepted by the switches I've tested it on.
    for (m = 0; m < 16 && c < options.oids.length; m++) {
        vb = new VarBind();
        vb.oid = options.oids[c];
        vb.type = options.types[c];
        vb.value = options.values[c];
        pkt.pdu.varbinds.push(vb);
        c++;
    }

    self.sendMsg(pkt, options, callback);
}

setOne(0);

};

USE:
oids: An array of OIDs to set. Example: [[1, 3, 6, 1, 2, 1, 1, 4, 0], [1, 3, 6, 1, 2, 1, 1, 5, 0]] or ['.1.3.6.1.2.1.1.4.0', '.1.3.6.1.2.1.1.5.0'].
values: An array of values to set. Example: ["contact@support.com", "Max Power"].
types: An array of types to set. Example: [4, 4].

session.setAll({ oids: oids, values: values, types: types }, function(error, varbinds){
if (error) {
console.log('Fail :(');
} else {
console.log('The set is done.');
}
});

I think i would prefer an interface that receives an array of objects

session.setAll(
  [
    {oid: <oid>, type: <type>, value: <val>},
    {oid: <oid>, type: <type>, value: <val>}
  ]
)

But in any case is this not a PR ;-)