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 = async family => {
  19. const {stdout} = await execa(db2util, [sql, "-p", family, "-o", "json"]);
  20. return parse(stdout);
  21. };
  22. const sync = family => {
  23. const {stdout} = execa.sync(db2util, [sql, "-p", family, "-o", "json"]);
  24. return parse(stdout);
  25. };
  26. module.exports.v4 = () => promise("IPV4");
  27. module.exports.v6 = () => promise("IPV6");
  28. module.exports.v4.sync = () => sync("IPV4");
  29. module.exports.v6.sync = () => sync("IPV6");