Skip to content

Network ​

In this section you will learn how to get detailed information about network interfaces, network connections and statistics as well as some internet related information (latency, check availability of site):

For function reference and examples we assume, that we imported systeminspector as follows:

const si = require('@ambicuity/systeminspector');

Network Interfaces, Network Stats, Network Connections ​

All functions in this section return a promise or can be called with a callback function (parameter cb in the function reference)

FunctionResult objectLinuxBSDMacWinSunComments
si.networkInterfaces(cb)[{...}]XXXXXarray of network interfaces (objects)
[0].ifaceXXXXXinterface
[0].ifaceNameXXXXXinterface name (differs on Windows)
[0].defaultXXXXXtrue if this is the default interface
[0].ip4XXXXXip4 address
[0].ip4subnetXXXXXip4 subnet mask
[0].ip6XXXXXip6 address
[0].ip6subnetXXXXXip6 subnet mask
[0].macXXXXXMAC address
[0].internalXXXXXtrue if internal interface
[0].virtualXXXXXtrue if virtual interface
[0].operstateXXXup / down
[0].typeXXXwireless / wired
[0].duplexXXduplex (full/half)
[0].mtuXXMTU maximum transmission unit
[0].speedXXXSpeed in Mbit / s
[0].dhcpXXXIP address obtained by DHCP
[0].dnsSuffixXXDNS suffix
[0].ieee8021xAuthXXIEEE 802.1x Auth
[0].ieee8021xStateXXXIEEE 802.1x State
[0].carrierChangesX# changes up/down
si.networkInterfaceDefault(cb): stringXXXXXget name of default network interface
si.networkGatewayDefault(cb): stringXXXXXget default network gateway
si.networkStats(iface,cb)[{...}]XXXXcurrent network stats of given interfaces,
iface list: comma separated,
iface parameter is optional,
defaults to first external network interface,
pass '*' for all interfaces
[0].ifaceXXXXinterface
[0].operstateXXXXup / down
[0].rx_bytesXXXXreceived bytes overall
[0].rx_droppedXXXXreceived dropped overall
[0].rx_errorsXXXXreceived errors overall
[0].tx_bytesXXXXtransferred bytes overall
[0].tx_droppedXXXXtransferred dropped overall
[0].tx_errorsXXXXtransferred errors overall
[0].rx_secXXXXreceived bytes / second (* see notes)
[0].tx_secXXXXtransferred bytes per second (* see notes)
[0].msXXXXinterval length (for per second values)
si.networkConnections(cb)[{...}]XXXXcurrent network network connections
returns an array of all connections
[0].protocolXXXXtcp or udp
[0].localAddressXXXXlocal address
[0].localPortXXXXlocal port
[0].peerAddressXXXXpeer address
[0].peerPortXXXXpeer port
[0].stateXXXXlike ESTABLISHED, TIME_WAIT, ...
[0].pidXXXXprocess ID
[0].processXXXprocess name

Site availability, Internet Latency ​

FunctionResult objectLinuxBSDMacWinSunComments
si.inetChecksite(url, cb)XXXXXresponse-time (ms) to fetch given URL
urlXXXXXgiven url
okXXXXXstatus code OK (2xx, 3xx)
statusXXXXXstatus code
msXXXXXresponse time in ms
si.inetLatency(host, cb): numberXXXXXresponse-time (ms) to external resource
host parameter is optional (default 8.8.8.8)

Getting correct stats values ​

In networkStats() the results / sec. values (rx_sec, tx_sec, ...) are calculated correctly beginning with the second call of the function. It is determined by calculating the difference of transferred bytes / IOs divided by the time between two calls of the function.

The first time you are calling one of this functions, you will get -1 for transfer rates. The second time, you should then get statistics based on the time between the two calls ...

So basically, if you e.g. need a values for filesystem stats stats every second, your code should look like this:

const si = require('@ambicuity/systeminspector');

setInterval(function() {
    si.networkStats().then(data => {
        console.log(data);
    })
}, 1000)

Beginning with the second call, you get network transfer values per second.

Examples ​

