How to “Ping” from a Node.js app?

前端 未结 6 1196
囚心锁ツ
囚心锁ツ 2020-12-14 05:42

I want to ping a server from my node.js app.

Is that doable?

Thanks

6条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-14 06:23

    Doing ping(programmable) requires root privileges because it requires raws sockets which require root access. You could perform ping following Gradwohl's snippet, but keep in mind that you are forking a new process which is expensive(relatively). If you don't need to do it a lot(concurrency) this will definitely work :)

    To do it in node.js(only) without forking process I think you have a couple of options, which are both hard to implement :()

    1. rewrite this ping python library to node.js and then run program as root user.
    2. write a c++ extension/addon for node.js using asio c++ library for node.js. It also has a couple of examples how to do icmp ping.

    Not (only) using node.js:

    1. use python ping library ran as root and communicate with node.js instance via redis. => EASIEST to implement.(hardly any work but I think rather fast :))
    2. write c(++) code again using asio c++ but instead of writing node.js extension communicate via hiredis with node.js which also uses redis.

    As a side-note how to use redis on node.js:

    • install redis from http://redis.io
    • install the fast node_redis library

提交回复
热议问题