GithubHelp home page GithubHelp logo

carloswalterbr / node-ping Goto Github PK

View Code? Open in Web Editor NEW

This project forked from danielzzz/node-ping

0.0 1.0 0.0 298 KB

a poor man's ping library (using udp scanning) for node

License: MIT License

JavaScript 100.00%

node-ping's Introduction

NODE-PING

a ping wrapper for nodejs

@last-modified: 2019-06-07

LICENSE MIT

(C) Daniel Zelisko

http://github.com/danielzzz/node-ping

DESCRIPTION

node-ping is a simple wrapper for the system ping utility

INSTALLATION

npm install ping

USAGE

Below are examples extracted from examples

Tradition calls

var ping = require('ping');

var hosts = ['192.168.1.1', 'google.com', 'yahoo.com'];
hosts.forEach(function(host){
    ping.sys.probe(host, function(isAlive){
        var msg = isAlive ? 'host ' + host + ' is alive' : 'host ' + host + ' is dead';
        console.log(msg);
    });
});

Tradition calls with configuration

var cfg = {
    timeout: 10,
    // WARNING: -i 2 may not work in other platform like window
    extra: ['-i', '2'],
};

hosts.forEach(function(host){
    ping.sys.probe(host, function(isAlive){
        var msg = isAlive ? 'host ' + host + ' is alive' : 'host ' + host + ' is dead';
        console.log(msg);
    }, cfg);
});

Promise wrapper

var ping = require('ping');

var hosts = ['192.168.1.1', 'google.com', 'yahoo.com'];

hosts.forEach(function (host) {
    ping.promise.probe(host)
        .then(function (res) {
            console.log(res);
        });
});

Promise Wrapper with configurable ping options

hosts.forEach(function (host) {
    // WARNING: -i 2 argument may not work in other platform like window
    ping.promise.probe(host, {
        timeout: 10,
        extra: ['-i', '2'],
    }).then(function (res) {
        console.log(res);
    });
});

Async-Await

var ping = require('ping');

var hosts = ['192.168.1.1', 'google.com', 'yahoo.com'];

for(let host of hosts){
    let res = await ping.promise.probe(host);
    console.log(res);
}

});

Async-Await with configurable ping options

var ping = require('ping');

var hosts = ['192.168.1.1', 'google.com', 'yahoo.com'];

for(let host of hosts){
     // WARNING: -i 2 argument may not work in other platform like window
    let res = await ping.promise.probe(host, {
           timeout: 10,
           extra: ['-i', '2'],
       });
    console.log(res);
}

});

Support configuration

Below is the possible configuration

/**
 * Cross platform config representation
 * @typedef {Object} PingConfig
 * @property {boolean} numeric - Map IP address to hostname or not
 * @property {number} timeout - Timeout in seconds for each ping request.
 * Behaviour varies between platforms. Check platform ping documentation for more information.
 * @property {number} deadline - Specify a timeout, in seconds, before ping exits regardless of
              how many packets have been sent or received. In this case ping
              does not stop after count packet are sent, it waits either for
              deadline expire or until count probes are answered or for some
              error notification from network. This option is only available on linux and mac.
 * @property {number} min_reply - Exit after sending number of ECHO_REQUEST
 * @property {boolean} v6 - Ping via ipv6 or not. Default is false
 * @property {string} sourceAddr - source address for sending the ping
 * @property {string[]} extra - Optional options does not provided
 */

Output specification

  • For callback based implementation:
/**
 * Callback after probing given host
 * @callback probeCallback
 * @param {boolean} isAlive - Whether target is alive or not
 * @param {Object} error - Null if no error occurs
 */
  • For promise based implementation
/**
 * Parsed response
 * @typedef {object} PingResponse
 * @param {string} host - The input IP address or HOST
 * @param {string} numeric_host - Target IP address
 * @param {boolean} alive - True for existed host
 * @param {string} output - Raw stdout from system ping
 * @param {number} time - Time (float) in ms for first successful ping response
 * @param {Array} times - Array of Time (float) in ms for each ping response
 * @param {string} min - Minimum time for collection records
 * @param {string} max - Maximum time for collection records
 * @param {string} avg - Average time for collection records
 * @param {string} stddev - Standard deviation time for collected records
 */

Note

  • Since ping in this module relies on the ping from underlying platform, arguments in PingConfig.extra will definitely be varied across different platforms.

  • However, numeric, timeout and min_reply have been abstracted. Values for them are expected to be cross platform.

  • By setting numeric, timeout or min_reply to false, you can run ping without corresponding arguments.

  • For LINUX users, since we have enable shell option in child_process.spawn, make sure arguments you are passing in into this library are sanitized. Otherwise, any input containing shell metacharacters may be used to trigger arbitrary command

FAQ

  • It does not work with busybox's ping implemetation #89

Try to install package iputils. For example, running apk add iputils

  • For questions regarding to the implementation of timeout, and deadline, please checkout discussions in #101

Contributing

Before opening a pull request please make sure your changes follow the contribution guidelines.

node-ping's People

Contributors

mondwan avatar danielzzz avatar sa-linetco avatar woicha avatar dougluce avatar nahanil avatar jritsema avatar weihua44 avatar germanbluefox avatar thib3113 avatar reminjp avatar yogeshkathayat avatar 0xflotus avatar mabukar avatar photuris avatar bryant1410 avatar svrooij avatar fuzzyma avatar andrewf403 avatar esdmr avatar tablekat avatar kr1sp1n avatar lhyunh avatar microacup avatar mrmuppet avatar ubcent avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.