Created
July 7, 2020 12:39
-
-
Save blubbll/55d95abe843b1540fccc5dc7023ab0b7 to your computer and use it in GitHub Desktop.
restart netgear
This file contains hidden or 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
//restart nighthawk m2 | |
const puppeteer = require('puppeteer'), | |
fs = require("fs"), | |
exec = require("child_process").exec | |
process.title = `restart`; | |
const host = "http://192.168.1.1"; | |
let browser, page; | |
(async () => { | |
//start | |
console.log("[browser]getting ready..."); | |
browser = await puppeteer.launch({ | |
headless: true, | |
ignoreHTTPSErrors: true | |
}); | |
page = (await browser.pages())[0]; | |
await page.goto(host, { | |
waitUntil: 'networkidle0' | |
}); | |
{ //login | |
console.log("[page]logging in..."); | |
await page.type('#user_name', 'admin'); | |
await page.type('#session_password', 'admin'); | |
await page.click('#login_submit'); | |
} | |
{ //restart | |
console.log("[page]restarting..."); | |
await page.evaluate((args) => { | |
fetch("http://192.168.1.1/api/model.json?internalapi=1&x=47989", { | |
"headers": { | |
"accept": "*/*", | |
"accept-language": "de-DE,de;q=0.9,en-US;q=0.8,en;q=0.7", | |
"cache-control": "no-cache", | |
"pragma": "no-cache" | |
}, | |
"referrer": "http://192.168.1.1/index.html", | |
"referrerPolicy": "no-referrer-when-downgrade", | |
"body": null, | |
"method": "GET", | |
"mode": "cors", | |
"credentials": "include" | |
}).then(res => res.json()).then(json => { | |
fetch("http://192.168.1.1/Forms/config", { | |
"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": "application/x-www-form-urlencoded", | |
"pragma": "no-cache" | |
}, | |
"referrer": "http://192.168.1.1/index.html", | |
"referrerPolicy": "no-referrer-when-downgrade", | |
"body": `general.shutdown=restart&err_redirect=/error.json&ok_redirect=/success.json&token=${json.session.secToken}`, | |
"method": "POST", | |
"mode": "cors", | |
"credentials": "include" | |
}).then(_ => { | |
window.restarted = true | |
}).catch(_ => { | |
window.restarted = false | |
}) | |
}); | |
}); | |
} | |
})(); | |
{ //result | |
const interval = setInterval(async _ => { | |
try { | |
if (page) { | |
const restarted = await ((await browser.pages())[0].evaluate(() => window.restarted)); | |
if (restarted === true) { | |
clearInterval(interval); | |
console.log("[result]restarted!"); | |
process.exit(); | |
} | |
if (restarted === false) { | |
console.log("[result]bad"); | |
process.exit(); | |
} | |
} | |
} catch (e) { | |
console.log("[err]browser not ready yet"); | |
} | |
}, 1999); | |
} { //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