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 :
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 :
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].
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.
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 ...
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With