I want to ping a serv开发者_如何学运维er from my node.js app.
Is that doable?
Thanks
You could use exec
to call the system ping command
var exec = require('child_process').exec;
exec("ping -c 3 localhost", function (err, stdout, stderr) {
console.log(stdout);
});
node-net-ping is an awesome module that uses raw sockets.
And, if you are looking for only raw sockets, the same developer has a module for that too: node-raw-socket.
I'm author of ping-wrapper.
It spawn ping and you can listen to events immediately. If process quits, it will be spawn automatically.
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 :()
- rewrite this ping python library to node.js and then run program as root user.
- 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:
- 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 :))
- 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
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. ;-)
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
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 :)
https://github.com/bscarvell/pingwrap
It is working fine,
const { exec } = require("child_process");
exec("ping -c 10 google.com", (error, stdout, stderr) => {
if (error) {
console.log(`error: ${error.message}`);
return;
}
if (stderr) {
console.log(`stderr: ${stderr}`);
return;
}
console.log(`stdout: ${stdout}`);
});
There are a few ways to do this. The easiest is to use the ping
command. You can do this by using the child_process
module.
const { spawnSync } = require('child_process');
const os = require('os');
// Windows: ping -n 1 HOSTNAME
// Linux: ping -c 1 HOSTNAME
function pingHostname(hostname) {
const result = spawnSync('ping', os.platform() === 'win32' ? ['-n', '1', hostname] : ['-c', '1', hostname]);
return result.stdout.toString().includes('Reply from');
}
Or you can use the net
module to ping a server.
const net = require('net');
async function pingHostname(hostname) {
return new Promise((resolve, reject) => {
const socket = net.createConnection(80, hostname);
socket.setTimeout(3000);
socket.on('connect', () => {
socket.end();
resolve(true);
});
socket.on('timeout', () => {
socket.destroy();
resolve(false);
});
socket.on('error', () => {
socket.destroy();
resolve(false);
});
});
}
References
- net
- child_process
- Ping with Option -c on Windows
精彩评论