Skip to content

Instantly share code, notes, and snippets.

@zhoukekestar
Created December 23, 2016 06:29
Show Gist options
  • Save zhoukekestar/8da88277a691843fff12320d04483529 to your computer and use it in GitHub Desktop.
Save zhoukekestar/8da88277a691843fff12320d04483529 to your computer and use it in GitHub Desktop.
Find MAC address by IP
// node findmac.js 10.10.2.255 c4-01-7c-3d-b8-40
const exec = require('child_process').exec;
// http://thebackroomtech.com/2010/08/22/determine-ip-address-from-a-mac-address/
var broadcast = process.argv[2]
, mac = process.argv[3]
, targetIP = '';
if (!broadcast) {
console.error('Broadcast ip is required.')
return;
}
if (!mac) {
console.error('MAC is required.')
return;
}
console.log('ping ' + broadcast);
exec('ping ' + broadcast, function(error, stdout, stderr) {
if (error) {
console.error(error);
return;
}
console.log('arp -a');
exec('arp -a', function(err, stdout, stderr) {
if (err) {
console.error(err)
}
stdout = stdout.split(/\r\n/);
for (var i = 0; i < stdout.length; i++) {
if (new RegExp(mac).test(stdout[i])) {
console.log('Find it:' + stdout[i]);
targetIP = stdout[i].match(/\d+\.\d+\.\d+.\d+/)[0];
break;
}
}
if (targetIP) {
console.log('targetIP: ' + targetIP)
// open explorer share.
//targetIP = '10.10.2.18'
//exec('explorer \\\\' + targetIP)
} else {
console.log('No such MAC.')
}
})
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment