Skip to content

Instantly share code, notes, and snippets.

@blubbll
Last active July 13, 2020 05:13
Show Gist options
  • Save blubbll/932dff7b33ded409be4b956ad82bbb33 to your computer and use it in GitHub Desktop.
Save blubbll/932dff7b33ded409be4b956ad82bbb33 to your computer and use it in GitHub Desktop.
network scanner for TL-WR841N
// network scanner for TL-WR841N
const puppeteer = require('puppeteer'),
fs = require("fs"),
exec = require("child_process").exec
//restart after an hour
setTimeout(_=>{
process.exit(0);
}, 60*30*1000);
const log = input => {
var today = new Date();
var date = today.getFullYear()+'-'+(today.getMonth()+1)+'-'+today.getDate();
var time = today.getHours() + ":" + today.getMinutes() + ":" + today.getSeconds();
var dateTime = date+' '+time;
console.log(dateTime+":");
console.log(input);
}
process.title = `Netlister`;
const host = "http://192.168.0.1";
let browser, user;
(async () => {
//start
log("[browser]getting ready...");
browser = await puppeteer.launch({
headless: true,
ignoreHTTPSErrors: true
});
page = (await browser.pages())[0];
await page.goto(host, {
waitUntil: 'networkidle0'
});
{
//login
log("[page]logging in...");
await page.type('#userName', 'admin');
await page.type('#pcPassword', 'admin');
await page.click('#loginBtn');
}
await page.evaluate((args) => {
const fetchy = _ => fetch("http://192.168.0.1/cgi?5", {
"headers": {
"accept": "*/*",
"accept-language": "de-DE,de;q=0.9,en-US;q=0.8,en;q=0.7",
"cache-control": "no-cache",
"content-type": "text/plain",
"pragma": "no-cache"
},
"referrer": "http://192.168.0.1/mainFrame.htm",
"referrerPolicy": "no-referrer-when-downgrade",
"body": "[LAN_HOST_ENTRY#0,0,0,0,0,0#0,0,0,0,0,0]0,4\r\nleaseTimeRemaining\r\nMACAddress\r\nhostName\r\nIPAddress\r\n",
"method": "POST",
"mode": "cors",
"credentials": "include"
}).then(res => res.text()).then(text => {
const entries = [];
const _entries = text.split("[");
for (const key in _entries) {
if (+key !== 0 && +key !== _entries.length - 1) {
const _entry = _entries[key];
const entry = {
id: +key
//[_entry.split("\n")[1].split("=")[0]] : _entry.split("\n")[1].split("=")[1],
}
for (const key in _entry.split("\n")) {
const line = _entry.split("\n")[+key];
if (+key !== 0)
entry[line.split("=")[0]] = line.split("=")[1];
}
entries.push(entry);
}
}
window.entries = entries;
setTimeout(fetchy, 4999);
});
fetchy();
});
})();
{ //result
let oldValue = "";
const interval = setInterval(async _ => {
try {
if (page) {
var ENTRIES = await ((await browser.pages())[0].evaluate(() => window.entries));
if (ENTRIES && JSON.stringify(oldValue) !== JSON.stringify(ENTRIES)) {
if(ENTRIES.length===0) process.exit(1);//broke
//write to file
fs.writeFile('entries.json', JSON.stringify(ENTRIES), err=> {
if (err) return console.log(err);
log("[File]writing values to file");
log(ENTRIES);
oldValue = Object.assign([], ENTRIES);
});
}
}
} catch (e) {
console.error(e);
log("[err]browser not ready yet");
}
}, 999);
}
{ //batch
require('readline')
.createInterface(process.stdin, process.stdout)
.question("", _ => {
process.exit();
});
} { //handle events
const handler = err => {
err && log(`[bad error]${err}`);
process.exit();
}
process.on('uncaughtException', handler);
process.on('unhandledRejection', handler);
}
});
{
//login
console.log("[page]logging in...");
await page.type('#userName', 'admin');
await page.type('#pcPassword', 'admin');
await page.click('#loginBtn');
}
await page.evaluate((args) => {
const fetchy = _ => fetch("http://192.168.0.1/cgi?5", {
"headers": {
"accept": "*/*",
"accept-language": "de-DE,de;q=0.9,en-US;q=0.8,en;q=0.7",
"cache-control": "no-cache",
"content-type": "text/plain",
"pragma": "no-cache"
},
"referrer": "http://192.168.0.1/mainFrame.htm",
"referrerPolicy": "no-referrer-when-downgrade",
"body": "[LAN_HOST_ENTRY#0,0,0,0,0,0#0,0,0,0,0,0]0,4\r\nleaseTimeRemaining\r\nMACAddress\r\nhostName\r\nIPAddress\r\n",
"method": "POST",
"mode": "cors",
"credentials": "include"
}).then(res => res.text()).then(text => {
const entries = [];
const _entries = text.split("[");
for (const key in _entries) {
if (+key !== 0 && +key !== _entries.length - 1) {
const _entry = _entries[key];
const entry = {
id: +key
//[_entry.split("\n")[1].split("=")[0]] : _entry.split("\n")[1].split("=")[1],
}
for (const key in _entry.split("\n")) {
const line = _entry.split("\n")[+key];
if (+key !== 0)
entry[line.split("=")[0]] = line.split("=")[1];
}
entries.push(entry);
}
}
window.entries = entries;
setTimeout(fetchy, 4999);
});
fetchy();
});
})();
{ //result
let oldValue = "";
const interval = setInterval(async _ => {
try {
if (page) {
var ENTRIES = await ((await browser.pages())[0].evaluate(() => window.entries));
if (ENTRIES && JSON.stringify(oldValue) !== JSON.stringify(ENTRIES)) {
//write to file
fs.writeFile('entries.json', JSON.stringify(ENTRIES), err=> {
if (err) return console.log(err);
console.log("[File]writing values to file");
console.log(ENTRIES);
oldValue = Object.assign([], ENTRIES);
});
}
}
} catch (e) {
console.error(e);
console.log("[err]browser not ready yet");
}
}, 999);
}
{ //batch
require('readline')
.createInterface(process.stdin, process.stdout)
.question("", _ => {
process.exit();
});
} { //handle events
const handler = err => {
err && console.log(`[bad error]${err}`);
process.exit();
}
process.on('uncaughtException', handler);
process.on('unhandledRejection', handler);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment