async-snmp
Version:
A lightweight wrapper for the Node.js net-snmp library
76 lines (74 loc) • 2.11 kB
JavaScript
// snmp-sessions/snmpSession.ts
import { AuthProtocols, createV3Session, PrivProtocols, SecurityLevel } from "net-snmp";
var SnmpSession = class {
constructor(config) {
this.defaultUser = {
name: "admin",
level: SecurityLevel.authPriv,
authProtocol: AuthProtocols.sha,
authKey: "public",
privProtocol: PrivProtocols.aes,
privKey: "public"
};
this.sessions = {};
if (config) {
this.defaultUser = config;
}
}
getSession(ip, userConfig, options) {
if (!this.sessions[ip]) {
this.sessions[ip] = 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;
}, {});
};
export {
originizeResult,
snmpGet,
snmpSet
};