A simple traceroute module for NodeJS apps.
npm install traceroute-lite
-or-
npm install ben-bradley/traceroute-lite
npm test
-or-
mocha -R spec
var Traceroute = require('traceroute-lite');
var traceroute = new Traceroute('8.8.8.8');
traceroute.on('hop', function(hop) {
console.log(hop); // { counter: 1, ip: '1.2.3.4', ms: 12 }
});
traceroute.start(function(err, hops) {
console.log(hops);
/* [
* { counter: 1, ip: '1.2.3.4', ms: 12 },
* { counter: 2, ip: '1.2.3.5', ms: 34 },
* ...,
* { counter: n, ip: '8.8.8.8', ms: 10 }
* ]
*/
});
A 'hop' is a unique IP that your trace crosses. They are represented as objects using this schema:
{
counter: Number, // A number representing which hop this is in sequence.
ip: String, // The IP address of the interface on the remote device.
ms: Number, // The milliseconds it takes to reach that IP
}
If a hop fails to respond, the ip
and ms
are null
.
hop
= When an individual hop is detected, responds with({ counter: 1, ip: '1.2.3.4', ms: 12 })
.done
= When the trace is complete, responds with(error, hops)
. Thedone
event occurs before the callback is executed.
Traceroute.start(callback)
accepts an optional callback that returns(error, hops)
. Thedone
event occurs before the callback is executed.
var Traceroute = require('traceroute-lite');
var traceroute = new Traceroute('8.8.8.8');
traceroute.start(function(err, hops) {
console.log(hops);
/* [
* { counter: 1, ip: '1.2.3.4', ms: 12 },
* { counter: 2, ip: '1.2.3.5', ms: 34 },
* ...,
* { counter: n, ip: '8.8.8.8', ms: 10 }
* ]
*/
});
var Traceroute = require('traceroute-lite');
var traceroute = new Traceroute('8.8.8.8');
traceroute.on('hop', function(hop) {
console.log(hop); // { counter: 1, ip: '1.2.3.4', ms: 12 }
});
traceroute.on('done', function(err, hops) {
console.log(hops);
/* [
* { counter: 1, ip: '1.2.3.4', ms: 12 },
* { counter: 2, ip: '1.2.3.5', ms: 34 },
* ...,
* { counter: n, ip: '8.8.8.8', ms: 10 }
* ]
*/
});
traceroute.start();