Skip to content

Instantly share code, notes, and snippets.

@jonathanhoskin
Last active July 7, 2025 09:43
Show Gist options
  • Select an option

  • Save jonathanhoskin/0bc11f55d0ec926c0a457d4110b1f46f to your computer and use it in GitHub Desktop.

Select an option

Save jonathanhoskin/0bc11f55d0ec926c0a457d4110b1f46f to your computer and use it in GitHub Desktop.
Huawei B315 Modem Reboot Script
// 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();
});
});
@jonathanhoskin
Copy link
Copy Markdown
Author

jonathanhoskin commented Oct 2, 2019

Hi @NordicZA - a couple of options jump to mind:

  1. 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.

  1. Use a cron job: https://opensource.com/article/17/11/how-use-cron-linux

@NordicZA
Copy link
Copy Markdown

NordicZA commented Oct 2, 2019

@jonathanhoskin thank you so much! I managed to do the first one really easily, thanks for your help!

@jonathanhoskin
Copy link
Copy Markdown
Author

👍 @NordicZA glad you got it sorted

@pantsofpie
Copy link
Copy Markdown

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

@jonathanhoskin
Copy link
Copy Markdown
Author

Nice one @pantsofpie - good to hear!

@kike981
Copy link
Copy Markdown

kike981 commented Mar 26, 2020

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