Increment the suffix of hotspot users by 1 for every run

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.");
}
}

ip hotspot user.rsc (440 Bytes)
system script.rsc (2.34 KB)

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.

Thank you for the reply.

I included the *.rsc attachment. Can you try it on your router, maybe you can understand the error.

I included the *.rsc attachment. Can you try it on your router, maybe you can understand the error.

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”;

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.

http://forum.mikrotik.com/t/can-you-control-the-length-of-the-generated-string-in-ros-6-xx/173241/1

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:
http://forum.mikrotik.com/t/string-comparaition/131954/1
http://forum.mikrotik.com/t/compare-2-strings-with-numbers/101049/1

You must make sure that at each step the variables you use are of the appropriate type.

@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.

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