ibmi.js 1.0 KB

123456789101112131415161718192021222324252627282930313233343536
  1. "use strict";
  2. const execa = require("execa");
  3. const db2util = "/QOpenSys/pkgs/bin/db2util";
  4. const sql = "select NEXT_HOP, LOCAL_BINDING_INTERFACE from QSYS2.NETSTAT_ROUTE_INFO where ROUTE_TYPE='DFTROUTE' and NEXT_HOP!='*DIRECT' and CONNECTION_TYPE=?";
  5. const parse = stdout => {
  6. let result;
  7. try {
  8. const resultObj = JSON.parse(stdout);
  9. const gateway = resultObj.records[0].NEXT_HOP;
  10. const iface = resultObj.records[0].LOCAL_BINDING_INTERFACE;
  11. result = {gateway, iface};
  12. } catch (err) {}
  13. if (!result) {
  14. throw new Error("Unable to determine default gateway");
  15. }
  16. return result;
  17. };
  18. const promise = family => {
  19. return execa.stdout(db2util, [sql, "-p", family, "-o", "json"]).then(stdout => parse(stdout));
  20. };
  21. const sync = family => {
  22. const {stdout} = execa.sync(db2util, [sql, "-p", family, "-o", "json"]);
  23. return parse(stdout);
  24. };
  25. module.exports.v4 = () => promise("IPV4");
  26. module.exports.v6 = () => promise("IPV6");
  27. module.exports.v4.sync = () => sync("IPV4");
  28. module.exports.v6.sync = () => sync("IPV6");