mirror of
https://github.com/node-red/node-red-nodes.git
synced 2025-03-01 10:37:43 +00:00
core to Pi node-red-nodes info updates
to be more consistent style
This commit is contained in:
@@ -30,9 +30,10 @@
|
||||
</script>
|
||||
|
||||
<script type="text/x-red" data-help-name="ping">
|
||||
<p>Pings a machine and returns the trip time in mS.</p>
|
||||
<p>Pings a machine and returns the trip time in mS as <code>msg.payload</code>.</p>
|
||||
<p>Returns <b>false</b> if no response received within 5 seconds, or if the host is unresolveable.</p>
|
||||
<p>Default ping is every 20 seconds but can be configured.</p>
|
||||
<p><code>msg.topic</code> contains the target host ip.
|
||||
</script>
|
||||
|
||||
<script type="text/javascript">
|
||||
|
@@ -1,5 +1,5 @@
|
||||
/**
|
||||
* Copyright 2013 IBM Corp.
|
||||
* Copyright 2013,2016 IBM Corp.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -16,46 +16,35 @@
|
||||
|
||||
module.exports = function(RED) {
|
||||
"use strict";
|
||||
var spawn = require('child_process').spawn;
|
||||
var plat = require('os').platform();
|
||||
var dns = require("dns")
|
||||
var ping = require ("net-ping");
|
||||
|
||||
function PingNode(n) {
|
||||
RED.nodes.createNode(this,n);
|
||||
this.host = n.host;
|
||||
this.timer = n.timer * 1000;
|
||||
this.session = ping.createSession();
|
||||
var node = this;
|
||||
|
||||
node.tout = setInterval(function() {
|
||||
var ex;
|
||||
if (plat == "linux") { ex = spawn('ping', ['-n', '-w', '5', '-c', '1', node.host]); }
|
||||
else if (plat.match(/^win/)) { ex = spawn('ping', ['-n', '1', '-w', '5000', node.host]); }
|
||||
else if (plat == "darwin") { ex = spawn('ping', ['-n', '-t', '5', '-c', '1', node.host]); }
|
||||
else { node.error("Sorry - your platform - "+plat+" - is not recognised."); }
|
||||
var res = false;
|
||||
var line = "";
|
||||
//var regex = /from.*time.(.*)ms/;
|
||||
var regex = /=.*[<|=]([0-9]*).*TTL|ttl..*=([0-9\.]*)/;
|
||||
ex.stdout.on('data', function (data) {
|
||||
line += data.toString();
|
||||
});
|
||||
//ex.stderr.on('data', function (data) {
|
||||
//console.log('[ping] stderr: ' + data);
|
||||
//});
|
||||
ex.on('close', function (code) {
|
||||
var m = regex.exec(line)||"";
|
||||
if (m !== '') {
|
||||
if (m[1]) { res = Number(m[1]); }
|
||||
if (m[2]) { res = Number(m[2]); }
|
||||
}
|
||||
var msg = { payload:false, topic:node.host };
|
||||
if (code === 0) { msg = { payload:res, topic:node.host }; }
|
||||
try { node.send(msg); }
|
||||
catch(e) {}
|
||||
dns.lookup(node.host, function(err, address, family) {
|
||||
if (typeof address === 'undefined') { address = node.host; }
|
||||
node.session.pingHost(address, function(error, target, sent, rcvd) {
|
||||
var msg = { payload:false, topic:target };
|
||||
if (error) {
|
||||
msg.error = error.toString();
|
||||
} else {
|
||||
msg.payload = rcvd - sent;
|
||||
}
|
||||
node.send(msg);
|
||||
node.session.close();
|
||||
});
|
||||
});
|
||||
}, node.timer);
|
||||
|
||||
this.on("close", function() {
|
||||
if (this.tout) { clearInterval(this.tout); }
|
||||
if (node.tout) { clearInterval(this.tout); }
|
||||
if (node.session) { node.session.close(); }
|
||||
});
|
||||
}
|
||||
RED.nodes.registerType("ping",PingNode);
|
||||
|
@@ -1,7 +1,8 @@
|
||||
node-red-node-ping
|
||||
==================
|
||||
|
||||
A <a href="http://nodered.org" target="_new">Node-RED</a> node to ping a remote server, for use as a keep-alive check.
|
||||
A <a href="http://nodered.org" target="_new">Node-RED</a> node to ping a
|
||||
remote server, for use as a keep-alive check.
|
||||
|
||||
Install
|
||||
-------
|
||||
@@ -18,12 +19,14 @@ The fix is to allow it as follows
|
||||
sudo setcap cap_net_raw=ep /bin/ping
|
||||
sudo setcap cap_net_raw=ep /bin/ping6
|
||||
|
||||
|
||||
Usage
|
||||
-----
|
||||
|
||||
Pings a machine and returns the trip time in mS.
|
||||
Pings a machine and returns the trip time in mS as **msg.payload**.
|
||||
|
||||
Returns boolean **false** if no response received within 5 seconds, or if the host is unresolveable.
|
||||
Returns boolean `false` if no response received, or if the host is unresolveable.
|
||||
**msg.wrror** will contain any error message if necessary.
|
||||
|
||||
**msg.topic** contains the ip address of the target host.
|
||||
|
||||
Default ping is every 20 seconds but can be configured.
|
||||
|
@@ -1,8 +1,9 @@
|
||||
{
|
||||
"name" : "node-red-node-ping",
|
||||
"version" : "0.0.7",
|
||||
"version" : "0.0.9",
|
||||
"description" : "A Node-RED node to ping a remote server, for use as a keep-alive check.",
|
||||
"dependencies" : {
|
||||
"net-ping":"1.1.*"
|
||||
},
|
||||
"repository" : {
|
||||
"type":"git",
|
||||
|
Reference in New Issue
Block a user