Random Time Script

Goodmorning everyone,

I’m writing a script (01) which will then be applied on various different routers.
This script (01) adds in routeros a script (02) and a scheduler that activates this script (02). I would like the execution time of this new script (02) to be differentiated on each of the routers on which it will be active.

Is there a way to generate a random time every time that script (01) is run?
Is there any way to convert an arbitrary string to an integer?

I apologize if I can’t make it clearer .. :slight_smile:

Thanks in advance

See here on how to generate number in RouterOS.
http://forum.mikrotik.com/t/one-line-password-generation-without-fetch-tool/141623/1

You may convert this to some time settings.

Thank you.

I coded this and it seems to work

:local otp ([/certificate scep-server otp generate minutes-valid=0 as-value]->"password")
:local a
:local b
:local c
:local hour
:local min
:local sec
:local rndTime

:set $a [:pick "$otp" 0 1]
:set $b [:pick "$otp" 1 2]
:set $c [:pick "$otp" 2 3]
:set $hour (([:find "0123456789abcdef" "$a" -1] * 256 + [:find "0123456789abcdef" "$b" -1] * 16 + [:find "0123456789abcdef" "$c" -1]) % 24)

:set $a [:pick "$otp" 4 5]
:set $b [:pick "$otp" 5 6]
:set $c [:pick "$otp" 6 7]
:set $min (([:find "0123456789abcdef" "$a" -1] * 256 + [:find "0123456789abcdef" "$b" -1] * 16 + [:find "0123456789abcdef" "$c" -1]) % 60)

:set $a [:pick "$otp" 8 9]
:set $b [:pick "$otp" 9 10]
:set $c [:pick "$otp" 10 11]
:set $sec (([:find "0123456789abcdef" "$a" -1] * 256 + [:find "0123456789abcdef" "$b" -1] * 16 + [:find "0123456789abcdef" "$c" -1]) % 60)

:set rndTime ($hour . ":" . $min . ":" . $sec)
:put $rndTime

/certificate scep-server otp remove [find password="$otp"]

Clever solution.

Here is a small update.
You can declare variable first time its used.
Added leading “0” to look better if its only one digit.

{
:local otp ([/certificate scep-server otp generate minutes-valid=0 as-value]->"password")

:local a [:pick "$otp" 0 1]
:local b [:pick "$otp" 1 2]
:local c [:pick "$otp" 2 3]
:local hour [:tostr (([:find "0123456789abcdef" "$a" -1] * 256 + [:find "0123456789abcdef" "$b" -1] * 16 + [:find "0123456789abcdef" "$c" -1]) % 24)]
:if ([:len $hour]=1) do={set $hour ("0".$hour)}

:set $a [:pick "$otp" 4 5]
:set $b [:pick "$otp" 5 6]
:set $c [:pick "$otp" 6 7]
:local min [:tostr (([:find "0123456789abcdef" "$a" -1] * 256 + [:find "0123456789abcdef" "$b" -1] * 16 + [:find "0123456789abcdef" "$c" -1]) % 60)]
:if ([:len $min]=1) do={set $min ("0".$min)}

:set $a [:pick "$otp" 8 9]
:set $b [:pick "$otp" 9 10]
:set $c [:pick "$otp" 10 11]
:local sec [:tostr (([:find "0123456789abcdef" "$a" -1] * 256 + [:find "0123456789abcdef" "$b" -1] * 16 + [:find "0123456789abcdef" "$c" -1]) % 60)]
:if ([:len $sec]=1) do={set $sec ("0".$sec)}

:local rndTime ($hour . ":" . $min . ":" . $sec)
:put $rndTime
}

Here the final version can be found, including inline help with the parameters.

http://forum.mikrotik.com/t/password-pin-and-and-hash/144676/1

I looked at your request and thought about adding a random time in the future. I would set a time range from a set time (now/future) to a second one set in future.Then generate a random number in that range.

I would use two EPOCH times and deduct max from low to get the range. Count the digits and generate a random number fitting that number of digits. Deduct this random from the max time.
If the result time is lower than the low time do it over till it fits or quick and dirty, divide the random result time by a random divider.

…or I could add ownbase-range as [6-24] as a number between 6 and 24. 6 Is the current hour or a current hour of 4+2 generating a time that lays 2 hours in the future.

Thank you! I’ll use your version :slight_smile:

Hmmm I am still in the thinking/design mode for the time part. The main challenge is to transferring and interpreting the parameters. I have already a idea to implement it and it is not that difficult going step by step. I will post when I am ready and in the meantime you keep using your version.

Yours is dedicated to time and I am extending my version with time. My version does a lot more which you don’t need right now.

I write my own version:

Random number between 0 and 99 or string between 00 and 99
http://forum.mikrotik.com/t/random-number-between-0-and-99-or-string-between-00-and-99/149267/1

If you want near-random time, but near-unique for upload some file on some ftp and you not want congestionate connection for do that all at the same time,
you can use the last 3 octet of original ether1 MAC address like:

:global Gorimac [/int eth get ether1 orig-mac-address]
:global shour (([:tonum ("0x".[:pick $Gorimac 9 11])] % 23) +1)
:global smin ([:tonum ("0x".[:pick $Gorimac 12 14])] % 60)
:global ssec ([:tonum ("0x".[:pick $Gorimac 15 17])] % 60)

:put "start-time=$shour:$smin:$ssec"

Rex,

I think your script will work for my needs.

I only want to generate a time between 1am and 5am.

What do I need to change in order to do this?

Thanks!

1am to 5am are 4 hours, or 14400 seconds

14400/100 = 144 seconds

So, for each one from 0 to 99, each number is 144 seconds
:global randomnum do={
/system resource irq
:local tmpsum 0
:foreach i in=[find] do={:set tmpsum ($tmpsum + [get $i count])}
:set tmpsum [:tostr $tmpsum]
:local lentmp [:len $tmpsum]
:return [:tonum [:pick $tmpsum ($lentmp - 2) $lentmp]]
}

{
:local randomtime (01:00:00 + [:totime (([$randomnum] + 1) * 144)])
:put $randomtime
}
On MAC, % 4 can produce 0, 1, 2 or 3, so the intervai si from 01:00:00 to 04:59:59
{
:local Gorimac [/int eth get ether1 orig-mac-address]
:local shour (([:tonum (“0x”.[:pick $Gorimac 9 11])] % 4) + 1)
:local smin ( [:tonum (“0x”.[:pick $Gorimac 12 14])] % 60 )
:local ssec ( [:tonum (“0x”.[:pick $Gorimac 15 17])] % 60 )

:local starttime “$shour:$smin:$ssec”

:put “start-time=$starttime”
}

Thanks Rex