<commit_before>/*jslint node: true, continue: false, plusplus: false, bitwise: true, plusplus: true
  newcap: true, maxerr: 50, indent: 4, undef: true, sloppy: true, nomen: true*/

// Example code for node-snmp-native.
// ----

// This file contains examples of how to use the library.

// Basic setup
// -----

// The snmp object is the main entry point to the library.
var snmp = require('snmp-native');

// We'll use the Underscore library for a few convenience functions.
// You should too. We'll also pull in the `util` library so we can print
// out object structures nicely.
var _ = require('underscore');
var util = require('util');

var host = 'localhost';
var community = 'public';

// A session is required to communicate with an agent.
var session = new snmp.Session(host, community);

// All OIDs are represented as integer arrays.
// There is no interpretation of string or MIB names.
// This here is the OID for sysDescr.0.
var oid = [1, 3, 6, 1, 2, 1, 1, 1, 0];

// Getting a single value
// -----

// This is how you issue a simple get request.
session.get(oid, function (err, pkt) {
    var vb;

    if (err) {
        // If there is an error, such as an SNMP timeout, we'll end up here.
        console.log(err);
    } else {
        // The pkt parameter is a Packet instance, which is closely
        // modelled after the actual layout of an SNMP packet.
        // The minimum you need to know is that there is an array of varbinds
        // that usually contain exactly one entry, in which the 'value'
        // property holds the reply.
        vb = pkt.pdu.varbinds[0];
        console.log('The system description is "' + vb.value + '"');

        // For reference, this shows how the entire structure looks.
        console.log('\nThe received Packet structure looks like this:');
        console.log(util.inspect(pkt, false, null) + '\n');
    }

    // The session must be closed when you're done with it.
    session.close();
});

// Parsing an OID string and getting an entire tree
// -----

// We'll establish a new session since we're doing something independently of the previous requests.
// Since it's all asynchronous, it's likely that we haven't received the response and closed the session
// above yet, but it might happen later while we're doing other stuff.
var session2 = new snmp.Session(host, community);

// Here we convert an OID from string representation to array.
// This is the base OID for the ifName tree.
var oidStr = '.1.3.6.1.2.1.31.1.1.1.1';
oid = _.map(_.compact(oidStr.split('.')), function (x) { return parseInt(x, 10); });

// You can also get an entire subtree (an SNMP walk).
session2.getSubtree(oid, function (err, varbinds) {
    var vb;

    // The callback will be called once for each entry in the tree.

    if (err) {
        // If there is an error, such as an SNMP timeout, we'll end up here.
        console.log(err);
    } else {
        _.each(varbinds, function (vb) {
            console.log('Name of interface ' + _.last(vb.oid)  + ' is "' + vb.value + '"');
        });
    }

    session2.close();
});

// Finally, you can get all of a collection of OIDs in one go.
// The semantics is similar to getSubtree.

var session3 = new snmp.Session(host, community);
var oids = [[1, 3, 6, 1, 2, 1, 1, 1, 0], [1, 3, 6, 1, 2, 1, 1, 2, 0]];
session3.getAll(oids, function (err, varbinds) {
    _.each(varbinds, function (vb) {
        console.log(vb.oid + ' = ' + vb.value);
    });
});


// Example output
// -----<commit_msg>Update example for new sessionless interface.<commit_after>/*jslint node: true, continue: false, plusplus: false, bitwise: true, plusplus: true
  newcap: true, maxerr: 50, indent: 4, undef: true, sloppy: true, nomen: true*/

// Example code for node-snmp-native.
// ----

// This file contains examples of how to use the library.

// Basic setup
// -----

// The snmp object is the main entry point to the library.
var snmp = require('snmp-native');

// We'll use the Underscore library for a few convenience functions.
// You should too. We'll also pull in the `util` library so we can print
// out object structures nicely.
var _ = require('underscore');
var util = require('util');

var host = 'localhost';
var community = 'public';

