Last active
October 5, 2024 10:42
-
-
Save jonathanhoskin/0bc11f55d0ec926c0a457d4110b1f46f to your computer and use it in GitHub Desktop.
Huawei B315 Modem Reboot Script
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
// PhantomJS script to reboot a Huawei B315 modem | |
// | |
// Author: Jonathan Hoskin / 2017-09-02 | |
// Twitter: @jhossnz | |
// Github: https://github.com/jonathanhoskin | |
// | |
// Requires PhantomJS ~ 2.1.1 | |
// | |
// Enter your own modem details here | |
var username = 'admin'; | |
var password = 'admin'; | |
var host = '192.168.1.254'; | |
// End modem details | |
// | |
// | |
// Script specific variables | |
var page = require('webpage').create(); | |
var loadInProgress = false; | |
var intervalTime = 100; | |
var homeUrl = 'http://' + host + '/html/home.html'; | |
var rebootUrl = 'http://' + host + '/html/reboot.html'; | |
// End script variable | |
page.onConsoleMessage = function(msg) { | |
console.log(msg); | |
}; | |
page.onLoadStarted = function() { | |
loadInProgress = true; | |
if (page.url) { | |
console.log('Page load started: ' + page.url); | |
} else { | |
console.log('Page load started'); | |
} | |
}; | |
page.onLoadFinished = function() { | |
loadInProgress = false; | |
if (page.url) { | |
console.log('Page load finished: ' + page.url); | |
} else { | |
console.log('Page load finished'); | |
} | |
}; | |
function checkLoggedIn() { | |
if (loadInProgress) { | |
console.log('Still logging in...'); | |
return false; | |
} else { | |
var loggedIn = page.evaluate(function() { | |
return ($('#logout_span').text() === 'Log Out'); | |
}); | |
return loggedIn; | |
} | |
} | |
function waitUntilLoggedIn(callback) { | |
setTimeout(function() { | |
if (checkLoggedIn()) { | |
callback(true); | |
} else { | |
console.log('Waiting for logged in page JS...'); | |
waitUntilLoggedIn(callback); | |
} | |
}, intervalTime); | |
} | |
function loginDialogVisible() { | |
var visible = page.evaluate(function() { | |
return ($('#dialog').length > 0); | |
}); | |
return visible; | |
} | |
function waitForLoginDialog(callback) { | |
setTimeout(function() { | |
if (loginDialogVisible) { | |
callback(true); | |
} else { | |
console.log('Waiting for login dialog JS...'); | |
waitForLoginDialog(callback); | |
} | |
}, intervalTime); | |
} | |
function login(callback) { | |
page.open(homeUrl, function(status) { | |
if (status !== 'success') { | |
console.log('Unable to load home.html'); | |
phantom.exit(); | |
} else { | |
console.log('Loaded home.html'); | |
page.evaluate(function() { | |
// This is a call to a JS method in the page main.js file | |
showloginDialog(); | |
return true; | |
}); | |
waitForLoginDialog(function() { | |
console.log('Filling login credentials'); | |
page.evaluate(function(u, p) { | |
$('input#username').val(u); | |
$('input#password').val(p); | |
return true; | |
}, username, password); | |
console.log('Clicking Log In button'); | |
page.evaluate(function() { | |
$('input#pop_login').click(); | |
return true; | |
}); | |
console.log('Logging in...'); | |
waitUntilLoggedIn(function() { | |
console.log('Logged in'); | |
callback(); | |
}); | |
}); | |
} | |
}); | |
} | |
function rebootButtonLoaded() { | |
var elementLoaded = page.evaluate(function() { | |
return ($('#button_reboot').find('input').length > 0); | |
}); | |
return elementLoaded; | |
} | |
function rebootConfirmButtonLoaded() { | |
var elementLoaded = page.evaluate(function() { | |
return ($('input#pop_confirm').length > 0); | |
}); | |
return elementLoaded; | |
} | |
function waitUntilRebootButtonLoaded(callback) { | |
setTimeout(function() { | |
if (loadInProgress) { | |
console.log('Reboot page still loading...'); | |
} else { | |
if (rebootButtonLoaded()) { | |
callback(); | |
} else { | |
console.log('Waiting for reboot page JS...'); | |
waitUntilRebootButtonLoaded(callback); | |
} | |
} | |
}, intervalTime); | |
} | |
function waitUntilRebootConfirmButtonLoaded(callback) { | |
setTimeout(function() { | |
if (loadInProgress) { | |
console.log('Reboot confirm still loading...'); | |
} else { | |
if (rebootConfirmButtonLoaded()) { | |
callback(); | |
} else { | |
console.log('Waiting for reboot confirm JS...'); | |
waitUntilRebootConfirmButtonLoaded(callback); | |
} | |
} | |
}, intervalTime); | |
} | |
function reboot(callback) { | |
page.open(rebootUrl, function(status) { | |
if (status !== 'success') { | |
console.log('Unable to load reboot.html'); | |
callback(); | |
return; | |
} | |
if (page.url !== rebootUrl) { | |
console.log('Wrong reboot URL! ' + page.url); | |
callback(); | |
return; | |
} | |
console.log('Loaded reboot.html'); | |
waitUntilRebootButtonLoaded(function() { | |
console.log('Loaded reboot button'); | |
page.evaluate(function() { | |
var rebootButton = $('#button_reboot').find('input').first(); | |
$(rebootButton).click(); | |
return true; | |
}); | |
console.log('Clicked reboot button') | |
waitUntilRebootConfirmButtonLoaded(function() { | |
console.log('Loaded reboot confirm button'); | |
page.evaluate(function() { | |
var confirmButton = $('input#pop_confirm'); | |
$(confirmButton).click(); | |
return true; | |
}); | |
console.log('Clicked reboot confirm button') | |
setTimeout(function() { | |
callback(); | |
}, 5000); // 5 second sleep, just to give the final Ajax calls time to complete | |
}); | |
}); | |
}); | |
} | |
login(function() { | |
reboot(function() { | |
console.log('Reboot Done') | |
phantom.exit(); | |
}); | |
}); |
Hello, script worked great on my B525 on old software version. Now there is a new update (81.191.13.00.1134) and everything has been redone.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice one @pantsofpie - good to hear!