javascriptnode.jselectronipping

How to ping IP in JS or Node.js


I'm creating an Electron app that checks the availability of an IP using the fetch API with domains. If I try with a local network IP like 192.168.1.190, the fetch API fails. I found library ping but it works like fetch and thus I'm getting the same rejection for local IPs. How can I actually ping with JS?

PS

An IP like 8.8.8.8 is also rejected by the fetch API.


Solution

  • if you use nodejs, you could use exec to call system ping command.

    const exec = require('child_process').exec;
    function process(error, stdout, stderr) {
        //TODO: process result or logging.
    }
    exec("ping -c 3 localhost", process);
    

    Or you can use 3rd party libraries. https://github.com/nospaceships/node-net-ping