Skip to content

Instantly share code, notes, and snippets.

@Phinome
Forked from szalishchuk/ip.js
Created October 15, 2016 05:19
Show Gist options
  • Save Phinome/0480922a32bc98e956ad3ee063c5e79a to your computer and use it in GitHub Desktop.
Save Phinome/0480922a32bc98e956ad3ee063c5e79a to your computer and use it in GitHub Desktop.

Revisions

  1. @szalishchuk szalishchuk renamed this gist Feb 17, 2014. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. @szalishchuk szalishchuk created this gist Feb 17, 2014.
    22 changes: 22 additions & 0 deletions ipAddress
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,22 @@
    var
    // Local ip address that we're trying to calculate
    address
    // Provides a few basic operating-system related utility functions (built-in)
    ,os = require('os')
    // Network interfaces
    ,ifaces = os.networkInterfaces();


    // Iterate over interfaces ...
    for (var dev in ifaces) {

    // ... and find the one that matches the criteria
    var iface = ifaces[dev].filter(function(details) {
    return details.family === 'IPv4' && details.internal === false;
    });

    if(iface.length > 0) address = iface[0].address;
    }

    // Print the result
    console.log(address);