-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
92 lines (82 loc) · 2.28 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import { Client } from "ssh2";
import { readFileSync } from "node:fs";
import { config } from "dotenv";
config();
const PRIVATE_KEY = process.env.PRIVATE_KEY;
const PROXY = {
host: process.env.PROXY_HOST,
user: process.env.PROXY_USER,
};
const REMOTE = {
host: process.env.REMOTE_HOST,
user: process.env.REMOTE_USER,
};
const runSNMP = (command) => {
return new Promise((resolve, reject) => {
const conn = new Client();
conn
.on("ready", () => {
let output = "";
conn.exec(command, (err, stream) => {
if (err) reject(err);
stream
.on("data", (data) => (output += data))
.on("close", () => {
resolve(output);
conn.end();
})
.stderr.on("data", (data) => reject(data));
});
})
.connect({
host: PROXY.host,
port: 22,
username: PROXY.user,
privateKey: readFileSync(PRIVATE_KEY),
});
});
};
const snmp = async (ip, community) => {
const ifOids = {
ifIndex: "INTEGER: ",
ifName: "STRING: ",
ifDescr: "STRING: ",
ifAlias: "STRING: ",
ifHighSpee: "Gauge32: ",
ifOperstatus: "INTEGER: ",
ifAdminStatus: "INTEGER: ",
};
const sysOids = {sysname: "", syslocation: ""};
let command = `ssh ${REMOTE.user}@${REMOTE.host}`;
command = `${command} /usr/sfw/bin/snmpwalk -v 2c -c ${community} ${ip}`;
await Promise.all(
Object.keys(ifOids).map(async (oid) => {
return await runSNMP(`${command} ${oid}`)
.then((res) => {
const arr = res
.split("\n")
.map((item) => item.split(ifOids[oid])[1])
.filter((item) => item !== undefined);
ifOids[oid] = arr;
})
.catch((err) => console.error(err));
}),
Object.keys(sysOids).map(async (oid) => {
return await runSNMP(`${command} ${oid}`)
.then((res) => {
const arr = res
.split("\n")
.map((item) => item.split("STRING: ")[1])
.filter((item) => item !== undefined);
sysOids[oid] = arr[0];
})
.catch((err) => console.error(err));
})
);
return {sysOids, ifOids}
};
const main = async () => {
const result = await snmp("192.168.1.1", "public");
console.log(result);
};
main()