【问题标题】:How to fetch ip and mac addresses of all the devices connected to another LAN using nodejs?如何使用nodejs获取连接到另一个局域网的所有设备的ip和mac地址?
【发布时间】:2020-10-28 16:54:12
【问题描述】:

如何使用 nodejs 或任何 powershell 脚本获取连接到同一组织中不同 LAN 的所有设备的 ip 和 mac 地址?

【问题讨论】:

    标签: node.js windows powershell ip mac-address


    【解决方案1】:

    在这里你可以使用child_process找到

    类似的东西。

    const { exec } = require("child_process");
    
    function os_func() {
        this.execCommand = function(cmd, callback) {
            exec(cmd, (error, stdout, stderr) => {
                if (error) {
                    console.error(`exec error: ${error}`);
                    return;
                }
    
                callback(stdout);
            });
        }
    }
    
    var os = new os_func();
    os.execCommand('arp -a', function (returnvalue) {
        console.log(returnvalue)
    });
    

    或者你可以使用arp-scan 安装 sudo apt-get install arp-scan

    只需更改命令sudo arp-scan -l

    这将为您提供所有信息

    【讨论】:

    • 嗨 Mayank,感谢您的回答,但在执行此代码后,我能够获取连接到我的 LAN 的 ip,是否可以获取连接到另一个 LAN 的 ip 地址(设备有不同的默认网关)?
    • 如何在windows中安装arp-scan包?在 Debian GNU/Linux 操作系统中,我们可以使用 sudo apt-get install arp-scan 安装 arp-scan,那么我们如何在 Windows 中安装它呢?......我尝试安装“npm i arpscan new”包和执行此代码但收到错误“exec 不是函数”
    • 您可以在窗口中签入。 arp-scan 将有其他选择,您只需更改 cmd。代码将返回一切。
    【解决方案2】:

    您可以在任何 Windows 机器上运行此 PowerShell 脚本。将您的主机名放入主机名文本中。只要您能 ping 通您正在运行脚本的主机名。


    $hostname = get-content -path "C:\temp\hostname.txt"
    
    Foreach ($computer in $hostname) {
    
       $ping =  Test-Connection -Name $computer -count 2 -Quiet 
    
       If ($ping -eq $true) {
    
        $prop = Get-Ciminstance -ClassName Win32_NetorkAdapterConfiguration -ComputerName $computer | select IPAddress, MacAddress, PSComputerName
        Write-Host $prop
    
       }
    
    }
    

    【讨论】:

    • 您好,您绝对可以为自己的答案编写解决方案,错误您可以将其作为问题发布,然后将您的解决方案也放入其中。这里还有一篇关于如何格式化代码的帖子meta.stackoverflow.com/questions/251361/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-26
    • 2012-11-06
    • 1970-01-01
    • 2012-11-27
    • 2015-10-30
    相关资源
    最近更新 更多