Community discussions

 
faogundele
just joined
Topic Author
Posts: 10
Joined: Wed May 02, 2018 5:01 pm

Change of Password for hotspot users

Wed May 02, 2018 5:15 pm

I am new to mikrotik, i need step by step to create a page where hotspot users will be able to change their password using USER SELF FORM MANAGEMENT. It is very urgent. I got this code online and created a script named changepass.php by clicking on plus button in winbox system and then script.


Please anyone that help. I newly installed mikrotik cloud




<?php
use PEAR2\Net\RouterOS;
require_once 'PEAR2/Autoload.php';

$errors = array();

try {
//Adjust RouterOS IP, username and password accordingly.
$client = new RouterOS\Client('192.168.88.1', 'admin', 'password');

$printRequest = new RouterOS\Request(
'/ip hotspot active print .proplist=user',
RouterOS\Query::where('address', $_SERVER['REMOTE_ADDR'])
);
$hotspotUsername = $client->sendSync($printRequest)->getProperty('user');
} catch(Exception $e) {
$errors[] = $e->getMessage();
}

if (isset($_POST['password']) && isset($_POST['password2'])) {
if ($_POST['password'] !== $_POST['password2']) {
$errors[] = 'Passwords do not match.';
} elseif (empty($errors)) {
//Here's the fun part - actually changing the password
$setRequest = new RouterOS\Request('/ip hotspot user set');
$client($setRequest
->setArgument('numbers', $hotspotUsername)
->setArgument('password', $_POST['password'])
);
}
}

?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Change your hotspot password</title>
<style type="text/css">
#errors {background-color: darkred; color: white;}
#success {background-color: darkgreen; color: white;}
</style>
</head>
<body>
<div>
<?php if (!isset($hotspotUsername)) { ?>
<h1>We're sorry, but we can't change your password right now.
Please try again later</h1>
<?php } else { ?>
<h1>
<br> HOTSPOT ... password change FORM <br><br>
You are currently logged in as "<?php
echo $hotspotUsername;
?>"</h1>

<?php if(!empty($errors)) { ?>
<div id="errors"><ul>
<?php foreach ($errors as $error) { ?>
<li><?php echo $error; ?></li>
<?php } ?>
</ul></div>
<?php } elseif (isset($_POST['password'])) { ?>
<div id="success">Your password has been changed.</div>
<?php } ?>

<form action="" method="post">
<ul>
<li>
<label for="password">New password:</label>
<input type="password" id="password" name="password" value="" />
</li>
<li>
<label for="password2">Confirm new password:</label>
<input type="password" id="password2" name="password2" value="" />
</li>
<li>
<input type="submit" id="act" name="act" value="Change password" />
</li>
</ul>
</form>
<?php } ?>
</div>
</body>
</html>
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2369
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: Change of Password for hotspot users

Thu May 03, 2018 9:02 am

I got this code online and created a script named changepass.php by clicking on plus button in winbox system and then script.
That's not how PHP or even the API in general works. RouterOS can't run PHP. You need to create the file on a separate device (or a KVM/MetaRouter), and have a web server and PHP installed on that device. When users access that device, and more specifically, that web page, they would be able to ask the web server to modify their hotspot password.
PEAR2_Net_RouterOS(1.0.0b6) - My API client in PHP
(Rate my posts? If you want... no pressure...)
 
faogundele
just joined
Topic Author
Posts: 10
Joined: Wed May 02, 2018 5:01 pm

Re: Change of Password for hotspot users

Fri May 04, 2018 2:58 pm

How can i use edit in /ip hotspot user edit command?

Any one with help?
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2369
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: Change of Password for hotspot users

Fri May 04, 2018 11:13 pm

The API protocol doesn't support "edit" commands. But to alter a property, you can use "set" as in the example code above.

That API client in particular supports an edit() method that is really an alias for the above type of "set", e.g. the part
$setRequest = new RouterOS\Request('/ip hotspot user set');
$client($setRequest
->setArgument('numbers', $hotspotUsername)
->setArgument('password', $_POST['password'])
);
can be replaced with
$util = new RouterOS\Util($client);
$util->setMenu('/ip hotspot user')->edit($hotspotUsername, 'password', $_POST['password']);
(but under the hood, it would be turned into the first code)
PEAR2_Net_RouterOS(1.0.0b6) - My API client in PHP
(Rate my posts? If you want... no pressure...)
 
faogundele
just joined
Topic Author
Posts: 10
Joined: Wed May 02, 2018 5:01 pm

Re: Change of Password for hotspot users

Mon May 07, 2018 1:16 pm

(but under the hood, it would be turned into the first code)

Thanks for your response, however, what do you mean by (but under the hood, it would be turned into the first code)
 
faogundele
just joined
Topic Author
Posts: 10
Joined: Wed May 02, 2018 5:01 pm

Re: Change of Password for hotspot users

Mon May 07, 2018 2:33 pm

Is there a way I can run a script in mikrotik core router box to change hotspot password?

Please i need urgent help.
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2369
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: Change of Password for hotspot users

Mon May 07, 2018 3:13 pm

(but under the hood, it would be turned into the first code)

