os.networkInterfaces()


os.networkInterfaces()方法返回一个对象,包含只有被赋予网络地址的网络接口.

在返回对象的每个关键词都指明了一个网络接口.

返回的值是一个对象数组, 每个都描述了赋予的网络地址.

被赋予网络地址的对象包含的属性:

  • address <string> 被赋予的 IPv4 或 IPv6 地址
  • netmask <string> IPv4 或 IPv6 子网掩码
  • family <string> IPv4IPv6
  • mac <string> 网络接口的MAC地址
  • internal <boolean> 如果 网络接口是loopback或相似的远程不能用的接口时, 值为true,否则为false
  • scopeid <number> IPv6 数字领域识别码 (只有当 familyIPv6时可用)
  • cidr <string> 以 CIDR 表示法分配的带有路由前缀的 IPv4 或 IPv6 地址。如果 netmask 参数不可用,则该属性是 null
{
  lo: [
    {
      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'
    },
    {
      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'
    }
  ],
  eth0: [
    {
      address: '192.168.1.108',
      netmask: '255.255.255.0',
      family: 'IPv4',
      mac: '01:02:03:0a:0b:0c',
      internal: false,
      cidr: '192.168.1.108/24'
    },
    {
      address: 'fe80::a00:27ff:fe4e:66a1',
      netmask: 'ffff:ffff:ffff:ffff::',
      family: 'IPv6',
      mac: '01:02:03:0a:0b:0c',
      internal: false,
      cidr: 'fe80::a00:27ff:fe4e:66a1/64'
    }
  ]
}

The os.networkInterfaces() method returns an object containing only network interfaces that have been assigned a network address.

Each key on the returned object identifies a network interface. The associated value is an array of objects that each describe an assigned network address.

The properties available on the assigned network address object include:

  • address <string> The assigned IPv4 or IPv6 address
  • netmask <string> The IPv4 or IPv6 network mask
  • family <string> Either IPv4 or IPv6
  • mac <string> The MAC address of the network interface
  • internal <boolean> true if the network interface is a loopback or similar interface that is not remotely accessible; otherwise false
  • scopeid <number> The numeric IPv6 scope ID (only specified when family is IPv6)
  • cidr <string> The assigned IPv4 or IPv6 address with the routing prefix in CIDR notation. If the netmask is invalid, this property is set to null.
{
  lo: [
    {
      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'
    },
    {
      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'
    }
  ],
  eth0: [
    {
      address: '192.168.1.108',
      netmask: '255.255.255.0',
      family: 'IPv4',
      mac: '01:02:03:0a:0b:0c',
      internal: false,
      cidr: '192.168.1.108/24'
    },
    {
      address: 'fe80::a00:27ff:fe4e:66a1',
      netmask: 'ffff:ffff:ffff:ffff::',
      family: 'IPv6',
      mac: '01:02:03:0a:0b:0c',
      internal: false,
      cidr: 'fe80::a00:27ff:fe4e:66a1/64'
    }
  ]
}