Community discussions

MikroTik App
 
User avatar
TLL
just joined
Topic Author
Posts: 5
Joined: Sun Aug 14, 2022 6:10 am

Increment the suffix of hotspot users by 1 for every run

Mon Feb 12, 2024 9:40 am

This script works by incrementing the suffix by 1 for every run, but the logic for numbers beyond 10 is not functioning correctly. It reverts back to 01 after reaching 10 instead of progressing to 11. Can anyone help me?

-----------------------------------------------------------
:local baseUsernames ("ELIJAH", "CHARLOTTE");
:local limitUptime "00:00:01";

:foreach baseUsername in=$baseUsernames do={
# Find the user in the hotspot user list
:local userIndex [/ip hotspot user find where name~"$baseUsername"];

# Check if user was found
:if ($userIndex != "") do={
# Get the current username
:local currentUser [/ip hotspot user get $userIndex name];

# Extract the suffix from the current username
:local currentSuffix;
:if ([:len $currentUser] > [:len $baseUsername]) do={
:set currentSuffix [:pick $currentUser ([:len $baseUsername] + 1) [:len $currentUser]];
} else={
:set currentSuffix "00";
}

# Convert the suffix to a number and increment it by 1
:local newSuffix ([:tonum $currentSuffix] + 1);

# Check if the new suffix exceeds 30, if so, set it to 30
:if ($newSuffix > 30) do={
:set newSuffix 30;
}

# Convert the new suffix to string format with proper padding
:local paddedNewSuffix;
:if ($newSuffix < 10) do={
:set paddedNewSuffix ("0" . $newSuffix);
} else={
:set paddedNewSuffix $newSuffix;
}

# Set the new username with the incremented suffix
:local newUsername ($baseUsername . $paddedNewSuffix);

# Set new name
/ip hotspot user set $userIndex name=$newUsername password=$newUsername;

:log info ("User $currentUser renamed to $newUsername successfully.");
} else={
:log warning ("User $baseUsername not found.");
}
}
-----------------------------------------------------------
You do not have the required permissions to view the files attached to this post.
 
jaclaz
Long time Member
Long time Member
Posts: 667
Joined: Tue Oct 03, 2023 4:21 pm

Re: Increment the suffix of hotspot users by 1 for every run

Mon Feb 12, 2024 12:10 pm

Cannot say if it is the cause of the issue, but at first sight this:
# Convert the new suffix to string format with proper padding
:local paddedNewSuffix;
:if ($newSuffix < 10) do={
:set paddedNewSuffix ("0" . $newSuffix);
} else={
:set paddedNewSuffix $newSuffix;
}
is "suspect", as a side note, in other scripting languages, to pad with a leading 0, I would prepend a 0 anyway and then take only the two rightmost characters of the suffix.
The $newsuffix value may or may not become a string paddedNewSuffix without enclosing it in double quotes.

Anyway, to troubleshoot, add some :put statements and look at what the values are at the various stages.
 
User avatar
TLL
just joined
Topic Author
Posts: 5
Joined: Sun Aug 14, 2022 6:10 am

Re: Increment the suffix of hotspot users by 1 for every run

Mon Feb 12, 2024 12:33 pm

I included the *.rsc attachment. Can you try it on your router, maybe you can understand the error.
 
jaclaz
Long time Member
Long time Member
Posts: 667
Joined: Tue Oct 03, 2023 4:21 pm

Re: Increment the suffix of hotspot users by 1 for every run

Mon Feb 12, 2024 1:50 pm

I have not a router or emulator handy right now, what I meant is, are you sure that this:
:set paddedNewSuffix ("0" . $newSuffix);
} else={
:set paddedNewSuffix $newSuffix;
is valid, or should it be:
:set paddedNewSuffix ("0" ."$newSuffix");
} else={
:set paddedNewSuffix "$newSuffix";
 
User avatar
TLL
just joined
Topic Author
Posts: 5
Joined: Sun Aug 14, 2022 6:10 am

Re: Increment the suffix of hotspot users by 1 for every run

Mon Feb 12, 2024 2:35 pm

Yes, I tried, but there is still no difference. The script still reverts back to 01 after reaching 10.
I see you are quite familiar with scripting. Can you help me? I gave remote access to one of the routers, just a router for the lab, it's chr cloud.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 12014
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Increment the suffix of hotspot users by 1 for every run

Mon Feb 12, 2024 2:41 pm

 
jaclaz
Long time Member
Long time Member
Posts: 667
Joined: Tue Oct 03, 2023 4:21 pm

Re: Increment the suffix of hotspot users by 1 for every run

Mon Feb 12, 2024 3:11 pm

No, I am not expert (actually I am a beginner) in Mikrotik scripting, though I have some experience in other kind of scripts.

This is why I say "suspect", handling the type of variables is often tricky in many scripting languages and Mikrotik seemingly is not an exception.

Check these:
viewtopic.php?t=150664#p744074
viewtopic.php?t=111595

You must make sure that at each step the variables you use are of the appropriate type.
 
User avatar
TLL
just joined
Topic Author
Posts: 5
Joined: Sun Aug 14, 2022 6:10 am

Re: Increment the suffix of hotspot users by 1 for every run

Mon Feb 12, 2024 3:28 pm

@rextended, @jaclaz
Thank you for the reply.

Actually what I want to try to achieve:

For example, let's say I have hotspot users named "ELIJAH27" and "CHARLOTTE2".
I need "ELIJAH27" change to "ELIJAH28" and "CHARLOTTE2" change to "CHARLOTTE3" when a specific condition is met (In this case, just the prefix and limit uptime).

The first step:
1. Isolate Prefix and Suffix for users "ELIJAH27" or "CHARLOTTE2".
"ELIJAH" and "CHARLOTTE" are the Prefix, and "27" and "2" are the Suffix.
2. As a Prefix, "ELIJAH" and "CHARLOTTE" in a name does not change.
3. As a Suffix, format the "27" and "2" as a numeral and ensure that the suffix is formatted as a numeral from 1 to 30.

The second step:
Use the suffix as described below:
Find hotspot users, iterate through all hotspot users and identify those with the prefix "ELIJAH" and "CHARLOTTE" and has limit-uptime of 1 second. If no user with that prefix is found, breaks out of the loop,
but when the prefixes "ELIJAH" and "CHARLOTTE" are found, update the username and password by incrementing the suffix by 1.
 
jaclaz
Long time Member
Long time Member
Posts: 667
Joined: Tue Oct 03, 2023 4:21 pm

Re: Increment the suffix of hotspot users by 1 for every run

Mon Feb 12, 2024 5:18 pm

Yep, I got what you want to do, the doubt I have is that at a certain point in your script you are using :tonum so the variable becomes surely a number, then after the arithmetic manipulation of the number you seem like not explicitly converting the result to a string, using :tostr.

To verify/troubleshoot, if you add at each step something *like*:
:put $myVar
:put [:typeof $myVar]
you can verify that the variable is the right type

Who is online

Users browsing this forum: No registered users and 3 guests