dev-ip.js 896 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. #! /usr/bin/env node
  2. /*
  3. * dev-ip
  4. * https://github.com/shakyshane/dev-ip
  5. *
  6. * Copyright (c) 2013 Shane Osbourne
  7. * Licensed under the MIT license.
  8. */
  9. "use strict";
  10. var messages = {
  11. error: "Couldn't find a suitable IP for you to use. (You're probably offline!)"
  12. };
  13. /**
  14. * @returns {Array}
  15. */
  16. function getIp() {
  17. var networkInterfaces = require("os").networkInterfaces();
  18. var matches = [];
  19. Object.keys(networkInterfaces).forEach(function (item) {
  20. networkInterfaces[item].forEach(function (address) {
  21. if (address.internal === false && address.family === "IPv4") {
  22. matches.push(address.address);
  23. }
  24. });
  25. });
  26. return matches;
  27. };
  28. module.exports = getIp;
  29. if (require.main === module) {
  30. var out = getIp();
  31. if (!out.length) {
  32. return console.log(messages.error);
  33. }
  34. console.log(getIp("cli"));
  35. }