Example ​
const si = require('@ambicuity/systeminspector');
si.networkInterfaces().then(data => console.log(data));
json
[
  {
    iface: 'lo0',
    ifaceName: 'lo0',
    default: false,
    ip4: '127.0.0.1',
    ip4subnet: '255.0.0.0',
    ip6: '::1',
    ip6subnet: 'ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff',
    mac: '',
    internal: true,
    virtual: false,
    operstate: 'down',
    type: 'wired',
    duplex: 'full',
    mtu: 16384,
    speed: null,
    dhcp: false,
    dnsSuffix: '',
    ieee8021xAuth: '',
    ieee8021xState: '',
    carrierChanges: 0
  },
  {
    iface: 'en0',
    ifaceName: 'en0',
    default: true,
    ip4: '192.168.0.27',
    ip4subnet: '255.255.255.0',
    ip6: 'fe80::134a:1e43:abc5:d413',
    ip6subnet: 'ffff:ffff:ffff:ffff::',
    mac: 'xx:xx:xx:xx:xx:xx',
    internal: false,
    virtual: false,
    operstate: 'up',
    type: 'wired',
    duplex: 'full',
    mtu: 1500,
    speed: 1000,
    dhcp: true,
    dnsSuffix: '',
    ieee8021xAuth: '',
    ieee8021xState: '',
    carrierChanges: 0
  }, ...
]
Get Default Interface only ​

With the 'default' parameter this function returns only the default interface

const si = require('@ambicuity/systeminspector');
si.networkInterfaces('default').then(data => console.log(data));
json
{
  iface: 'en0',
  ifaceName: 'en0',
  default: true,
  ip4: '192.168.0.27',
  ip4subnet: '255.255.255.0',
  ip6: 'fe80::134a:1e43:abc5:d413',
  ip6subnet: 'ffff:ffff:ffff:ffff::',
  mac: 'xx:xx:xx:xx:xx:xx',
  internal: false,
  virtual: false,
  operstate: 'up',
  type: 'wired',
  duplex: 'full',
  mtu: 1500,
  speed: 1000,
  dhcp: true,
  dnsSuffix: '',
  ieee8021xAuth: '',
  ieee8021xState: '',
  carrierChanges: 0
}
Example ​
const si = require('@ambicuity/systeminspector');
si.networkInterfaceDefault().then(data => console.log(data));
json
eth0
Example ​
const si = require('@ambicuity/systeminspector');
si.networkGatewayDefault().then(data => console.log(data));
json
192.168.0.1
Example ​
const si = require('@ambicuity/systeminspector');
setInterval(function() {
  si.networkStats().then(data => {
    console.log(data);
  })
}, 1000)
json
[
  {                                 // first call
    iface: 'en0',
    operstate: 'up',
    rx_bytes: 1752866207,
    rx_dropped: 0,
    rx_errors: 0,
    tx_bytes: 180934681,
    tx_dropped: 0,
    tx_errors: 0,
    rx_sec: null,
    tx_sec: null,
    ms: 0
  }
]
[
  {                                 // second call
    iface: 'en0',
    operstate: 'up',
    rx_bytes: 1752866822,
    rx_dropped: 0,
    rx_errors: 0,
    tx_bytes: 180939820,
    tx_dropped: 0,
    tx_errors: 0,
    rx_sec: 624.3654822335026,
    tx_sec: 5217.258883248731,
    ms: 985
  }
]...
Example ​
const si = require('@ambicuity/systeminspector');
si.networkConnections().then(data => console.log(data));
json
[
  {
    protocol: 'tcp4',
    localAddress: '192.168.0.27',
    localPort: '55788',
    peerAddress: '163.128.xxx.xxx',
    peerPort: '443',
    state: 'CLOSE_WAIT',
    pid: 702,
    process: ''
  },
  {
    protocol: 'tcp4',
    localAddress: '192.168.0.27',
    localPort: '55761',
    peerAddress: '148.253.xxx.xxx',
    peerPort: '22',
    state: 'ESTABLISHED',
    pid: 7267,
    process: ''
  },
  ...
]
Example ​
const si = require('@ambicuity/systeminspector');
si.inetChecksite('google.com').then(data => console.log(data));
json
{
  url: 'google.com',
  ok: true,
  status: 301,
  ms: 82
}
Example ​
const si = require('@ambicuity/systeminspector');
si.inetLatency().then(data => console.log(data));
json
13.484
// Example with given host IP address
const si = require('@ambicuity/systeminspector');
si.inetLatency('216.58.207.142').then(data => console.log(data));
json
11.291
Inspector AI
SystemInspector AI initialized. How can I help you query your hardware?