Community discussions

MikroTik App
 
stefanosp
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 54
Joined: Tue Mar 01, 2011 1:01 pm
Location: Northern Italy

Random Time Script

Sun Apr 04, 2021 7:36 pm

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 .. :-)

Thanks in advance
 
User avatar
Jotne
Forum Guru
Forum Guru
Posts: 3291
Joined: Sat Dec 24, 2016 11:17 am
Location: Magrathean

Re: Random Time Script

Sun Apr 04, 2021 9:58 pm

See here on how to generate number in RouterOS.
viewtopic.php?f=9&t=164114

You may convert this to some time settings.
 
stefanosp
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 54
Joined: Tue Mar 01, 2011 1:01 pm
Location: Northern Italy

Re: Random Time Script

Mon Apr 05, 2021 8:05 am

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"]
 
User avatar
Jotne
Forum Guru
Forum Guru
Posts: 3291
Joined: Sat Dec 24, 2016 11:17 am
Location: Magrathean

Re: Random Time Script

Mon Apr 05, 2021 11:49 am

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
}
 
msatter
Forum Guru
Forum Guru
Posts: 2897
Joined: Tue Feb 18, 2014 12:56 am
Location: Netherlands / Nīderlande

Re: Random Time Script

Mon Apr 05, 2021 1:19 pm

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"]
Here the final version can be found, including inline help with the parameters.

viewtopic.php?f=9&t=169030

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.
 
stefanosp
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 54
Joined: Tue Mar 01, 2011 1:01 pm
Location: Northern Italy

Re: Random Time Script

Mon Apr 05, 2021 2:32 pm

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
}
Thank you! I'll use your version :-)
 
msatter
Forum Guru
Forum Guru
Posts: 2897
Joined: Tue Feb 18, 2014 12:56 am
Location: Netherlands / Nīderlande

Re: Random Time Script

Mon Apr 05, 2021 3:40 pm

Thank you! I'll use your version :-)
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.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Random Time Script

Sat May 22, 2021 8:07 pm

I write my own version:

Random number between 0 and 99 or string between 00 and 99
viewtopic.php?f=9&t=175453&p=858629

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"
 
tabate47
Long time Member
Long time Member
Posts: 510
Joined: Wed Mar 13, 2013 5:23 am
Location: Los Angeles

Re: Random Time Script

Thu Apr 20, 2023 2:43 pm

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!
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Random Time Script

Thu Apr 20, 2023 2:55 pm

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

example code

: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

example code

{
: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"
}
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Random Time Script

Thu Apr 20, 2023 3:19 pm

 
tabate47
Long time Member
Long time Member
Posts: 510
Joined: Wed Mar 13, 2013 5:23 am
Location: Los Angeles

Re: Random Time Script

Thu Apr 20, 2023 3:54 pm

Thanks Rex

Who is online

Users browsing this forum: No registered users and 27 guests