SORU
6 EYLÜL 2010, PAZARTESİ


Node.js yerel IP adresini almak

Basit node.js bir program benim makine üzerinde çalışan var ve programım olduğu bilgisayarın yerel IP adresi çalışan almak istiyorum. Nasıl node.js ile alabilirim?

CEVAP
9 Aralık 2011, Cuma


'use strict';

var os = require('os');
var ifaces = os.networkInterfaces();

Object.keys(ifaces).forEach(function (ifname) {
  var alias = 0
    ;

  ifaces[ifname].forEach(function (iface) {
    if ('IPv4' !== iface.family || iface.internal !== false) {
      // skip over internal (i.e. 127.0.0.1) and non-ipv4 addresses
      return;
    }

    if (alias >= 1) {
      // this single interface has multiple ipv4 addresses
      console.log(ifname   ':'   alias, iface.address);
    } else {
      // this interface has only one ipv4 adress
      console.log(ifname, iface.address);
    }
  });
});

// en0 192.168.1.101
// eth0 10.0.0.101

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • Garrett Müller

    Garrett Mül

    26 HAZİRAN 2009
  • magnum33563

    magnum33563

    8 NİSAN 2011
  • TechShowsYou

    TechShowsYou

    3 Mart 2011