如何从 Node.js 应用程序“Ping”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4737130/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to "Ping" from a Node.js app?
提问by donald
I want to ping a server from my node.js app.
我想从我的 node.js 应用程序 ping 服务器。
Is that doable?
那可行吗?
Thanks
谢谢
采纳答案by Nikolaus Gradwohl
You could use execto call the system ping command
您可以exec用来调用系统 ping 命令
var sys = require('sys')
var exec = require('child_process').exec;
function puts(error, stdout, stderr) { sys.puts(stdout) }
exec("ping -c 3 localhost", puts);
回答by umutm
node-net-pingis an awesome module that uses raw sockets.
node-net-ping是一个很棒的模块,它使用原始套接字。
And, if you are looking for only raw sockets, the same developer has a module for that too: node-raw-socket.
而且,如果您只寻找原始套接字,那么同一个开发人员也有一个模块:node-raw-socket。
回答by langpavel
I'm author of ping-wrapper.
我是ping-wrapper 的作者。
It spawn ping and you can listen to events immediately. If process quits, it will be spawn automatically.
它产生 ping,您可以立即收听事件。如果进程退出,它将自动生成。
回答by Alfred
Doing ping(programmable) requires root privileges because it requires raws sockets which require root access. You could perform ping following Gradwohl'ssnippet, 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 :)
执行 ping(programmable) 需要 root 权限,因为它需要需要 root 访问权限的原始套接字。您可以按照Gradwohl 的代码段执行 ping ,但请记住,您正在分叉一个昂贵(相对)的新进程。如果你不需要做很多(并发)这肯定会工作:)
To do it in node.js(only) without forking process I think you have a couple of options, which are both hard to implement :()
要在没有分叉过程的 node.js(only) 中做到这一点,我认为你有几个选项,这两个选项都很难实现:()
- rewrite this ping python libraryto node.js and then run program as root user.
- write a c++ extension/addon for node.jsusing asio c++ libraryfor node.js. It also has a couple of examples how to do icmp ping.
- 将此ping python 库重写为 node.js,然后以 root 用户身份运行程序。
- 使用用于 node.js 的asio c++ 库为 node.js编写c++ 扩展/插件。它还提供了几个如何执行icmp ping的示例。
Not (only) using node.js:
不(仅)使用 node.js:
- use python ping libraryran as root and communicate with node.js instance via redis. => EASIEST to implement.(hardly any work but I think rather fast :))
- write c(++) code again using asio c++ but instead of writing node.js extension communicate via hirediswith node.js which also uses redis.
- 使用以 root 身份运行的python ping 库并通过 redis 与 node.js 实例通信。=>最容易实现。(几乎没有任何工作,但我认为相当快:))
- 使用 asio c++ 再次编写 c(++) 代码,而不是编写 node.js 扩展,通过hiredis与同样使用redis 的node.js 进行通信。
As a side-note how to use redis on node.js:
作为旁注如何在 node.js 上使用 redis:
- install redis from http://redis.io
- install the fast node_redis library
- 从http://redis.io安装 redis
- 安装快速node_redis 库
回答by Menztrual
I know this answer has been answered quite a while ago, but for people who are looking for the same answer, I have written a module on github to try simplify it more :)
我知道这个答案很久以前就有人回答了,但是对于正在寻找相同答案的人,我已经在 github 上编写了一个模块来尝试进一步简化它:)
回答by reconbot
You can also use my nodejs ping wrapper yaping. One day we will get raw sockets in nodejs and we'll be able to make our own ping packets and lie about our response times. ;-)
您也可以使用我的 nodejs ping 包装器yaping。有一天,我们将在 nodejs 中获得原始套接字,我们将能够制作自己的 ping 数据包并谎报我们的响应时间。;-)
This simple function should
这个简单的函数应该
- do dns lookups
- ping once
- timeout after 10 seconds
- communicate all the well though out error codes that ping provides
- spawn a child processes out of wedlock
- 做 dns 查询
- ping一次
- 10 秒后超时
- 通过 ping 提供的错误代码进行沟通
- 非婚生子进程

