hi,
For one scenario, it is necessary for users to have only 3 sessions for each username to connect to the VPN. To solve this problem, I have used the script in the UP section of the profile, but the script does not work properly, please help.
:local allowed 3;
:local scount 0;
:local nama “$user”;
:local sess [/ppp active find where name=$nama]
:local callerid [/ppp active get [find name=$nama] caller-id];
:log warning "PPP User ($nama) Logged in from $callerid ";
:foreach sessid in= $sess do={
:set scount ($scount + 1);
:if ($scount <= $allowed ) do={
:log warning “Session ($scount) find for User: ($nama) is ($sessid) Allowed!”;
} else={
/ppp active remove [find session-id=$sessid];
:log error “Session ($scount) find for User: ($nama) is ($sessid) not Allowed and disconnected!”;
}
}
If you wrap you code in bracket { } and cut/paste to terminal, you would have see what was wrong.
For debugging in terminal change :log warning to :put to see the output
This line is missing a $
:set scount ($scount + 1);
correct
:set $scount ($scount + 1)
No need for ; at end of each line, use tab in code and post it with code tags like this version that should work:
:local allowed 3
:local scount 0
:local nama "$user"
:local sess [/ppp active find where name=$nama]
:local callerid [/ppp active get [find name=$nama] caller-id]
:log warning "PPP User ($nama) Logged in from $callerid "
:foreach sessid in= $sess do={
:set $scount ($scount + 1)
:if ($scount <= $allowed ) do={
:log warning "Session ($scount) find for User: ($nama) is ($sessid) Allowed!"
} else={
/ppp active remove [find session-id=$sessid]
:log error "Session ($scount) find for User: ($nama) is ($sessid) not Allowed and disconnected!"
}
}
Test version to debug in terminal. Change ass_test_user_name to a vpn user name
{
:local allowed 3
:local scount 0
:local nama "add_test_user_name"
:local sess [/ppp active find where name=$nama]
:local callerid [/ppp active get [find name=$nama] caller-id]
:put "PPP User ($nama) Logged in from $callerid "
:foreach sessid in= $sess do={
:set $scount ($scount + 1)
:if ($scount <= $allowed ) do={
:put "Session ($scount) find for User: ($nama) is ($sessid) Allowed!"
} else={
# /ppp active remove [find session-id=$sessid]
:put "Session ($scount) find for User: ($nama) is ($sessid) not Allowed and disconnected!"
}
}
}
Edit
I see in ppp user session that you can set user login to only one or many. MT should add a number instead and you have not needed the script.
:global, :local and :set do not want $,
if you want "set" something inside array like :set ($array->"item") "value" apparently you must use $ in front of variable,
but really at that point the "name" of the variable is "passed" from the function (...) and you use the $ because the variable is inside the function (->)
I can make mistakes too, RouterOS is quite forgiving with syntax errors,
but then sometimes things don't work as expected anymore when they fix some problem, or whatever...
better to write immediately with the correct syntax...
some error check for understand
{
added path for avoid multiple time same string
/ppp active
:local allowed 3
:local scount 0
removed useless quotes
:local nama "$user"
:local nama $user
removed path for avoid multiple time same string
:local sess [/ppp active find where name=$nama]
:local sess [find where name=$nama]
already $sess is [find where name=$nama], why call it again???
(also missing where on previous line)
not fixed now: this line potentially can do error if is present more than one ppp session with same name
/ppp active remove [find session-id=$sessid]
remove [find where session-id=$sessid]
:log error "Session ($scount) find for User: ($nama) is ($sessid) not Allowed and disconnected!"
}