mirror of https://github.com/IoTcat/wIoT.git
parent
7501895874
commit
2ab9c12b48
9 changed files with 745 additions and 434 deletions
@ -0,0 +1 @@ |
|||||||
|
npm-debug.log |
@ -0,0 +1,30 @@ |
|||||||
|
macfromip |
||||||
|
========= |
||||||
|
|
||||||
|
## Synopsis |
||||||
|
|
||||||
|
* Nodejs script; |
||||||
|
* Gets a MAC address from a LAN IP address; |
||||||
|
* Only works on linux, OSX and win32 platforms; |
||||||
|
|
||||||
|
## Code Example |
||||||
|
|
||||||
|
``` |
||||||
|
var macfromip = require('macfromip'); |
||||||
|
|
||||||
|
macfromip.getMac('192.168.2.169', function(err, data){ |
||||||
|
if(err){ |
||||||
|
console.log(err); |
||||||
|
} |
||||||
|
console.log(data); |
||||||
|
}); |
||||||
|
``` |
||||||
|
|
||||||
|
## Installation |
||||||
|
|
||||||
|
``` |
||||||
|
npm install macfromip |
||||||
|
``` |
||||||
|
|
||||||
|
## TODO List: |
||||||
|
Complete list on [Trello](https://trello.com/b/B1WM4gbZ/macfromip) |
@ -0,0 +1,10 @@ |
|||||||
|
var macfromip = require('./macfromip.js'); |
||||||
|
|
||||||
|
macfromip.getMac('192.168.2.57', function(err, data){ |
||||||
|
if(err){ |
||||||
|
console.log(err); |
||||||
|
} |
||||||
|
else{ |
||||||
|
console.log(data); |
||||||
|
} |
||||||
|
}); |
@ -0,0 +1,160 @@ |
|||||||
|
/* jshint node: true */ |
||||||
|
'use strict'; |
||||||
|
|
||||||
|
var macfromip = exports; |
||||||
|
|
||||||
|
var cp = require('child_process'); |
||||||
|
var os = require('os'); |
||||||
|
|
||||||
|
var MACADDRESS_LENGTH = 17; |
||||||
|
|
||||||
|
macfromip.isEmpty = function(value){ |
||||||
|
return ((value === null) || (typeof value === 'undefined') || 0 === value.length); |
||||||
|
}; |
||||||
|
|
||||||
|
macfromip.isString = function(value){ |
||||||
|
if(macfromip.isEmpty(value)){ |
||||||
|
throw new Error('Expected a not null value'); |
||||||
|
} |
||||||
|
|
||||||
|
if (typeof value === 'string') { |
||||||
|
return true; |
||||||
|
} |
||||||
|
|
||||||
|
return false; |
||||||
|
}; |
||||||
|
|
||||||
|
macfromip.isIpAddress = function(ipaddress){ |
||||||
|
if(!macfromip.isString(ipaddress)){ |
||||||
|
throw new Error('Expected a string'); |
||||||
|
} |
||||||
|
|
||||||
|
/* Thanks to http://www.w3resource.com/javascript/form/ip-address-validation.php#sthash.kBJql3HS.dpuf */ |
||||||
|
if (/^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/.test(ipaddress.trim())){ |
||||||
|
return (true); |
||||||
|
} |
||||||
|
return (false); |
||||||
|
}; |
||||||
|
|
||||||
|
macfromip.ipIsSelf = function(ipaddress){ |
||||||
|
var ifaces = os.networkInterfaces(); |
||||||
|
var selfIps = new Array(); |
||||||
|
|
||||||
|
Object.keys(ifaces).forEach(function (ifname) { |
||||||
|
var alias = 0; |
||||||
|
ifaces[ifname].forEach(function (iface) { |
||||||
|
if ('IPv4' !== iface.family) { |
||||||
|
// skip over internal (i.e. 127.0.0.1) and non-ipv4 addresses
|
||||||
|
return; |
||||||
|
} |
||||||
|
selfIps.push(iface.address); |
||||||
|
}); |
||||||
|
}); |
||||||
|
|
||||||
|
var index; |
||||||
|
for (index = 0; index < selfIps.length; ++index) { |
||||||
|
if(selfIps[index] === ipaddress){ |
||||||
|
return true; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
return false; |
||||||
|
}; |
||||||
|
|
||||||
|
macfromip.getMacInLinux = function(ipAddress, callback){ |
||||||
|
// OSX requires -c switch first
|
||||||
|
var ls = cp.exec('ping -c 1 ' + ipAddress, |
||||||
|
function(error, stdout, stderr) { |
||||||
|
if (error !== null) { |
||||||
|
callback('IP address unreachable', 'exec error: ' + error); |
||||||
|
return; |
||||||
|
} |
||||||
|
if (stderr !== null && stderr !== '') { |
||||||
|
callback('IP address unreachable', 'stderr: ' + stderr); |
||||||
|
return; |
||||||
|
} |
||||||
|
|
||||||
|
var ls2 = cp.exec('arp -a', |
||||||
|
function(error2, stdout2, stderr2) { |
||||||
|
if (error2 !== null) { |
||||||
|
callback('IP address unreachable', 'exec error: ' + error2); |
||||||
|
return; |
||||||
|
} |
||||||
|
if (stderr2 !== null && stderr2 !== '') { |
||||||
|
callback('IP address unreachable', 'stderr: ' + stderr2); |
||||||
|
return; |
||||||
|
} |
||||||
|
|
||||||
|
stdout2 = (stdout2.substring(stdout2.indexOf(ipAddress) + (ipAddress.length + 5))).substring(MACADDRESS_LENGTH, 0); |
||||||
|
callback(false, stdout2); |
||||||
|
return; |
||||||
|
}); |
||||||
|
}); |
||||||
|
}; |
||||||
|
|
||||||
|
macfromip.getMacInWin32 = function(ipAddress, callback){ |
||||||
|
var ls = cp.exec('ping ' + ipAddress + ' -n 1', |
||||||
|
function(error, stdout, stderr) { |
||||||
|
if (error !== null) { |
||||||
|
callback('IP address unreachable', 'exec error: ' + error); |
||||||
|
return; |
||||||
|
} |
||||||
|
if (stderr !== null && stderr !== '') { |
||||||
|
callback('IP address unreachable', 'stderr: ' + stderr); |
||||||
|
return; |
||||||
|
} |
||||||
|
|
||||||
|
var ls2 = cp.exec('arp -a', |
||||||
|
function(error2, stdout2, stderr2) { |
||||||
|
if (error2 !== null) { |
||||||
|
callback('IP address unreachable', 'exec error: ' + error2); |
||||||
|
return; |
||||||
|
} |
||||||
|
if (stderr2 !== null && stderr2 !== '') { |
||||||
|
callback('IP address unreachable', 'stderr: ' + stderr2); |
||||||
|
return; |
||||||
|
} |
||||||
|
|
||||||
|
var offset = 22 - ipAddress.length; |
||||||
|
|
||||||
|
stdout2 = (stdout2.substring(stdout2.indexOf(ipAddress) + (ipAddress.length + offset))).substring(MACADDRESS_LENGTH, 0); |
||||||
|
callback(false, stdout2); |
||||||
|
return; |
||||||
|
}); |
||||||
|
}); |
||||||
|
}; |
||||||
|
|
||||||
|
macfromip.getMac = function(ipAddress, callback) { |
||||||
|
|
||||||
|
if(!macfromip.isIpAddress(ipAddress)) { |
||||||
|
throw new Error("The value you entered is not a valid IP address"); |
||||||
|
} |
||||||
|
|
||||||
|
if(macfromip.ipIsSelf(ipAddress)){ |
||||||
|
throw new Error("The IP address cannot be self"); |
||||||
|
} |
||||||
|
|
||||||
|
switch(os.platform()){ |
||||||
|
case 'linux': |
||||||
|
macfromip.getMacInLinux(ipAddress, function(err, mac){ |
||||||
|
callback(err, mac); |
||||||
|
}); |
||||||
|
break; |
||||||
|
|
||||||
|
case 'win32': |
||||||
|
macfromip.getMacInWin32(ipAddress, function(err, mac){ |
||||||
|
callback(err, mac); |
||||||
|
}); |
||||||
|
break; |
||||||
|
// OSX
|
||||||
|
case 'darwin': |
||||||
|
macfromip.getMacInLinux(ipAddress, function(err, mac){ |
||||||
|
callback(err, mac); |
||||||
|
}); |
||||||
|
break; |
||||||
|
|
||||||
|
default: |
||||||
|
callback('Unsupported platform: ' + os.platform(), null); |
||||||
|
break; |
||||||
|
} |
||||||
|
}; |
@ -0,0 +1,62 @@ |
|||||||
|
{ |
||||||
|
"_from": "macfromip", |
||||||
|
"_id": "macfromip@1.1.1", |
||||||
|
"_inBundle": false, |
||||||
|
"_integrity": "sha1-uy9GiBDMOm0ZFxabj4InHuMOTd0=", |
||||||
|
"_location": "/macfromip", |
||||||
|
"_phantomChildren": {}, |
||||||
|
"_requested": { |
||||||
|
"type": "tag", |
||||||
|
"registry": true, |
||||||
|
"raw": "macfromip", |
||||||
|
"name": "macfromip", |
||||||
|
"escapedName": "macfromip", |
||||||
|
"rawSpec": "", |
||||||
|
"saveSpec": null, |
||||||
|
"fetchSpec": "latest" |
||||||
|
}, |
||||||
|
"_requiredBy": [ |
||||||
|
"#USER", |
||||||
|
"/" |
||||||
|
], |
||||||
|
"_resolved": "https://registry.npmjs.org/macfromip/-/macfromip-1.1.1.tgz", |
||||||
|
"_shasum": "bb2f468810cc3a6d1917169b8f82271ee30e4ddd", |
||||||
|
"_spec": "macfromip", |
||||||
|
"_where": "E:\\Arduino_project\\wIoT\\node", |
||||||
|
"author": { |
||||||
|
"name": "bcamarneiro", |
||||||
|
"email": "camarneirobruno@gmail.com" |
||||||
|
}, |
||||||
|
"bugs": { |
||||||
|
"url": "https://github.com/bcamarneiro/macfromip/issues" |
||||||
|
}, |
||||||
|
"bundleDependencies": false, |
||||||
|
"contributors": [ |
||||||
|
{ |
||||||
|
"name": "Paul Cook" |
||||||
|
}, |
||||||
|
{ |
||||||
|
"name": "Sean Nessworthy", |
||||||
|
"email": "sean@nessworthy.me" |
||||||
|
} |
||||||
|
], |
||||||
|
"deprecated": false, |
||||||
|
"description": "On given an IP Address, will attempt to identify that IP's MAC address.", |
||||||
|
"directories": { |
||||||
|
"test": "test" |
||||||
|
}, |
||||||
|
"homepage": "https://github.com/bcamarneiro/macfromip", |
||||||
|
"keywords": [ |
||||||
|
"MAC", |
||||||
|
"IP", |
||||||
|
"Address" |
||||||
|
], |
||||||
|
"license": "MIT", |
||||||
|
"main": "macfromip.js", |
||||||
|
"name": "macfromip", |
||||||
|
"repository": { |
||||||
|
"type": "git", |
||||||
|
"url": "git+https://github.com/bcamarneiro/macfromip.git" |
||||||
|
}, |
||||||
|
"version": "1.1.1" |
||||||
|
} |
@ -0,0 +1,22 @@ |
|||||||
|
var macfromip = require('../macfromip.js'); |
||||||
|
|
||||||
|
exports['isIpAddress'] = function (test) { |
||||||
|
test.equal(macfromip.isEmpty('0'), false); |
||||||
|
test.equal(macfromip.isEmpty(' '), false); |
||||||
|
test.equal(macfromip.isEmpty(''), true); |
||||||
|
test.equal(macfromip.isEmpty(null), true); |
||||||
|
test.equal(macfromip.isEmpty(), true); |
||||||
|
|
||||||
|
test.equal(macfromip.isString(false), false); |
||||||
|
test.equal(macfromip.isString(1233536), false); |
||||||
|
test.equal(macfromip.isString('1233536'), true); |
||||||
|
test.equal(macfromip.isString('#$%&asda3445'), true); |
||||||
|
|
||||||
|
test.equal(macfromip.isIpAddress('127.0.0.1'), true); |
||||||
|
test.equal(macfromip.isIpAddress('255.255.255.255'), true); |
||||||
|
test.equal(macfromip.isIpAddress('0.0.0.0'), true); |
||||||
|
test.equal(macfromip.isIpAddress('asdasdasdghdsgsdg'), false); |
||||||
|
test.equal(macfromip.isIpAddress('192.168.2.5a'), false);
|
||||||
|
|
||||||
|
test.done(); |
||||||
|
}; |
Loading…
Reference in new issue