-
-
Save jonathanhoskin/0bc11f55d0ec926c0a457d4110b1f46f to your computer and use it in GitHub Desktop.
// 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(); | |
}); | |
}); |
Hi Linuxman, I've solved the issue.
The script check the "Logout" button, as I use the modem in french, I've just changed the script to check the "sortie" button
checkLoggedIn() {
if (loadInProgress) {
console.log('Still logging in...');
return false;
} else {
var loggedIn = page.evaluate(function() {
return ($('#logout_span').text() === 'Sortie');
});
return loggedIn;
}
Hope it will help
You're a genius @fab76 !!!
Exactly that's what happened, in my case the modem is in Spanish, so I made it this way and it works perfect.
function checkLoggedIn() {
if (loadInProgress) {
console.log('Still logging in...');
return false;
} else {
var loggedIn = page.evaluate(function() {
return ($('#logout_span').text() === 'Cerrar sesión');
});
return loggedIn;
}
}
Sometimes I lose my internet connection and this script will help me restart it automatically.
Thank you very much @jonathanhoskin
@linuxman, If you want I did a simple script that do a ping to the google dns, if it fails, it will reboot the box, then I added it to a cron executed all the 2 minutes
#!/bin/bash
target=8.8.8.8
count=$( ping -c 1 $target | grep icmp* | wc -l )
if [ $count -eq 0 ]
then
date >> /home/script/pingfail.log
date >> /home/script/reboot.log
sudo phantomjs /home/script/huawei >> /home/script/reboot.log
else
date >> /home/script/pingsuccess.log
fi
It works well, I can see arround 1 reboot per day
I'm extremely new to coding so please excuse me if I sound dumb.
How would I go about automating this script to run at certain intervals? I had no issue running the script but I have no idea how one would automate the process of navigating to the script in CMD Prompt to run it.
Hi @NordicZA - a couple of options jump to mind:
- Place a while loop in the execution:
#!/bin/bash
while [ 1 ]; do
phantomjs reboot-modem.js
sleep 60
done
This creates an infinitely repeating loop. sleep 60
will sleep for minute.
- Use a cron job: https://opensource.com/article/17/11/how-use-cron-linux
@jonathanhoskin thank you so much! I managed to do the first one really easily, thanks for your help!
👍 @NordicZA glad you got it sorted
Thanks heaps for this script! Just wanted to add that this also works for the Huawei B525 (specifically, the Optus version used for home broadband in Australia):
piepants@dalian:~$ phantomjs reboot-modem.js
Page load started: about:blank
Page load finished: http://192.168.8.1/html/home.html
Loaded home.html
Page load started: http://192.168.8.1/html/home.html
Page load finished: http://192.168.8.1/html/home.html
Filling login credentials
Clicking Log In button
Logging in...
Waiting for logged in page JS...
Waiting for logged in page JS...
Logged in
Page load started: http://192.168.8.1/html/home.html
Page load finished: http://192.168.8.1/html/reboot.html
Loaded reboot.html
Loaded reboot button
Clicked reboot button
Page load started: http://192.168.8.1/html/reboot.html
Page load finished: http://192.168.8.1/html/reboot.html
Loaded reboot confirm button
Clicked reboot confirm button
Page load started: http://192.168.8.1/html/reboot.html
Page load finished: http://192.168.8.1/html/reboot.html
Reboot Done
Nice one @pantsofpie - good to hear!
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.
Hello !!!
I have the same problem as @fav76 but i'm using a B310s-518
I am using phantomjs 2.1.1 in Manjaro Linux.
Any suggestion will be welcome.