Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ping Continuously Like Ping in CMD Using Node.Js

I want use node.js to ping host in local network. Here is my code :

var ping = require('ping');

var host2 = ['192.168.0.1', '192.168.1.2', '192.168.2.3'];

host2.forEach(function(host){
    ping.sys.probe(host, function(active){
        var info = active ? 'IP ' + host + ' = Active' : 'IP ' + host + ' = Non-Active';
        console.log(info);
    });
});

This code only run (ping) once. All I want is to ping continuously. Is this possible with node.js?

EDIT : When I run the code :

enter image description here

EDIT 2 : When using setInterval / setTimeout :

Code :

var ping = require('ping');

var host2 = ['192.168.0.1', '192.168.1.2', '192.168.2.3'];

host2.forEach(function(host){
    ping.sys.probe(host, function tes(active){
        var info = active ? 'IP ' + host + ' = Active' : 'IP ' + host + ' = Non-Active';
        console.log(info);
    });
    setInterval(tes, 2000);
});

Result :

enter image description here

like image 719
Zhumpex Avatar asked May 26 '17 04:05

Zhumpex


People also ask

How do I run a continuous ping in cmd?

Continuous ping in Windows 7, 8, and 10 Step 1: Open the Windows command prompt. One way of doing this is by entering the key combination Windows + R and enter the command CMD. Step 2: Enter the command line ping with the -t option and any address and confirm by clicking [Enter].

How do I make my ping continuous?

Use the command "ping 192.168. 1.101 -t" to initiate a continuous ping. Again, replace the IP address with one specific to your device as needed. The -t can be placed before or after the IP address.

What is continuous Ping in Windows?

The continuous ping command lets you continue to send more packets over time to assess the network connection continually. Here’s how to set up a continuous ping in Windows. Step 1: Start by opening a Windows command prompt by holding the Windows and R key and entering CMD into the run box. Step 2: Enter the ping command as well as the -t ...

How does Windows Ping work?

Windows responds to the command by running an endless ping on the chosen IP address. Ping will add an entry to the standard output each time that a packet returns home to the host computer.

How do I end a ping command in Linux?

You can end the command by ending the command line program, which you do by holding Control and pressing C. The program will show a summary of ping statistics and a conclusion when you end the command and stop pinging the target computer. You will get these results as a terminal output when you use the Control + C command to end the ping.

How many times does Ping run by default in Linux?

The ping command runs four times by default on Windows, so you’ll need to include a little bit of extra code to make it run a continuous ping. Linux runs an endless loop of ping by default. For Linux, you need to define a set number of pings rather than having to set it to run continuously.


1 Answers

Well the obvious answer is this:

var ping = require('ping');

var host2 = ['192.168.0.1', '192.168.1.2', '192.168.2.3'];

var frequency = 1000; //1 second

host2.forEach(function(host){
    setInterval(function() {
        ping.sys.probe(host, function(active){
            var info = active ? 'IP ' + host + ' = Active' : 'IP ' + host + ' = Non-Active';
            console.log(info);
        });
    }, frequency);
});

That will ping each host in the host2 array once every second.

like image 160
Clonkex Avatar answered Sep 24 '22 02:09

Clonkex