【问题标题】:Get local network ip addresses on NodeJS server在 NodeJS 服务器上获取本地网络 IP 地址
【发布时间】:2021-01-14 09:25:09
【问题描述】:

我需要获取谁向 nodeJS 服务器发出请求的信息(私有 IP 地址和 MAC)。这是在 Intranet 上运行的,所以我想我可以通过某种方式获取 MAC 数据,而来自os.networkinterfaces() or req.connection 的 ip 对我没有用,因为它们分别给了我服务器或公共 ip。

【问题讨论】:

    标签: node.js ip-address


    【解决方案1】:

    local-devices

    Npm

    npm install local-devices
    

    示例

    // Using a transpiler
    import find from 'local-devices'
    // Without using a transpiler
    const find = require('local-devices');
     
    // Find all local network devices.
    find().then(devices => {
      devices /*
      [
        { name: '?', ip: '192.168.0.10', mac: '...' },
        { name: '...', ip: '192.168.0.17', mac: '...' },
        { name: '...', ip: '192.168.0.21', mac: '...' },
        { name: '...', ip: '192.168.0.22', mac: '...' }
      ]
      */
    })
    

    【讨论】:

    • 是的,我实际上最终使用了这个和 req.connection 的组合,如果它是从 Intranet 制作的,结果证明它确实提供了私有 IP;然后我将此IP链接到find()。然后,获取设备MAC。谢谢你的回答!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-22
    • 2012-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多