ping vs tcp-ping
Network Ping Libraries Comparison
1 Year
pingtcp-ping
What's Network Ping Libraries?

Network ping libraries are used to test the reachability of hosts on a network and measure the round-trip time for messages sent from the originating host to a destination computer. These libraries provide developers with the ability to perform ICMP echo requests (ping) and TCP connection tests to assess network connectivity and performance. They are essential tools for diagnosing network issues, monitoring server availability, and ensuring that applications can reliably communicate over the network.

Package Weekly Downloads Trend
Github Stars Ranking
Stat Detail
Package
Downloads
Stars
Size
Issues
Publish
License
ping67,71533642.9 kB82 years agoMIT
tcp-ping8,791124-511 years agoMIT
Feature Comparison: ping vs tcp-ping

Protocol Support

  • ping:

    The 'ping' package primarily supports ICMP protocol for sending echo requests, which is the standard method for checking host reachability. It is ideal for basic network diagnostics but may be blocked by firewalls, limiting its effectiveness in some environments.

  • tcp-ping:

    The 'tcp-ping' package uses TCP connections to check service availability on specific ports. This method is less likely to be blocked by firewalls and can provide more accurate results for services that rely on TCP, making it suitable for testing web servers and other TCP-based services.

Ease of Use

  • ping:

    The 'ping' package is designed for simplicity and ease of use, allowing developers to quickly implement ping functionality with minimal configuration. It provides straightforward methods for sending pings and receiving responses, making it accessible even for those with limited networking knowledge.

  • tcp-ping:

    The 'tcp-ping' package is also user-friendly, offering a simple API for initiating TCP pings. However, it may require a better understanding of TCP connections and ports, which could add a slight learning curve for developers unfamiliar with these concepts.

Performance

  • ping:

    The performance of the 'ping' package is generally good for basic ICMP requests, but it can be affected by network latency and the responsiveness of the target host. It is suitable for quick checks but may not provide detailed performance metrics.

  • tcp-ping:

    The 'tcp-ping' package can offer more reliable performance metrics for services running over TCP, as it measures the time taken to establish a connection. This can be particularly useful for monitoring service availability and responsiveness in production environments.

Error Handling

  • ping:

    The 'ping' package includes basic error handling for scenarios where the target host is unreachable or times out. However, it may not provide extensive details on the nature of the errors encountered, which could limit troubleshooting capabilities.

  • tcp-ping:

    The 'tcp-ping' package typically offers more detailed error handling related to TCP connections, allowing developers to identify issues such as connection refusals or timeouts. This can aid in diagnosing specific service-related problems.

Use Cases

  • ping:

    The 'ping' package is best suited for general network diagnostics, such as checking if a server is online or measuring round-trip times for network latency assessments. It is ideal for simple monitoring scripts and basic connectivity checks.

  • tcp-ping:

    The 'tcp-ping' package is particularly useful for applications that need to monitor the availability of specific services on a network, such as web servers, databases, or any TCP-based service. It is well-suited for more complex monitoring solutions that require service-level checks.

How to Choose: ping vs tcp-ping
  • ping:

    Choose 'ping' if you need a straightforward implementation for sending ICMP echo requests to test host availability. It is simple to use and provides basic functionality for network diagnostics without additional overhead.

  • tcp-ping:

    Choose 'tcp-ping' if you require a TCP-based approach to check the availability of services on specific ports. This is particularly useful for applications that need to verify the responsiveness of services like web servers or databases, as it can provide more relevant results for service availability.

README for ping

NODE-PING Build Status

a ping wrapper for nodejs

@last-modified: 2020-12-26

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 windows
    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 windows
    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 windows
    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 {number} packetSize - Specifies the number of data bytes to be sent
                                   Default: Linux / MAC: 56 Bytes, Windows: 32 Bytes
 * @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} inputHost - The input IP address or HOST
 * @param {string} host - Parsed host from system command's output
 * @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} packetLoss - Packet Losses in percent (100% -> "100.000")
 * @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.

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

  • For questions regarding to the defintions of host, inputHost, and numeric_host, please checkout discussions in #133

Contributing

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

Contributors

Made with contrib.rocks.