-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cc05451
commit abc587b
Showing
15 changed files
with
6,911 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2015 Adafruit | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# Adafruit Raspberry Pi Finder | ||
|
||
This package is an atom-shell app, and is not useful outside of that context. Please | ||
visit the [Adafruit Pi Finder](https://github.com/adafruit/Adafruit-Pi-Finder) repo for | ||
more information. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
// a heavily simplified fork of https://www.npmjs.com/package/node-arp | ||
// removes ping and cleans up a few other things | ||
|
||
var util = require('util'), | ||
spawn = require('child_process').spawn, | ||
os = require('os').platform(); | ||
|
||
function arp(ip, cb) { | ||
|
||
if(os.indexOf('linux') === 0) { | ||
linux(ip, cb); | ||
} else if(os.indexOf('win') === 0) { | ||
windows(ip, cb); | ||
} else if(os.indexOf('darwin') === 0) { | ||
mac(ip, cb); | ||
} | ||
|
||
}; | ||
|
||
function linux(ip, cb) { | ||
|
||
var arp = spawn('arp', [ '-n', ip ]), | ||
buffer = ''; | ||
|
||
arp.stdout.on('data', function (data) { | ||
buffer += data; | ||
}); | ||
|
||
arp.on('close', function(code) { | ||
|
||
var lines = buffer.split('\n'); | ||
|
||
if (code !== 0) { | ||
return cb('arp failed'); | ||
} | ||
|
||
if (lines.length >= 2) { | ||
var chunks = lines[1].split(' ').filter(String); | ||
return cb(null, chunks[2]); | ||
} | ||
|
||
cb(null, false); | ||
|
||
}); | ||
|
||
}; | ||
|
||
function windows(ip, cb) { | ||
|
||
var arp = spawn('arp', ['-a', ip]), | ||
buffer = ''; | ||
|
||
arp.stdout.on('data', function(data) { | ||
buffer += data; | ||
}); | ||
|
||
arp.on('close', function(code) { | ||
|
||
var lines = buffer.split('\r\n'); | ||
|
||
if (code !== 0) { | ||
return cb('arp failed'); | ||
} | ||
|
||
for(var i = 3; i < lines.length; i++) { | ||
|
||
var chunks = lines[i].split(' ').filter(String); | ||
|
||
if (chunks[0] === ip) { | ||
return cb(null, chunks[1].replace(/-/g, ':')); | ||
} | ||
|
||
} | ||
|
||
cb(null, false); | ||
|
||
}); | ||
|
||
}; | ||
|
||
function mac(ip, cb) { | ||
|
||
var arp = spawn('arp', ['-n', ip]), | ||
buffer = ''; | ||
|
||
arp.stdout.on('data', function(data) { | ||
buffer += data; | ||
}); | ||
|
||
arp.on('close', function(code) { | ||
|
||
var chunks = buffer.split(' ').filter(String); | ||
|
||
if (code !== 0) { | ||
return cb('arp failed'); | ||
} | ||
|
||
if (chunks[3] !== 'no') { | ||
var mac = chunks[3].replace(/^0:/g, '00:').replace(/:0:/g, ':00:').replace(/:0$/g, ':00'); | ||
return cb(null, mac); | ||
} | ||
|
||
cb(null, false); | ||
|
||
}); | ||
|
||
}; | ||
|
||
exports = module.exports = arp; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
var ip = require('ip'), | ||
events = require('events'), | ||
util = require('util'), | ||
ping = require('./ping.js'), | ||
async = require('async'), | ||
arp = require('./arp.js'); | ||
|
||
/**** Finder is an event emitter ****/ | ||
util.inherits(Finder, events.EventEmitter); | ||
|
||
/**** Finder prototype ****/ | ||
var proto = Finder.prototype; | ||
|
||
/**** Expose Finder ****/ | ||
exports = module.exports = Finder; | ||
|
||
/**** Finder constructor ****/ | ||
function Finder(options) { | ||
|
||
if (!(this instanceof Finder)) { | ||
return new Finder(options); | ||
} | ||
|
||
events.EventEmitter.call(this); | ||
|
||
util._extend(this, options || {}); | ||
|
||
// grab the user's IP | ||
this.ip = ip.address(); | ||
// grab the first three octects of the IP | ||
this.subnet = this.ip.substr(0, this.ip.lastIndexOf('.')) || false; | ||
|
||
} | ||
|
||
proto.ip = false; | ||
proto.subnet = false; | ||
|
||
proto.start = function(cb) { | ||
|
||
async.times( | ||
255, | ||
this.ping.bind(this), | ||
this.finish.bind(this, cb) | ||
); | ||
|
||
}; | ||
|
||
proto.ping = function(position, next) { | ||
|
||
var host = this.subnet + '.' + position; | ||
|
||
this.emit('ip', host); | ||
|
||
ping(host, function(){ | ||
|
||
this.arp(host, next); | ||
|
||
}.bind(this)); | ||
|
||
}; | ||
|
||
proto.arp = function(host, next) { | ||
|
||
arp(host, function(err, mac) { | ||
|
||
if (err || !mac) { | ||
return next(); | ||
} | ||
|
||
if(/b8:27:eb/.test(mac)) { | ||
return next(null, host); | ||
} | ||
|
||
next(); | ||
|
||
}.bind(this)); | ||
|
||
}; | ||
|
||
proto.finish = function(cb, err, ips) { | ||
|
||
var results = []; | ||
|
||
if(err) { | ||
return cb(err); | ||
} | ||
|
||
if(Array.isArray(ips)) { | ||
|
||
results = ips.filter(function(i) { | ||
return i; | ||
}); | ||
|
||
} | ||
|
||
if(! results.length) { | ||
return cb(); | ||
} | ||
|
||
cb(null, results); | ||
|
||
}; |
Oops, something went wrong.