sebhildebrandt / systeminformation

System Information Library for Node.JS
MIT License
2.69k stars 305 forks source link

This might be a feature already: Getting the IP number (default network interface) of a system #647

Closed GerritKuilder closed 2 years ago

GerritKuilder commented 2 years ago

Is your feature request related to a problem? Please describe. It looks like I can only get the default ip number of a system through networkInterfaceDefault -> networkInterfaces and find the right one

Describe the solution you'd like

as the hostname is set in osInfo, it would be nice if it was available there

Describe alternatives you've considered https://www.npmjs.com/package/ip

sebhildebrandt commented 2 years ago

@GerritKuilder thank you for supporting the project!! I will have a look at it later today.

sebhildebrandt commented 2 years ago

@GerritKuilder I made a first implementation but I am not sure about a good name for the function ... currently I have something like:

si.networkInterfaceIP()

which returns

{
  iface: 'en0',
  ip4: '192.168.1.102',
  ip6: 'fd08:2a3e:62b8:2416:18d2:536a:823b:3718'
}

What do you think?

GerritKuilder commented 2 years ago

Sounds good, but rather than a new function what about adding it to si.osInfo() as this already has the hostname too? Or add the hostname to si.networkInterfaceIP? And then maybe loose the IP? si.networkInterface() This does identify the machine uniquely by netWorkInterface.

I am a bit too tired of thinking semantics about what is the best name is or if osInfo is a good place too.

I will have another bit of thinking tomorrow.

Thanks for your efforts sofar.

sebhildebrandt commented 2 years ago

@GerritKuilder thank you! Appreciate all your ideas! Take your time.

GerritKuilder commented 2 years ago

Morning,

Not being a specialist in Discovery/cmdb [1] but running into it on a daily basis I would say that the mac address and hostname would be very useful addition in the si.networkInterface() (so without the IP). This would make it very easy to identify a device.

[1] (https://docs.servicenow.com/bundle/rome-it-operations-management/page/product/discovery/concept/c_DiscoveryIdentifiers.html)

What do you think?

Rgeards,

Gerrit Kuilder

sebhildebrandt commented 2 years ago

@GerritKuilder Hmmmm ... feels like this information should be in networkInterfaces(). Would it make sense to just add a default: true/false property in the networkInterfaces() array? And then a simple filter would then bring up the wanted information (IP address, mac address, ...)

What do you think?

GerritKuilder commented 2 years ago

Hi Seb,

this is what I currently have:

  const defaultIf = await si.networkInterfaceDefault()
  const nwInterfaces = await si.networkInterfaces()
  const myip = nwInterfaces.find(x => x.ifaceName === defaultIf)

I think it would be more userfriendly if we could do

const mydefault = await si.networkInterfaces('defaultInterFace')

Which would return the interface where default = true returning the required info with si.networkInterfaceDefault would be a breaking change, so that's not really an option.

Regards,

Gerrit Kuilder

sebhildebrandt commented 2 years ago

@GerritKuilder this is a nice idea ... I will think of implementing it this way. Maybe I would make the parameter more flexible to allow several ways to get just the default interface e.g.

mydefault = await si.networkInterfaces('defaultInterFace')
mydefault = await si.networkInterfaces('default')
mydefault = await si.networkInterfaces(true)
mydefault = await si.networkInterfaces({default: true})

Would this make sense?

GerritKuilder commented 2 years ago

perfect

sebhildebrandt commented 2 years ago

@GerritKuilder Version 5.11.0 just released, can you check it on your side?

I only implemmented the following parameter options:

mydefault = await si.networkInterfaces('defaultInterFace')
mydefault = await si.networkInterfaces('default')

which returns only the default interface (as an object). If this works on your side, I will close the issue.

GerritKuilder commented 2 years ago
const si = require('systeminformation')
start()
async function start() {
    mydefault = await si.networkInterfaces('defaultInterFace')

    console.log(mydefault)
    mydefault = await si.networkInterfaces('default')
    console.log(mydefault)
}

Result:

 node index.js
{
  iface: 'enp2s0',
  ifaceName: 'enp2s0',
  default: true,
  ip4: '192.168.2.17',
  ip4subnet: '255.255.255.0',
  ip6: 'fe80::7e10:c9ff:fe50:1df9',
  ip6subnet: 'ffff:ffff:ffff:ffff::',
  mac: '7c:10:c9:50:1d:f9',
  internal: false,
  virtual: false,
  operstate: 'up',
  type: 'wired',
  duplex: 'full',
  mtu: 1500,
  speed: 1000,
  dhcp: false,
  dnsSuffix: 'Unknown',
  ieee8021xAuth: 'Not defined',
  ieee8021xState: 'Disabled',
  carrierChanges: 2
}
{
  iface: 'enp2s0',
  ifaceName: 'enp2s0',
  default: true,
  ip4: '192.168.2.17',
  ip4subnet: '255.255.255.0',
  ip6: 'fe80::7e10:c9ff:fe50:1df9',
  ip6subnet: 'ffff:ffff:ffff:ffff::',
  mac: '7c:10:c9:50:1d:f9',
  internal: false,
  virtual: false,
  operstate: 'up',
  type: 'wired',
  duplex: 'full',
  mtu: 1500,
  speed: 1000,
  dhcp: false,
  dnsSuffix: 'Unknown',
  ieee8021xAuth: 'Not defined',
  ieee8021xState: 'Disabled',
  carrierChanges: 2
}
GerritKuilder commented 2 years ago

Works perfect, Also tested on my raspberry pi. The first one is an asus pn51.

Let me know if you want more details.

sebhildebrandt commented 2 years ago

@GerritKuilder thank you. closing it for now.

For the next major release I will make a brewing change here ... then networkInterfaceDefault() will no longer give just a string, it will then give back what we now have with networkInterfaces('default'). I guess this makes more sense then.