跳到主要内容
CNode

node检测一个ip是否ping通,可否这样写呢?

社区
Aadam1985发布于 12 年前最后回复 12 年前
4166880
    var client = net.createConnection(ip, port);
    client.on('connect', function () {
        success();
    });
    client.on('error', function(e) {
        fail();
    });
查看回复

回复 (4)

M
meteormatt#412 年前

net-ping模块挺不错的.

var ping = require ("net-ping");
var session = ping.createSession ();
session.pingHost (target, function (error, target) {
    if (error)
        console.log (target + ": " + error.toString ());
    else
        console.log (target + ": Alive");
});
Y
yaochun#312 年前

我一般是这样做的:

调用request库,然后

request.head('http://' + ip, function(error, response, body){ if (error) { //...

} else {
    //...

}

});

A
adam1985#112 年前

没人问题呢

参与回复

登录后即可参与回复。登录