Community discussions

MikroTik App
 
lukef
newbie
Topic Author
Posts: 35
Joined: Mon Jul 07, 2008 4:48 am

user manager administration

Wed Nov 05, 2008 2:23 am

Hey Guys i need some help with making a script that removes old users from user manager.
so far i have the following
system scheduler add name="Remove Users out of Credit" interval=168:00:00 on-event="/tool user-manager remove [tool user-manager user find credit-left=0]"
The issue with this is it will remove all users out of credit. I would rather have anothe filter that will remove all users out of credit that were last seen over a month ago
Is this possible. I have spent some time mucking around with the last-seen command but have not been able to use it in the above situation

Any help appreciated
 
fosben
Frequent Visitor
Frequent Visitor
Posts: 81
Joined: Thu Dec 14, 2006 4:50 pm

Re: user manager administration

Wed Nov 05, 2008 1:35 pm

my best guess is to get the last-seen value as a variable , then format it into smaller strings that you can run a test on..

This finds todays date and split them up in 3. Youll need to make a loop for each customer, get its last seen date and compare it to todays date if it has 0 credit...
:local varDate;
:local varMonth;
:local varDay;
:local varYear;
:set varDate [/system clock get date];
:set varMonth [:pick $varDate 0 3];
:set varDay [:pick $varDate 4 6];
:set varYear [:pick $varDate 7 11];
One problem is that the month is returned in letters (like 'oct') and not in numbers, so you have to find a workaround for that too. Simplest way I guess is to check if the month variables from todays date and last-seen is not equal and if todays date(day)=28 then remove the user with 0 credit. I used 28 because of february only have 28 days... That way they would be removed at the end of the month, and since the month values are not equal, it has gone atleast 29 days since he was active

remember to /tool user-manager database save name="userm_db_backup"
before you execute scritpt on your real routerboard :)
 
lukef
newbie
Topic Author
Posts: 35
Joined: Mon Jul 07, 2008 4:48 am

Re: user manager administration

Thu Nov 06, 2008 5:16 am

Hey Fosben
Thanks so much for your help, With your guidance i have come up with this script.
This script removes users if they are out of credit and haven't been seen for a month.

NOTE
Ensure the date is set correctly on routeros before using.
Also it will remove unlimited accounts if they haven't been seen for a month

:local varDate
:local varMonth
:local varDay
:local varYear
:local varlastseen
:local varlastseenmonth
:local varcredit
:local user
:set varDate [/system clock get date];
:set varMonth [:pick $varDate 0 3];




/tool user-manager user
:foreach i in=[ /tool user-manager user find subscriber=hs_admin ] do={
:set varlastseen [get $i last-seen]
:set varcredit [get $i credit-left]
:set user [get $i name]
:set varlastseenmonth [:pick $varlastseen 0 3];
:if ([$varcredit] = 00:00:00) do={
:if ([$varlastseenmonth] != [$varMonth] ) do={
/tool user-manager user remove $i
}
}
}
 
lukef
newbie
Topic Author
Posts: 35
Joined: Mon Jul 07, 2008 4:48 am

Re: user manager administration

Thu Nov 06, 2008 8:34 am

Take 2; the script above will actually delete unlimited accounts if they have never been logged into..

### This Script removes stale Accounts if they are out of credit and haven't been seen for a month or if they have an unlimited account and havent been seen for a month

:local varDate
:local varMonth
:local varDay
:local varYear
:local varlastseen
:local varlastseenmonth
:local varcredit
:local user
:set varDate [/system clock get date];
:set varMonth [:pick $varDate 0 3];




/tool user-manager user
:foreach i in=[ /tool user-manager user find subscriber=hs_admin ] do={
:set varlastseen [get $i last-seen]
:set varcredit [get $i credit-left]
:set user [get $i name]
:set varlastseenmonth [:pick $varlastseen 0 3];
:if ([$varcredit] = 00:00:00) do={
:if ([$varlastseenmonth] = "nev") do={ :nothing} else={
:if ([$varlastseenmonth] != [$varMonth] ) do={
/tool user-manager user remove $i
:log info "The following User $user Has been Reomved using StaleAccountRemoval"

}
}
}

}
 
fosben
Frequent Visitor
Frequent Visitor
Posts: 81
Joined: Thu Dec 14, 2006 4:50 pm

Re: user manager administration

Thu Nov 06, 2008 1:34 pm

yes, that script would work if you only ran it at the end of each month. If you run it every day, you will have a small disaster ;)

Who is online

Users browsing this forum: No registered users and 44 guests