I’m trying to create a script that will send syslog messages once a trial user logs in at a hotspot site. I have configured sending log messages to my syslog server and I’ve defined a script in /system script. The script name is associated with the on-login action of my hotspot user profile assigned to trial users.
The script source is as follows:
:local freeuser [/ip hotspot active find where user=$user];
:local freemac [/ip hotspot active get $freeuser mac-address];
:local freeip [/ip hotspot active get $freeuser address];
:log info ($freemac." ".$freeip);
This works great, but only if there is only one user logged in (only one entry in /ip hotspot active). If there are 2 and more users, I get an error message:
interrupted
invalid internal item number
There seems to be some problem with the $user variable. I’m trying to use it, because the wiki article about hotspot user profiles suggests so. When I try to use the variable directly, for example:
:log info $user
I get a syntax error.
I’m doing it all on a RB433 running RouterOS v5.16.
Where am I wrong?
My modification of your script was spontaneous
but now I’ve tested this script and it works for me as expected (ROS 5.21 on x86).
Does script run counter increase when new trial user log-in?
have you tried running the script while having more than one user listed in /ip hotspot active?
The run counter increments as new users log in, but the script shows no effect if there is more than one active user.
Best regards,
Jacek
EDIT:
I’m trying to investigate the meaning of the $user variable - the way it’s described in the wiki:
on-login (string; Default: “”) Script name to be executed, when user logs in to the HotSpot from the particular profile. It is possible to get username from internal user variable. For example, :log info “User $user logged in!”
I changed the script to the following code:
:local a [/ip hotspot active find where user=$user]; :log info $a;
When I log my second user into the hotspot I get for example:
echo: script,info: *A141EFC,*A141EFE
in the log.
What does the variable $user do? What’s it for anyway? I’m not able to select the user I need using this variable.
:local freeuser [/ip hotspot active find user=$user];
:local freemac [/ip hotspot active get $freeuser mac-address];
:local freeip [/ip hotspot active get $freeuser address];
:log info ($freemac." ".$freeip);
@jacekes
Unfortunatelly, today I was unable to test this script with few simultaneous trial users, I’ll try tomorrow.
echo: script,info: *A141EFC,*A141EFE
AFAIK they are ROS internal hotspot users IDs, I’m suprised that your script returned two ID instead of one.
Every trial user name is dynamically created and is unique, because is MAC-based, so my script should work with many simultaneous trial users.
Did you try example script from Wiki?
:log info "User $user logged in!"
Does it work properly with more than one trial user logged-in???
If not, it should be clearly explained by MT or considered as a bug.
@c0d3rSh3ll, thanks, I’ll try it tomorrow, because I’ve got my RB433 at my office. But I think I’ve tried it already.
@ditonet, I know that these “*A…” are internal ID’s, I’m just confused with not being able to select one particular ID using the $user variable. The example script from the wiki description doesnt’t work at all - it’s impossible to create it. When I type:
system scr add name=test source=":log info \"user $user has logged in\""
in the console, I get a syntax error at the “$” sign. I guess I’ve tried and was able to create the script from winbox and it didn’t work.
I hope the guys from Mikrotik Support will notice this topic, if not, I’ll email them.
I got an answer from Maris from Mikrotik Support.
The script source containing the $user variable has to be defined in the /ip hotspot user profile on-login parameter instead of just the name of an external script defined in /system script.
It works as it should.
Have you or anyone else reading this been able to figure out a way to use the /ip hotspot user profile on-login $user variable to log the things like the MAC?
:local freeuser [/ip hotspot active find user=$user];
:local freemac [/ip hotspot active get $freeuser mac-address];
:local freeip [/ip hotspot active get $freeuser address];
:log info ($freemac." ".$freeip);
works correctly and logs the MAC address and IP address of the user. You just need to set the script code as the value of /ip hotspot user profile on-login parameter.
If the script is defined in /system script and the name of the script is set as the on-login value, the $user variable won’t work.
Excuse me as I have been banging my head for a day or so , but is there any chance I could bum an export of your profile on-login?
If I’m understanding correctly my config should be like this:
on-login=“:local freeuser [/ip hotspot active find user=$user];
:local freemac [/ip hotspot active get $freeuser mac-address];
:local freeip [/ip hotspot active get $freeuser address];
:log info ($freemac." ".$freeip);”
/ip hotspot user profile
set [ find default=yes ] idle-timeout=none keepalive-timeout=2m on-login=":local who \$user;\r\
\n:local whomac [/ip hotspot active get [find user=\$who] mac-address];\r\
\n:local whoip [/ip hotspot active get [find user=\$who] address];\r\
\n:log info (\$whomac.\" \".\$whoip);"
Thanks for the export…Yea, not sure what the hang up is, I had tried such a variation with no success…
Something simple like:
on-login=":log info \"Hello \$user\""
Will log as it should, even though /system script…
But anything extended like yours:
on-login=":local who \$user;\r\
\n:local whomac [/ip hotspot active get [find user=\$who] mac-address];\r\
\n:local whoip [/ip hotspot active get [find user=\$who] address];\r\
\n:log info (\$whomac.\" \".\$whoip);"
Just won’t log for some reason…
If you or anyone else has a thought to what I should check, but if it simply works for you than I’d have to wonder about the box… What’s the lowest version you’ve successfully ran such a script with? My box is still at 4.17
I’ve tried it only on v5.21. I might have some time next week to downgrade my 433 and check if it works.
In the meantime you can try evolving your simple yet working script. For example try to substitute the value of $user to a new local variable , for example:
:local tempvar $user;
:log info tempvar
… and so on, adding next commands leading to something similar to the code I’ve pasted in my previous post.
This way you might be able to catch the point, at which the script goes wrong.
Have fun!
Edit: One more thing - do it with winbox. Scripting in command line is nasty, escaping all those special characters and stuff.