This repository has been archived by the owner on Feb 15, 2019. It is now read-only.
forked from silverwind/default-gateway
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaix.js
95 lines (77 loc) · 2.32 KB
/
aix.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
93
94
95
"use strict";
const getSqlStatement = family => {
let sql =
"select NEXT_HOP, LOCAL_BINDING_INTERFACE from QSYS2.NETSTAT_ROUTE_INFO where ROUTE_TYPE='DFTROUTE' and NEXT_HOP!='*DIRECT'";
if (family === "v4") {
sql += " and CONNECTION_TYPE='IPV4'";
} else {
sql += " and CONNECTION_TYPE='IPV6'";
}
return sql;
};
const getGatewayInformationAsync = async family => {
return new Promise((resolve, reject) => {
try {
const idbConnector = require("idb-connector");
const dbconn = new idbConnector.dbconn();
dbconn.conn("*LOCAL");
const sql = getSqlStatement(family);
const stmt = new idbConnector.dbstmt(dbconn);
stmt.exec(sql, async results => {
try {
stmt.close();
dbconn.disconn();
dbconn.close();
} catch (err) {
reject(new Error("Unable to determine default gateway"));
return;
}
if (results && results[0] && results[0].NEXT_HOP) {
resolve({
gateway: results[0].NEXT_HOP,
interface: results[0].LOCAL_BINDING_INTERFACE || ""
});
} else {
reject(new Error("Unable to determine default gateway"));
}
});
} catch (err) {
reject(new Error("Unable to determine default gateway"));
return;
}
});
};
const getGatewayInformationSync = family => {
let results;
try {
const idbConnector = require("idb-connector");
const dbconn = new idbConnector.dbconn();
dbconn.conn("*LOCAL");
const sql = getSqlStatement(family);
const stmt = new idbConnector.dbstmt(dbconn);
results = stmt.execSync(sql);
stmt.close();
dbconn.disconn();
dbconn.close();
} catch (err) {
throw new Error("Unable to determine default gateway");
}
if (results && results[0] && results[0].NEXT_HOP) {
return {
gateway: results[0].NEXT_HOP,
interface: results[0].LOCAL_BINDING_INTERFACE || ""
};
} else {
throw new Error("Unable to determine default gateway");
}
};
const promise = family => {
return getGatewayInformationAsync(family);
};
const sync = family => {
return getGatewayInformationSync(family);
};
module.exports.v4 = () => promise("v4");
module.exports.v6 = () => promise("v6");
module.exports.v4.sync = () => sync("v4");
module.exports.v6.sync = () => sync("v6");