Hi,
I am just doing my first steps in scripting. I would appreciate your help for a noob question:
[admin@MikroTik] /queue simple> :global currentrate;
[admin@MikroTik] /queue simple> :set currentrate [get [find name=“NonVoIP”] rate]
[admin@MikroTik] /queue simple> :put $currentrate
12bps/917.5kbpsNow I would like to assign from string variable currentrate integer value 12 to integer variable uprate and integer value 917500 to integer variable downrate. How can I achieve this? This seems hard to me, but should be usual practice as it occurs often in RouterOS.
Any hint greatly appreciated !!!
Thank you.
Try this to start:
[admin@test] > :put [:pick "12bps/917.5kbps" 0 [find "12bps/917.5kbps" "/" -1]]
12bps
[admin@test] > :put [:pick "12bps/917.5kbps" ([find "12bps/917.5kbps" "/" -1]+1) 20]
917.5kbps
I think you can probably figure out the rest.
SurferTim,
thank you very much. So I will have to manually break up the string into pieces and convert the parts to integer values. Then I will have to detect whether those pieces contain “bps” oder “kpbs” or “mbps” and multiply the preceding values accordingly by 1/1000/1E6. Yes, this should be possible using your code fragment as a guideline. Huh, but this is quite tricky.
I am wondering whether this day-by-day task to read a RouterOS variable with two integer components (e.g., up-/down rates), which has been coded as one string by mikrotik, has no easier solution.
Anyway, thanks again!
on the same path… if there is any better way please let me know …
best regards,
917.5kbps if you remove kbps remain 917.5 that on RouterOS is still a string because do not support decimal numbers, so, on that case:
123.45Mbps/917.5kbps
split at bps/ and -3 length and you have:
123.4M and 917.5k
If you want omogeneous numbers, convert both on bit:
check if are present M or K and remove
check if present any dot inside, if present split at dot and you have two numbers
examples:
123 4 M
so, M mean that is “123” + “4” + significative zeros + “000” = 123400000
971 5 k
so, k mean that is “971” + “5” + significative zeros = 971500
{
:local manage do={
:local value [:tostr $1]
:local integer “”
:local decimal “”
:local multiplier 1
:if ($value~“k”) do={:set multiplier 1000 ; :set value [:pick $value 0 ([:len $value] - 1)]}
:if ($value~“M”) do={:set multiplier 1000000 ; :set value [:pick $value 0 ([:len $value] - 1)]}
:if ($value~“\.”) do={
:set integer ([:tonum [:pick $value 0 [:find $value “.” -1] ] ] * $multiplier)
:set decimal [:pick $value ([:find $value “.” -1] + 1) [:len $value]]
:set decimal ([:tonum [:pick “$decimal\30000” 0 3]] * ($multiplier / 1000))
:return ($integer + $decimal)
} else={
:return ([:tonum $value] * $multiplier)
}
:return 0
}
:local readedValue "2.3kbps/2.79Mbps"
:local bpsPosition [:find $readedValue "bps/" -1]
:local ulValue [:pick $readedValue 0 $bpsPosition]
:local dlValue [:pick $readedValue ($bpsPosition + 4) ([:len $readedValue] - 3)]
:put “$ulValue = $[$manage $ulValue]bps”
:put “$dlValue = $[$manage $dlValue]bps”
}