Thanks for your response, however, what do you mean by (but under the hood, it would be turned into the first code)
I mean that if you analyze the source code for the RouterOS\Util::edit() method, you'll see that it ultimately does the same thing as the first piece of code - it creates a RouterOS\Request object for the "set" command at the menu, fills up the "numbers" and other arguments, and sends the request via the RouterOS\Client object.

In other words, whether you do the first or second piece of code, the same things will end up going to the router, and the same things will be returned by the router.
Is there a way I can run a script in mikrotik core router box to change hotspot password?

Please i need urgent help.
If your router has enough RAM, create a MetaRouter guest, or if your router is an x86 PC - create a KVM guest. Install any OS onto it (OpenWrt will do), install Apache and PHP, configure Apache so that PHP can run on it, and place the files within the MetaRouter/KVM. Connect to the "real" machine through the internal IP configured between RouterOS and the guest.

That's the general process, but I've never fully completed it myself to give you a detailed step-by-step guide.

If you don't have enough RAM to spare for a MetaRouter/KVM, you're out of luck - you need to get a separate device and connect it to the router in some way, be it VPN or normally.
PEAR2_Net_RouterOS(1.0.0b6) - My API client in PHP
(Rate my posts? If you want... no pressure...)
 
faogundele
just joined
Topic Author
Posts: 10
Joined: Wed May 02, 2018 5:01 pm

Re: Change of Password for hotspot users

Tue May 08, 2018 11:14 am

Thanks for your response. I will give it a trial and actually get back to you on my result.

Details of my mikrotik router
Product code CCR1009-7G-1C-1S+PC
Architecture TILE
CPU TLR4-00980CH-10CE-A3b
CPU core count 9
CPU nominal frequency 1 GHz
Dimensions 272x190x47mm
License level 6
Operating System RouterOS
Size of RAM 2 GB
Storage size 128 MB
Storage type NAND
Tested ambient temperature -20C .. +60C



Will this RAM size (2 GB) be enough?
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2369
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: Change of Password for hotspot users

Tue May 08, 2018 9:38 pm

The router model would've been sufficient (unless it was an x86 PC) :D

How much RAM you need depends on which OS you're going to install, and i don't know of a TILE compatible one you can use. In general, if you can spare at least 256MiB for the guest, that would most probably work.
PEAR2_Net_RouterOS(1.0.0b6) - My API client in PHP
(Rate my posts? If you want... no pressure...)
 
faogundele
just joined
Topic Author
Posts: 10
Joined: Wed May 02, 2018 5:01 pm

Re: Change of Password for hotspot users

Wed May 09, 2018 1:08 pm

Thanks so much for your response. If any information is out there regarding metarouter on tile architecture, kindly inform me.
 
faogundele
just joined
Topic Author
Posts: 10
Joined: Wed May 02, 2018 5:01 pm

Re: Change of Password for hotspot users

Thu Jun 14, 2018 11:12 pm

please I need urgent help. I have installed Ubuntu, Apache, PHP, MySQL, phpmyadmin tested and running. static IP enabled.

I have mikrotik routerboard on tile architecture

I want to achieve user self change of hotspot password and also to send mikrotik syslog to external webserver

1. can I run the above php script to enable change of hotspot password by self.

2. do I need to load users credentials on webserver before it can work

3. or do I use external login php script to do authentication, where will be users credentials hosted.
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2369
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: Change of Password for hotspot users

Fri Jun 15, 2018 9:17 am

The hotspot user credentials remain hosted on the router.

The only thing you need to give to the script are credentials for a RouterOS user. From then on, the idea is similar to logging in from a terminal, and doing the commands on the router... Except that you're letting PHP do them automatically when users submit the form, instead of you manually doing them when users phone you or whatever.

The whole flow is that users go to that web page, they submit the new password they want, then PHP connects to the given router with the given RouterOS credentials, and performs a "set" command for the password of the currently logged in hotspot user.
PEAR2_Net_RouterOS(1.0.0b6) - My API client in PHP
(Rate my posts? If you want... no pressure...)
 
faogundele
just joined
Topic Author
Posts: 10
Joined: Wed May 02, 2018 5:01 pm

Re: Change of Password for hotspot users

Sat Jun 16, 2018 12:23 am

Thanks alot, I have done that. I tested phpinfo on the url to know if the php is working properly. it displayed php information installed same thing with Apache. however when I run above script on browser url bar, the page displayed is blank and white.

what could be the problem or error?
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2369
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: Change of Password for hotspot users

Sun Jun 17, 2018 12:08 am

There are several possible reasons. To narrow it down, you'll need to see some error messages from PHP.

Open up your php.ini, and find the lines "display_errors", "error_reporting", "error_log", and set them to
display_errors = Off
log_erros = On
error_reporting = E_ALL | E_STRICT
error_log = "php_errors.log"
You may open phpinfo() again to confirm the settings are in effect. Then start the file again, and check for a php_errors.log in the same folder. Open it up, and see what it says.


The most frequent cause people encounter is that they need to allow PHP and/or Apache to make outgoing connections. If PHP is running as an Apache module, whitelist Apache, or if you're running PHP as FCGI, whitelist "php-cgi".
PEAR2_Net_RouterOS(1.0.0b6) - My API client in PHP
(Rate my posts? If you want... no pressure...)

Who is online

Users browsing this forum: No registered users and 2 guests