简体   繁体   English

如何在原生 NodeJS 中执行 ICMP ping

[英]How to do ICMP ping in native NodeJS

How about comrades, a few days ago I was trying to ICMP ping to an IP from NodeJS.同志们,前几天我在尝试从NodeJS ICMP ping 一个IP。 But as is the rule in the forum, I do not come with empty hands, I have come across some posts even on this website talking about how to do this, but none of them convinces me.但是按照论坛的规则,我不会空手而归,我什至在这个网站上也遇到过一些帖子讨论如何做到这一点,但没有一个能让我信服。

One of the immovable parameters of my project is to avoid the use of NPM / Node-GYP.我的项目不可动摇的参数之一是避免使用 NPM/Node-GYP。 Therefore the option of using raw-sockets is discarded (unless you can use C ++ code in NodeJS without using things external to node itself).因此放弃使用原始套接字的选项(除非您可以在 NodeJS 中使用 C++ 代码而不使用节点本身外部的东西)。

Also tried (and implemented) the option of using system commands, here you can see my valid implementation for Linux and Windows (I have not tried it on Mac but I am almost sure it works)还尝试(并实现)了使用系统命令的选项,在这里你可以看到我对 Linux 和 Windows 的有效实现(我没有在 Mac 上尝试过,但我几乎确定它可以工作)

'use strict';

import { execSync } from "child_process";

class Ping {
    #stdoutToMS (stdout) {
        let res;

        let a = stdout.split('=');
        for (let i = 0; i < a.length; i++) {
            res = a[i].split("ms");
        }

        return ~~res[0].split('/')[0].trim();
    }

    ping (host, timeout = 5000) {
        let mstout = timeout / 1000;
        let stdout;

        try {
            if (process.platform === "win32") {
                stdout = execSync("ping -n 1 -l 1 -w " + timeout + ' ' + host);
            } else {
                stdout = execSync("ping -c 1 -s 16 -W " + mstout + ' ' + host);
            }
        } catch (err) {
            return false;
        }

        return this.#stdoutToMS(stdout.toString());
    }
};

export default Ping;

If anyone has any ideas on how to do this natively in node without using external software, I'd be very grateful if you would tell me.如果有人对如何在不使用外部软件的情况下在 node 中本地执行此操作有任何想法,如果您能告诉我,我将不胜感激。

It does not appear you can access a ping equivalent from nodejs without some external code.如果没有一些外部代码,您似乎无法从 nodejs 访问 ping 等效项。

Since ping uses the ICMP protocol to do what it does (and it has to use that protocol because it's trying to contact an endpoint that is listening for that) and there is no implementation of the ICMP protocol in nodejs, your only option would be to create your own implementation of the ICMP protocol entirely in nodejs code by getting access to a RAW OS socket and implementing the protocol yourself.由于 ping 使用 ICMP 协议来完成它所做的事情(并且它必须使用该协议,因为它正在尝试联系正在侦听该协议的端点)并且在 nodejs 中没有实现 ICMP 协议,因此您唯一的选择是通过访问 RAW OS 套接字并自己实现协议,完全在 nodejs 代码中创建自己的 ICMP 协议实现。 I am not aware of any built-in ability to get a RAW socket in plain nodejs (no external software).我不知道在普通 nodejs(没有外部软件)中获得 RAW 套接字的任何内置功能。

The only examples of ICMP implementation I could find in nodejs ALL use external code to create access to a raw socket.我可以在 nodejs 中找到的唯一 ICMP 实现示例都使用外部代码来创建对原始套接字的访问。 That seems to be a verification that there is no other way to do it.这似乎证明了没有其他方法可以做到这一点。

There is a module here that exposes a raw socket, but it uses some native code to implement that.有一个模块这里暴露原始套接字,但它使用一些本地代码来实现这一点。 You can examine its implementation to see what it's doing.您可以检查它的实现以了解它在做什么。

There's also this library which exposes a RAW socket from libuv to be used within nodejs, but it also uses some of it's own native code.还有这个库,它从 libuv 公开一个 RAW 套接字以在 nodejs 中使用,但它也使用了一些它自己的本机代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM