forked from PrimeEuler/mib.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmib2json.js
More file actions
executable file
·92 lines (71 loc) · 2.17 KB
/
mib2json.js
File metadata and controls
executable file
·92 lines (71 loc) · 2.17 KB
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
#!/usr/bin/env node
/**
* Executable that parses a MIB and outputs OID definitions as JSON format.
* USAGE:
*
* mib2json path/to/MY-DEFS.MIB [MY-DEFS-MIB]
*
* Outputs pretty-formatted json to stdout. If the MIB could not be found
* or parsed, this will exit with a `1` status code.
*/
var path = require('path');
var Mib = require('./lib/mib');
var mib = Mib();
/* this is necessary because type & macro definitions are almost certainly
* being used from Smiv2, etc
*/
mib.LoadMIBs();
var defaultMibs = Object.keys(mib.Modules);
module.exports = mib;
/**
* NOTE: no idea why but the original author requires the path + filename
* to use
*/
function main(args) {
if ( ! args.length > 1 ) {
console.warn("USAGE: mib2json path/to/MY-DEFS.MIB [MY-DEFS-MIB]");
process.exit(1);
}
var filePath = args[2];
// for some reason the original author wanted the path/file to be separated by
// *two* slashes... So we will take a valid path and 'fix' it.
var pathname = path.dirname(filePath);
var filename = path.basename(filePath);
filePath = [pathname, filename].join('//');
// import and parse the new MIB definition:
mib.Import(filePath);
mib.Serialize();
var newMib = args[3];
if ( ! newMib ) { // attempt to guess MIB defs name if it wasn't given
var newMibs = inferNewMIB(mib);
if ( newMibs.length < 1 ) {
console.error('No new MIB definition found!');
process.exit(1);
}
if (newMibs.length > 1 ) {
console.error('Multiple MIB definitions found! Please specify MIB name as second arg:')
for ( var i in newMibs )
console.warn(' ', newMibs[i]);
process.exit(1);
}
newMib = newMibs[0];
}
var module = mib.Modules[newMib];
if ( ! module ) {
console.error("Couldn't find module named %s after parsing MIB!", newMib);
process.exit(1);
}
process.stdout.write(
JSON.stringify(module, null, 2));
}
function inferNewMIB(mib) {
// find the new MIB by removing all of the default ones:
for ( var i in defaultMibs ) {
delete mib.Modules[defaultMibs[i]]
}
return Object.keys(mib.Modules);
}
if ( require.main === module ) {
main(process.argv);
process.exit(0);
}