1 | #!/usr/bin/env node
|
2 | "use strict";
|
3 |
|
4 | process.title = "oui";
|
5 | parseArgs();
|
6 |
|
7 | function parseArgs() {
|
8 | const args = require("minimist")(process.argv.slice(2), {
|
9 | boolean: true,
|
10 | string: ["_"],
|
11 | });
|
12 |
|
13 | if (args._[0] === "update" || args.update) {
|
14 | const interval = require("char-spinner")();
|
15 | const opts = {};
|
16 | if (args._[1]) opts.url = args._[1];
|
17 | if (args.w) opts.web = true;
|
18 |
|
19 | require("./update.js")(opts).then(() => {
|
20 | clearInterval(interval);
|
21 | process.exit(0);
|
22 | }).catch(err => {
|
23 | clearInterval(interval);
|
24 | process.stdout.write(`${err}\n`);
|
25 | process.exit(1);
|
26 | });
|
27 | } else if (args._[0] === "search" || args.search) {
|
28 | const params = args.search ? args._ : args._.slice(1);
|
29 | search(params.map(pattern => {
|
30 | return `*${pattern}*`;
|
31 | }));
|
32 | } else if (!args._.length || args._[0] === "help" || args.help) {
|
33 | process.stdout.write(`${[
|
34 | "",
|
35 | " Usage: oui [mac]|[command] [options]",
|
36 | "",
|
37 | " Commands:",
|
38 | " [mac] look up a MAC address in the database",
|
39 | " search [patterns] search vendors using one or more search patterns",
|
40 | " version print the version",
|
41 | " update [url] [-w] update the database with optional source URL. If -w",
|
42 | " is given, oui.web.js will also be updated.",
|
43 | "",
|
44 | " Examples:",
|
45 | " oui 20:37:06:12:34:56",
|
46 | " oui 203706",
|
47 | " oui update",
|
48 | " oui search cisco theory",
|
49 | " echo 20:37:06:12:34:56 | oui",
|
50 | " echo 203706 | oui",
|
51 | ].join("\n")}\n\n`);
|
52 | process.exit(0);
|
53 | } else if (args._[0] === "version" || args.v || args.V || args.version) {
|
54 | const pkg = require("path").join(__dirname, "package.json");
|
55 | process.stdout.write(`${require(pkg).version}\n`);
|
56 | } else {
|
57 | lookup(args._[0]);
|
58 | }
|
59 | }
|
60 |
|
61 | function lookup(str) {
|
62 | let result;
|
63 | try {
|
64 | result = require(".")(str);
|
65 | } catch (err) {
|
66 | process.stdout.write(`${err.message}\n`);
|
67 | process.exit(1);
|
68 | }
|
69 |
|
70 | if (result) {
|
71 | process.stdout.write(`${result}\n`);
|
72 | } else {
|
73 | process.stdout.write(`${str} not found in database\n`);
|
74 | }
|
75 |
|
76 | process.exit(0);
|
77 | }
|
78 |
|
79 | function search(patterns) {
|
80 | const results = require(".").search(patterns, {nocase: true});
|
81 |
|
82 | if (!results.length) {
|
83 | return process.exit(1);
|
84 | }
|
85 |
|
86 | const structured = [];
|
87 | results.forEach(result => {
|
88 | const oui = result.oui;
|
89 | const [organzation, address, country] = result.organization.split("\n");
|
90 | structured.push({oui, organzation, address, country});
|
91 | });
|
92 |
|
93 | const arr = [Object.keys(structured[0]).map(name => name.toUpperCase())];
|
94 | for (const entry of structured) arr.push(Object.values(entry).map(v => v || ""));
|
95 | process.stdout.write(`${require("text-table")(arr, {hsep: " "})}\n`);
|
96 | process.exit(0);
|
97 | }
|