Skip to content
Browse files

FIRST

  • Loading branch information...
0 parents commit 9b5860b558fd9ad331a14aa0613e4ea0be17abb7 @jaw187 committed
Showing with 159 additions and 0 deletions.
  1. +46 −0 README.md
  2. +1 −0 index.js
  3. +14 −0 package.json
  4. +98 −0 traceroute.js
46 README.md
@@ -0,0 +1,46 @@
+node-traceroute
+===============
+
+Simple wrapper around native traceroute command. A trace in node will return an array of hops. Each hop contains the hosts in that hop and the respective round trip times of each host.
+
+Install
+-------
+
+```
+$ npm install traceroute
+```
+
+Example
+-------
+```javascript
+traceroute = require('traceroute');
+traceroute.trace('google.com', function (err,hops) {
+ if (!err) console.log(hops);
+});
+```
+This example would write the following to the console if run from my network...
+```javascript
+[ { '66.97.5.249': [ 43.206, 43.377, 43.379 ] },
+ { '216.182.7.102': [ 43.575, 43.799, 43.808 ] },
+ { '216.182.7.165': [ 44.538, 44.613, 44.837 ] },
+ { '216.182.7.253': [ 44.846, 56.281, 56.303 ] },
+ { '4.53.88.197': [ 57.735, 57.707, 57.891 ] },
+ { '4.69.155.254': [ 58.618, 48.514, 48.567 ] },
+ { '4.69.134.77': [ 34.167, 44.317 ], '4.69.148.45': [ 44.366 ] },
+ { '4.69.141.22': [ 44.542, 44.316, 44.642 ] },
+ { '4.69.138.196': [ 44.56, 35.554 ],
+ '4.69.138.228': [ 45.035 ] },
+ { '4.59.128.18': [ 35.777, 35.827, 45.305 ] },
+ { '72.14.238.232': [ 45.621 ],
+ '209.85.255.68': [ 45.079, 42.695 ] },
+ { '209.85.251.37': [ 32.588, 32.569, 32.657 ] },
+ { '209.85.251.9': [ 59.068 ],
+ '209.85.254.48': [ 60.287, 75.094 ] },
+ { '66.249.94.22': [ 61.565, 62.063 ],
+ '72.14.238.242': [ 63.001 ] },
+ { '64.233.174.140': [ 96.476, 97.585 ],
+ '72.14.239.83': [ 98.656 ] },
+ { '64.233.174.191': [ 149.286, 93.528, 94.405 ] },
+ { '216.239.43.76': [ 83.901, 85.089, 84.837 ] },
+ { '74.125.224.240': [ 84.645, 75.322, 75.585 ] } ]
+```
1 index.js
@@ -0,0 +1 @@
+exports=require('./traceroute');
14 package.json
@@ -0,0 +1,14 @@
+{
+ "author": "James Weston <james@tenseven.org>",
+ "name": "traceroute",
+ "description": "Wrapper around native traceroute command",
+ "version": "0.0.1",
+ "repository": {
+ "type": "git",
+ "url": "git://github.com/jaw187/node-traceroute.git},
+ "engines": {
+ "node": "~0.6.5"
+ },
+ "dependencies": {},
+ "devDependencies": {}
+}
98 traceroute.js
@@ -0,0 +1,98 @@
+var child = require('child_process'),
+ net = require('net'),
+ dns = require('dns'),
+ isWin = (/^win/.test(require('os').platform()));
+
+
+function parseHop(line) {
+ line = line.replace(/\*/g,'0');
+ if (isWin) line = line.replace(/\</g,'');
+ var s = line.split(' ');
+ for (var i=s.length - 1; i > -1; i--) {
+ if (s[i] === '') s.splice(i,1);
+ if (s[i] === 'ms') s.splice(i,1);
+ }
+
+ if (isWin) return parseHopWin(s);
+ else return parseHopNix(s);
+}
+
+function parseHopWin(line) {
+ if (line[4] === 'Request')
+ return false;
+
+ var hop = {};
+ hop[line[4]] = [ +line[1], +line[2], +line[3]];
+
+ return hop;
+}
+
+function parseHopNix(line) {
+ if (line[1] === '0')
+ return false;
+
+ var hop = {},
+ lastip = line[1];
+
+ hop[line[1]] = [+line[2]];
+
+ for (var i=3; i < line.length; i++) {
+ if (net.isIP(line[i])) {
+ lastip = line[i];
+ if (!hop[lastip])
+ hop[lastip] = [];
+ }
+ else hop[lastip].push(+line[i]);
+ }
+
+ return hop;
+}
+
+function parseOutput(output,cb) {
+ var lines = output.split('\n'),
+ hops=[];
+
+ lines.shift();
+ lines.pop();
+
+ if (isWin) {
+ for (var i = 0; i < lines.length; i++)
+ if (/^\s+1/.test(lines[i]))
+ break;
+ lines.splice(0,i);
+ lines.pop(); lines.pop();
+ }
+
+ for (var i = 0; i < lines.length; i++)
+ hops.push(parseHop(lines[i]));
+
+ cb(null,hops);
+}
+
+function trace(host,cb) {
+ dns.lookup(host, function (err) {
+ if (err && net.isIP(host) === 0)
+ cb('Invalid host');
+ else {
+ var traceroute;
+
+ if (isWin) {
+ traceroute = child.exec('tracert -d ' + host, function (err,stdout,stderr) {
+ if (!err)
+ parseOutput(stdout,cb);
+ });
+ }
+ else {
+ traceroute = child.exec('traceroute -n ' + host, function (err,stdout,stderr) {
+ if (!err)
+ parseOutput(stdout,cb);
+ });
+ }
+ }
+ });
+}
+
+exports.trace = function (host,cb) {
+ host = host + '';
+ trace(host.toUpperCase(),cb);
+}

0 comments on commit 9b5860b

Please sign in to comment.
Something went wrong with that request. Please try again.