async-snmp
Version:
A lightweight wrapper for the Node.js net-snmp library
105 lines (101 loc) • 3.21 kB
JavaScript
;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// index.ts
var index_exports = {};
__export(index_exports, {
originizeResult: () => originizeResult,
snmpGet: () => snmpGet,
snmpSet: () => snmpSet
});
module.exports = __toCommonJS(index_exports);
// snmp-sessions/snmpSession.ts
var import_net_snmp = require("net-snmp");
var SnmpSession = class {
constructor(config) {
this.defaultUser = {
name: "admin",
level: import_net_snmp.SecurityLevel.authPriv,
authProtocol: import_net_snmp.AuthProtocols.sha,
authKey: "public",
privProtocol: import_net_snmp.PrivProtocols.aes,
privKey: "public"
};
this.sessions = {};
if (config) {
this.defaultUser = config;
}
}
getSession(ip, userConfig, options) {
if (!this.sessions[ip]) {
this.sessions[ip] = (0, import_net_snmp.createV3Session)(ip, userConfig || this.defaultUser, options);
}
return this.sessions[ip];
}
closeSession(ip) {
if (this.sessions[ip]) {
this.sessions[ip].close();
delete this.sessions[ip];
}
}
closeAllSessions() {
for (const session of Object.values(this.sessions)) {
session.close();
}
this.sessions = {};
}
};
var snmpSessionManager = new SnmpSession(process.env.SNMP_USER_CONFIG ? JSON.parse(process.env.SNMP_USER_CONFIG) : void 0);
// snmp-sessions/snmp.ts
var snmpGet = (ip, oids, userConfig, options) => {
return new Promise((resolve, reject) => {
const session = snmpSessionManager.getSession(ip, userConfig, options);
session == null ? void 0 : session.get(oids, (error, varbinds) => {
if (error) {
reject(error);
} else {
resolve(varbinds || []);
}
});
});
};
var snmpSet = (ip, varbinds, userConfig, options) => {
return new Promise((resolve, reject) => {
const session = snmpSessionManager.getSession(ip, userConfig, options);
session == null ? void 0 : session.set(varbinds, (error, setVarbinds) => {
if (error) {
reject(error);
} else {
resolve(setVarbinds || []);
}
});
});
};
var originizeResult = (varbinds) => {
return varbinds.reduce((acc, varbind) => {
const key = varbind.oid;
acc[key] = Buffer.isBuffer(varbind.value) ? varbind.value.toString("utf-8") : varbind.value;
return acc;
}, {});
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
originizeResult,
snmpGet,
snmpSet
});