Hello,
I would like to add an user to the Hotspot and would like to send the sms to the client with his new username / password to the hotspot.
there are two questions:
- which is better: to add through the Webfig to internal user database, or to the userman database (I find userman limited, as it doesn’t leave a log that new user is added, but I’m still open for discussion). Also, the internal database has no field for phone and the comment is the only field I could possibly use for it.
- how to get the script working - parsing logs, parsing /tool user-manager user export?
Is there any sane way of doing this hack?
Thanks.
You could use something like FreeRADIUS to store users in. I think it has a dedicated field for a phone, though I’m not sure honestly.
Alternatively, yeah. You’d need to use the comment field on users for that. You could create your own desktop/web interface (a.k.a. a “billing system”) that ensures you won’t fill in an invalid number in there, and then eventually adds the user via the API protocol.
The actual procedure of “sending an SMS upon registration” is something you’ll need to either do yourself (perhaps again via the API protocol if the SMS gateway is attached to the router), or use a pre-existing billing system that implements it.
You could use something like FreeRADIUS to store users in. I think it has a dedicated field for a phone, though I’m not sure honestly.
The actual procedure of “sending an SMS upon registration” is something you’ll need to either do yourself (via your own app - be it a web or desktop one), or use a pre-existing “billing system” that implements it.
No no no no no! I, thinking simpler, freeradius is always welcome, but I just not need the full blown panel. Webig is perfectly fine, as long as it sends sms to customer. I’m trying to implement script for monitoring log and set some global variables (username, his phone from comment and password0 and then I’m to send this via /tool sms.
Wanted to ask if its proper way hack, as I fail to code the things I want currently (still trying).
Well, OK, maybe not a “full blown” panel, but just a single custom web panel from where you do these things?
It’s pretty trivial to make a form that would add the user via “User Manager”, then automatically writes a log entry and sends an SMS via “/tool sms”.
It’s pretty trivial to make a form that would add the user via “User Manager”, then automatically writes a log entry and sends an SMS via “/tool sms”.
yeah, you say pretty trival… im fighting this since 2 hours without any luck and its all yuck 
- user manager doesn’t send ANY logs about adding new user, thats why I choose parsing standard logs and go with webfig (it is fine to not use user manager). So how can I trigger an event from usermanager, huh? am I supposed to parse /tool user-manager user print??
- the script in bash would be fine perl ok, but this mt scripts I still learn… no success
for example, there is a script just for log monitoring:
http://wiki.mikrotik.com/wiki/Manual:Scripting-examples#Detect_new_log_entry
but how to make it actually run something?..
Well, I suppose I should’ve added “trivial, assuming you know HTML and PHP”.
You don’t need to parse the output of a “print” - that’s what clients of MikroTik’s API protocol do for you. In fact, why would you need to print anything?
Here’s such a form I cooked up in the last half an hour (including the time taken to take care for “distractions”):
<?php
use PEAR2\Net\RouterOS;
require_once 'PEAR2_Net_RouterOS-1.0.0b4.phar';
header('Content-Type: text/html;charset=UTF-8');
$errors = array();
try {
//Adjust RouterOS IP, username and password accordingly.
$util = new RouterOS\Util(
$client = new RouterOS\Client('192.168.0.1', 'admin', 'password')
);
} catch(Exception $e) {
//Inspect $e if you need details
$errors[] = 'Unable to connect to RouterOS';
}
if (empty($errors) && isset($_POST['act'])) {
if (empty($_POST['username'])) {
$errors[] = 'A username is required.';
}
if (empty($_POST['phone'])) {
$errors[] = 'A phone is required.';
}
if (empty($_POST['password']) && empty($_POST['password2'])) {
$errors[] = 'A password is required.';
} elseif (empty($_POST['password']) || empty($_POST['password2'])
|| $_POST['password'] !== $_POST['password2']) {
$errors[] = 'Passwords do not match.';
}
if (empty($errors)) {
$util->changeMenu('/tool user-manager user');
$userId = $util->add(
array(
'customer' => 'admin',
'name' => $_POST['username'],
'password' => $_POST['password'],
'phone' => $_POST['phone']
)
);
if ('' === $userId) {
$errors[] = 'Failed to add user. Possibly because username already exists.';
}
if (empty($errors)) {
$logRequest = new RouterOS\Request('/log info');
$logRequest->setArgument('message', 'Added user "' . $_POST['username'] . '".');
if (count($client->sendSync($logRequest)) > 1) {
$errors[] = 'User was added, but the log was not written to.';
}
$smsRequest = new RouterOS\Request('/tool sms send port=usb1');
$smsRequest->setArgument('phone-number', $_POST['phone']);
$smsRequest->setArgument(
'message',
'Your username is "' . $_POST['username'] . '", and your password is "' . $_POST['password'] . '".'
);
if (count($client->sendSync($smsRequest)) > 1) {
$errors[] = 'User was added, but no SMS was sent.';
}
}
}
}
?><!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>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
<title>Add user</title>
<style type="text/css">
#errors {background-color:darkred;color:white;}
#success {background-color:darkgreen;color:white;}
</style>
</head>
<body>
<div>
<?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['act'])) { ?>
<div id="success">User added.</div>
<?php } ?>
<form action="" method="post" accept-charset="UTF-8">
<ul>
<li>
<label for="username">Username:</label>
<input type="text" id="username" name="username" value="<?php echo isset($_POST['username']) ? htmlspecialchars($_POST['username'], ENT_COMPAT, 'UTF-8') : ''; ?>" />
</li>
<li>
<label for="password">Password:</label>
<input type="password" id="password" name="password" value="" />
</li>
<li>
<label for="password2">Confirm password:</label>
<input type="password" id="password2" name="password2" value="" />
</li>
<li>
<label for="phone">Phone:</label>
<input type="text" id="phone" name="phone" value="<?php echo isset($_POST['phone']) ? htmlspecialchars($_POST['phone'], ENT_COMPAT, 'UTF-8') : ''; ?>" />
</li>
<li>
<input type="submit" id="act" name="act" value="Add user" />
</li>
</ul>
</form>
</div>
</body>
</html>
Nice performance
I will get this to myself, even though… I can’t use the PHP here
the thing was I needed more than adding users the way to sms them with their creds…
I’ve also got squeezed the forum and found working enough solution: http://forum.mikrotik.com/t/is-it-possible-to-run-a-script-on-login/55422/1
Thank you 
the thing was I needed more than adding users the way to sms them with their creds…
It sounds like pretty soon, you’d end up getting a “full blown” panel after all
.
True. Nice api btw. If im again in mt i will definately look at it.