// A session is required to communicate with an agent.
var session = new snmp.Session(host, community);

// All OIDs are represented as integer arrays.
// There is no interpretation of string or MIB names.
// This here is the OID for sysDescr.0.
var oid = [1, 3, 6, 1, 2, 1, 1, 1, 0];

// Getting a single value
// -----

// This is how you issue a simple get request.
session.get(oid, function (err, pkt) {
    var vb;

    if (err) {
        // If there is an error, such as an SNMP timeout, we'll end up here.
        console.log(err);
    } else {
        // The pkt parameter is a Packet instance, which is closely
        // modelled after the actual layout of an SNMP packet.
        // The minimum you need to know is that there is an array of varbinds
        // that usually contain exactly one entry, in which the 'value'
        // property holds the reply.
        vb = pkt.pdu.varbinds[0];
        console.log('The system description is "' + vb.value + '"');

        // For reference, this shows how the entire structure looks.
        console.log('\nThe received Packet structure looks like this:');
        console.log(util.inspect(pkt, false, null) + '\n');
    }

    // The session must be closed when you're done with it.
    session.close();
});

// Parsing an OID string and getting an entire tree
// -----

// We'll establish a new session since we're doing something independently of the previous requests.
// Since it's all asynchronous, it's likely that we haven't received the response and closed the session
// above yet, but it might happen later while we're doing other stuff.
var session2 = new snmp.Session(host, community);

// Here we convert an OID from string representation to array.
// This is the base OID for the ifName tree.
var oidStr = '.1.3.6.1.2.172.16.17.32.1.1';
oid = _.map(_.compact(oidStr.split('.')), function (x) { return parseInt(x, 10); });

// You can also get an entire subtree (an SNMP walk).
session2.getSubtree(oid, function (err, varbinds) {
    var vb;

    // The callback will be called once for each entry in the tree.

    if (err) {
        // If there is an error, such as an SNMP timeout, we'll end up here.
        console.log(err);
    } else {
        _.each(varbinds, function (vb) {
            console.log('Name of interface ' + _.last(vb.oid)  + ' is "' + vb.value + '"');
        });
    }

    session2.close();
});

// You can get all of a collection of OIDs in one go.
// The semantics is similar to getSubtree.

var session3 = new snmp.Session(host, community);
var oids = [[1, 3, 6, 1, 2, 1, 1, 1, 0], [1, 3, 6, 1, 2, 1, 1, 2, 0]];
session3.getAll(oids, function (err, varbinds) {
    _.each(varbinds, function (vb) {
        console.log(vb.oid + ' = ' + vb.value);
    });
    session3.close();
});

// You can also create a destination-less "session" to use on multiple
// hosts. This is useful for conserving file descriptors when talking
// to a large number of hosts. This example scans the 192.168.1.0/24
// network for SNMP responders.

var session4 = new snmp.Session(); // New session without IP + community parameters.
var oid = [1, 3, 6, 1, 2, 1, 1, 1, 0]; // sysDescr.0
var cnt = 254; // Expected number of callbacks.
for (var i = 1; i < 255; i++) {
    // We need a function to get a closure over i.
    (function (host) {
        session4.get(oid, host, 'public', function (err, pkt) {
            if (err) {
                // Probably a Timeout.
                console.log('Error for ' + host + ': ' + err);
            } else {
                // Print the returned value (sysDescr).
                var vb = pkt.pdu.varbinds[0];
                console.log(host + ': ' + vb.oid + ' = ' + vb.value);
            }

            if (--cnt === 0) {
                // All requests have returned, time to close the session and exit.
                session4.close();
            }
        });
    }('192.168.1.' + i));
}

// Example output
// -----
{'fraction_non_alphanumeric': 0.12984472429392346, 'fraction_numerical': 0.017850592982027142, 'mean_word_length': 2.476413089672758, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 5, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3766446', 'n_tokens_mistral': 2621, 'n_tokens_neox': 2469, 'n_words': 1049}