• Node.js Video Tutorials

Node.js - os.networkInterfaces() 方法



Node.js 的 os 模組提供了一系列與作業系統相關的實用方法和屬性。

Node.js os.networkInterfaces() 方法將返回一個物件,其中包含已分配網路地址的網路介面。在返回的物件中,每個鍵都指定了一個網路介面。

返回的物件包含具有某些屬性的網路介面。這些屬性描述如下:

  • address − 這是一個字串,包含已分配的 IPv4 或 IPv6 地址。

  • netmask − 這是一個字串,包含已分配的 IPv4 或 IPv6 網路掩碼。

  • netmask − 此字串可以是 IPv4 或 IPv6。

  • mac − 一個字串,顯示網路介面的 MAC 地址。

  • internal − 這是一個布林值,如果網路介面是環回介面或類似的無法遠端訪問的介面,則為 true。否則為 false。

  • scopeid − 這是一個數字,包含 IPv6 範圍 ID,僅在 family 為 IPv6 時適用。

cidr − 這是一個 字串,指定已分配的 IPv4 或 IPv6 地址以及 CIDR 表示法中的路由字首。如果 netmask 無效,則該屬性設定為 NULL

語法

以下是 Node.js os.networkInterfaces() 方法的語法:

os.networkInterfaces()

引數

此方法不接受任何引數。

返回值

此方法返回一個物件,其中包含有關特定網路的網路介面的資訊。

示例

在以下示例中,我們嘗試使用 os.networkInterfaces() 方法列印網路的網路介面。

const os = require('os');
console.log("The network interfaces which have been assigned to this Network address are: ") 
console.log(os.networkInterfaces());

輸出

從下面的輸出中可以看出,當前計算機連線到乙太網,並且 os.networkInterfaces() 方法返回了連線的“乙太網”網路的網路介面。

The network interfaces which have been assigned to this Network address are:
{
  Ethernet: [
      {
         address: 'fe80::46d1:d65a:b6cc:2926',
         netmask: 'ffff:ffff:ffff:ffff::',
         family: 'IPv6',
         mac: '88:a4:c2:bb:d1:f9',
         internal: false,
         cidr: 'fe80::46d1:d65a:b6cc:2926/64',
         scopeid: 8
      },
      {
         address: '192.168.2.33',
         netmask: '255.255.255.0',
         family: 'IPv4',
         mac: '88:a4:c2:bb:d1:f9',
         internal: false,
         cidr: '192.168.2.33/24'
      }
   ],
  'Loopback Pseudo-Interface 1': [
      {
         address: '::1',
         netmask: 'ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff',
         family: 'IPv6',
         mac: '00:00:00:00:00:00',
         internal: true,
         cidr: '::1/128',
         scopeid: 0
      },
      {
         address: '127.0.0.1',
         netmask: '255.0.0.0',
         family: 'IPv4',
         mac: '00:00:00:00:00:00',
         internal: true,
         cidr: '127.0.0.1/8'
      }
   ]
}

示例

在以下示例中,我們嘗試列印當前系統連線的各種網路介面的詳細資訊。

const os = require('os');
console.log(os.networkInterfaces());

輸出

執行上述程式後,os.networkInterfaces() 方法返回了連線的“Wi-Fi”網路的網路介面。

{
   'Wi-Fi': [
      {
         address: 'fe80::57e9:cc91:95fc:5f5b',
         netmask: 'ffff:ffff:ffff:ffff::',
         family: 'IPv6',
         mac: 'bc:f1:71:3b:eb:a1',
         internal: false,
         cidr: 'fe80::57e9:cc91:95fc:5f5b/64',
         scopeid: 16
      },
      {
         address: '192.168.25.229',
         netmask: '255.255.255.0',
         family: 'IPv4',
         mac: 'bc:f1:71:3b:eb:a1',
         internal: false,
         cidr: '192.168.25.229/24'
      }
   ],
   'Loopback Pseudo-Interface 1': [
      {
         address: '::1',
         netmask: 'ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff',
         family: 'IPv6',
         mac: '00:00:00:00:00:00',
         internal: true,
         cidr: '::1/128',
         scopeid: 0
      },
      {
         address: '127.0.0.1',
         netmask: '255.0.0.0',
         family: 'IPv4',
         mac: '00:00:00:00:00:00',
         internal: true,
         cidr: '127.0.0.1/8'
      }
   ]
}
nodejs_os_module.htm
廣告