Created
August 25, 2021 04:16
-
-
Save cstrat/91de08136c8975db8d360338215f0a57 to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env node | |
const spawn = require('child_process').spawn; | |
const https = require("https"); | |
const arp = spawn("arp", ["-n"]); | |
const arpArray = []; | |
let buffer = ''; | |
let errstream = ''; | |
arp.stdout.on('data', function (data) { | |
buffer += data; | |
}); | |
arp.stderr.on('data', function (data) { | |
errstream += data; | |
}); | |
arp.on('close', async function (code) { | |
if (code !== 0) { | |
console.log("Error running arp " + code + " " + errstream); | |
cb(true, code); | |
return; | |
} | |
const table = buffer.split('\n'); | |
for (var line=1; line <= table.length - 1; line++) { | |
const entry = table[line].split(/[ ]+/); | |
console.log(entry); | |
if (entry[3] === 'C') { | |
arpArray.push( | |
{ | |
ip: entry[0], | |
mac: entry[2], | |
} | |
); | |
} | |
} | |
console.log(arpArray); | |
sendARPData(arpArray); | |
}); | |
function sendARPData(data) { | |
const dataString = JSON.stringify(data); | |
const options = { | |
hostname: 'homeserver', | |
port: '443', | |
path: '/api/webhook/firewalla', | |
method: 'POST', | |
headers: { | |
'Content-Type': 'application/json', | |
'Content-Length': dataString.length, | |
}, | |
timeout: 1000, | |
} | |
try { | |
const req = https.request(options, (res) => { | |
if (res.statusCode < 200 || res.statusCode > 299) { | |
return (new Error(`HTTP status code ${res.statusCode}`)) | |
} | |
const body = []; | |
res.on('data', (chunk) => body.push(chunk)); | |
res.on('end', () => { | |
const resString = Buffer.concat(body).toString(); | |
console.log(resString); | |
}) | |
}); | |
req.on('error', (err) => { | |
console.error(err) | |
}); | |
req.on('timeout', () => { | |
req.destroy() | |
return(new Error('Request time out')) | |
}); | |
req.write(dataString); | |
req.end(); | |
} catch (e) { | |
console.log("ERROR", e); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment