├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── cli.js ├── index.js ├── package.json └── test.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - 6 4 | - 5 5 | - 4 6 | - "0.12" 7 | - "0.10" 8 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Mathias Buus 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## network-address 2 | 3 | get the local network address of your machine 4 | 5 | ``` 6 | npm install network-address 7 | ``` 8 | 9 | [![build status](http://img.shields.io/travis/mafintosh/network-address.svg?style=flat)](http://travis-ci.org/mafintosh/network-address) 10 | 11 | it is easy to use 12 | 13 | ``` js 14 | var address = require('network-address') 15 | 16 | console.log(address()) // prints something like 192.168.3.102 17 | console.log(address.ipv6()) // prints something like fc00::5137:ecb:55be:c2fc 18 | ``` 19 | 20 | it is also available as a command line tool 21 | 22 | ``` 23 | npm install -g network-address 24 | ``` 25 | 26 | you are now able to run `network-address` in your terminal -------------------------------------------------------------------------------- /cli.js: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | var argv = process.argv.slice(2) 3 | 4 | var ipv6 5 | var iface 6 | 7 | for (var i = 0; i < argv.length; i++) { 8 | if (argv[i] === '-6') ipv6 = true 9 | else if (argv[i][0] !== '-') iface = argv[i] 10 | } 11 | 12 | if (ipv6) console.log(require('./index').ipv6(iface)) 13 | else console.log(require('./index')(iface)) 14 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var os = require('os') 2 | 3 | function pickInterface (interfaces, family) { 4 | for (var i in interfaces) { 5 | for (var j = interfaces[i].length - 1; j >= 0; j--) { 6 | var face = interfaces[i][j] 7 | var reachable = family === 'IPv4' || face.scopeid === 0 8 | if (!face.internal && face.family === family && reachable) return face.address 9 | } 10 | } 11 | return family === 'IPv4' ? '127.0.0.1' : '::1' 12 | } 13 | 14 | function reduceInterfaces (interfaces, iface) { 15 | var ifaces = {} 16 | for (var i in interfaces) { 17 | if (i === iface) ifaces[i] = interfaces[i] 18 | } 19 | return ifaces 20 | } 21 | 22 | function ipv4 (iface) { 23 | var interfaces = os.networkInterfaces() 24 | if (iface) interfaces = reduceInterfaces(interfaces, iface) 25 | return pickInterface(interfaces, 'IPv4') 26 | } 27 | 28 | function ipv6 (iface) { 29 | var interfaces = os.networkInterfaces() 30 | if (iface) interfaces = reduceInterfaces(interfaces, iface) 31 | return pickInterface(interfaces, 'IPv6') 32 | } 33 | 34 | ipv4.ipv4 = ipv4 35 | ipv4.ipv6 = ipv6 36 | 37 | module.exports = ipv4 38 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "network-address", 3 | "version": "1.1.2", 4 | "scripts": { 5 | "test": "standard && tape test.js" 6 | }, 7 | "dependencies": {}, 8 | "repository": { 9 | "type": "git", 10 | "url": "git://github.com/mafintosh/network-address" 11 | }, 12 | "description": "get the local network address of your machine", 13 | "keywords": [ 14 | "network", 15 | "address" 16 | ], 17 | "author": "Mathias Buus Madsen ", 18 | "bin": { 19 | "network-address": "./cli.js" 20 | }, 21 | "devDependencies": { 22 | "standard": "^6.0.7", 23 | "tape": "^3.0.3" 24 | }, 25 | "bugs": { 26 | "url": "https://github.com/mafintosh/network-address/issues" 27 | }, 28 | "homepage": "https://github.com/mafintosh/network-address", 29 | "main": "index.js", 30 | "license": "MIT" 31 | } 32 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | var tape = require('tape') 2 | var address = require('./') 3 | 4 | tape('ipv4', function (t) { 5 | t.ok(/^\d+\.\d+\.\d+\.\d+$/.test(address()), 'looks like a ipv4') 6 | t.end() 7 | }) 8 | 9 | tape('ipv4 #2', function (t) { 10 | t.ok(/^\d+\.\d+\.\d+\.\d+$/.test(address.ipv4()), 'looks like a ipv4') 11 | t.end() 12 | }) 13 | 14 | tape('ipv4 #3', function (t) { 15 | t.ok(/^\d+\.\d+\.\d+\.\d+$/.test(address.ipv4('lo')), 'looks like a ipv4') 16 | t.end() 17 | }) 18 | 19 | tape('ipv6', function (t) { 20 | t.ok(/([0-f]*:)?([0-f]*:)?([0-f]*:)?([0-f]*:)?$/.test(address.ipv6()), 'looks like a ipv6') 21 | t.end() 22 | }) 23 | --------------------------------------------------------------------------------