danielzzz / node-ping

a poor man's ping library (using udp scanning) for node
MIT License
336 stars 105 forks source link

NodeJs implementation generates CMD window when ping #125

Closed CVidalAST closed 3 years ago

CVidalAST commented 4 years ago

I’m using ping in a nodejs application, this one I’m running with PM2. When the application performs the ping process a CMD console window is opened using the Windows TCP/IP Ping utility, I want to know if there is a way to hide this and that it does not appear. Attachment captures the window that appears when the NodeJS application pings. In addition, I attach application code in NodeJS.

Capture_ping

`var mqtt = require('mqtt'); var ping = require('ping');

var servidor_nodo = ['192.10.253.10']; var estacion_clima = ['192.10.253.11']; var gateway_lora = ['192.10.253.12']; var router = ['192.10.253.1']; var satelital = ['192.168.0.1'];

//CREDENCIALES MQTT var options = { port: 1883, host: 'localhost', clientId: 'lcd_ping_nodejs' + Math.round(Math.random() (0- 10000) -1) , username: '', password: '', keepalive: 60, reconnectPeriod: 1000, protocolId: 'MQIsdp', protocolVersion: 3, clean: true, encoding: 'utf8' };

var client = mqtt.connect("mqtt://localhost", options);

//SE REALIZA LA CONEXION client.on('connect', function () { console.log("Conexión MQTT Exitosa!"); client.subscribe('Wisensor/Ping/Query', function (err) { }); })

//CUANDO SE RECIBE MENSAJE client.on('message', function (topic, message) { console.log("Mensaje recibido desde -> " + topic + " Mensaje -> " + message.toString()); if (topic == "Wisensor/Ping/Query"){ var msg = message.toString();

if (msg == "servidor_nodo"){
ping.sys.probe(servidor_nodo, function(isAlive){
    var result = isAlive ? 'host ' + servidor_nodo + ' is alive' : 'host ' + servidor_nodo + ' is dead';
    console.log(result);
    if (result == "host 192.10.253.10 is alive"){
      client.publish('Wisensor/Ping/Servidor_nodo', 'servidor_nodo_online', (error) => {
      console.log(error || 'Resultado publicado en -> Wisensor/Ping/Servidor_nodo');
      })
      }
      if (result == "host 192.10.253.10 is dead"){
      client.publish('Wisensor/Ping/Servidor_nodo', 'servidor_nodo_offline', (error) => {
      console.log(error || 'Resultado publicado en -> Wisensor/Ping/Servidor_nodo');
      })
      }

});    

} if (msg == "estacion_clima"){ ping.sys.probe(estacion_clima, function(isAlive){ var result = isAlive ? 'host ' + estacion_clima + ' is alive' : 'host ' + estacion_clima + ' is dead'; console.log(result); if (result == "host 192.10.253.11 is alive"){ client.publish('Wisensor/Ping/Estacion_clima', 'estacion_clima_online', (error) => { console.log(error || 'Resultado publicado en -> Wisensor/Ping/Estacion_clima'); }) } if (result == "host 192.10.253.11 is dead"){ client.publish('Wisensor/Ping/Estacion_clima', 'estacion_clima_offline', (error) => { console.log(error || 'Resultado publicado en -> Wisensor/Ping/Estacion_clima'); }) }

});  

} if (msg == "gateway_lora"){ ping.sys.probe(gateway_lora, function(isAlive){ var result = isAlive ? 'host ' + gateway_lora + ' is alive' : 'host ' + gateway_lora + ' is dead'; console.log(result); if (result == "host 192.10.253.12 is alive"){ client.publish('Wisensor/Ping/Gateway_lora', 'gateway_lora_online', (error) => { console.log(error || 'Resultado publicado en -> Wisensor/Ping/Gateway_lora'); }) } if (result == "host 192.10.253.12 is dead"){ client.publish('Wisensor/Ping/Gateway_lora', 'gateway_lora_offline', (error) => { console.log(error || 'Resultado publicado en -> Wisensor/Ping/Gateway_lora'); }) } });

} if (msg == "router"){ ping.sys.probe(router, function(isAlive){ var result = isAlive ? 'host ' + router + ' is alive' : 'host ' + router + ' is dead'; console.log(result); if (result == "host 192.10.253.1 is alive"){ client.publish('Wisensor/Ping/Router', 'router_online', (error) => { console.log(error || 'Resultado publicado en -> Wisensor/Ping/Router'); }) } if (result == "host 192.10.253.1 is dead"){ client.publish('Wisensor/Ping/Router', 'router_offline', (error) => { console.log(error || 'Resultado publicado en -> Wisensor/Ping/Router'); }) } });

} if (msg == "satelital"){ ping.sys.probe(satelital, function(isAlive){ var result = isAlive ? 'host ' + satelital + ' is alive' : 'host ' + satelital + ' is dead'; console.log(result); if (result == "host 192.168.0.1 is alive"){ client.publish('Wisensor/Ping/Satelital', 'satelital_online', (error) => { console.log(error || 'Resultado publicado en -> Wisensor/Ping/Satelital'); }) } if (result == "host 192.168.0.1 is dead"){ client.publish('Wisensor/Ping/Satelital', 'satelital_offline', (error) => { console.log(error || 'Resultado publicado en -> Wisensor/Ping/Satelital'); }) } });

}

}

});`

mondwan commented 4 years ago

Since this library simply uses underlying system call library, you can try which argument can stop the prompt being shown in your own environment.

Once you have figure out which arguments can stop the prompt, please let us know.

Of cause, PR is welcome :)

eternalharvest commented 3 years ago

@mondwan, @CVidalAST

I confirmed this issue in pm2 on Windows environment. And I foud solution for this, so I make a PR for this. Just set windowsHide option to true.