Random String

Hi All
I recently started to code some scripts on MK, and i really wanted to have a script that could generate a random string and that string i would then use to weekly change my wi-fi password. After bit investigation, i did not find anything that could immediately sort my issue so i decided to code my own script and i believe it might be useful for the others if it doesnt exist yet.
The below script was tested and work on 6.32 and 6.32.3 versions. Give it a try and please give suggestions.


Copy and paste from here:

This Script Was Created BY TSOKOTSA 2015 and it was made to Generate a Random String With 8 Digits for The GUEST WI-FI Internet

Here i get the hour, minutes and seconds

:local time [:pick [/system clock get time] 0 2];
:local min [:pick [/system clock get time] 3 5];
:local sec [:pick [/system clock get time] 6 8];

for each value that i pick, i also get the inverse combination of the value. lets say that the seconds are 29 by the time # the script executes, i also get the 92 which is the inverse of the 29. i do that for all values time, minutes and sec.

:local char;
:set char ( $char . [ :pick $time 1 ] . [ :pick $time 0 ]);

:local char1;
:set char1 ( $char1 . [ :pick $min 1 ] . [ :pick $min 0 ]);

:local char2;
:set char2 ( $char2 . [ :pick $sec 1 ] . [ :pick $sec 0 ]);

Here i have an array. This array must be long enough to accommodate the sum of the highest possible values of the # combination.

e.g the highest possible of normal time / hour / sec is 23:59:59. so the inverse of that would be 32:95:95

so the sum of that inverse should not be longer than the array. so have that in mind.

On my array i did generate a random string online you can use mine or generate your own with your possible values.

:local arrayalpha ("2","C","h","Y","!","f","j","c","}","q","k",":","(","}","3","Y","T","|","C","v","}","n","8","I","r","4","p","V","6",":","S","V","=","p","Z","T","6",")","l",":","K","b","=","Y","7","X","c","P","@","g","m","U","T","","g","v","N","j","E","g","f","D","h","!","W","p","U","z","T","S","}","h","M","Y","i","E","c","4","!","Q","(","<","Y","h","@","e","q","7","]","l","R","-","h","S","w","(",">","/","r","=","<","I","&","5","@","!","|","h","p","l","1","Y","U","2","J","R","T","Y","}","=","b","c","$","9","d","e","@","S","6","W","B","Q","$","S","b","p","}","3","m","X","g","8","y","j","h","S","Z","F","@","!","t","}","!","U","j","/","W","5","}","a","g","!","S","S","T","{","7","f","G","W","T","","/","k","3","+","T","X","p","m","!","e","i","L","]","3","d","G","T","+","@","H","!","Q","k","X","@","P","Q","+","k","f","K","T","S","{","Q","W","S","<","S","z","z","k","V","@","X","{","4","g","t","X","1","h","5","k","T","","f","i","{","]","(","Y","/","1","j","h","l","]","S","3","b","Y",":","i","T","j","h","T","7","w","g","D","X","g","T","T","i","|","S","R","!","}","D","d","c","h","d","T","l","N",")","V","u","Z","T","}",":","{","S","m","Z","d","}","Z","Z","S","Y","|","","d","k","v","h","(","7","/","-");

How do i ensure a complex string? Because it is impossible to determinate the seconds at the time the script is

executed and in addition to that it is almos impossible to determinate the inverse number of the seconds and most # impossible to determinate the sum of the inverses.

:local new;
:set new ( $new . [ :pick $arrayalpha ($time+$min+$sec) ] . [ :pick $arrayalpha ($time+$sec) ] . [ :pick $arrayalpha ($min+$sec) ] . [ :pick $arrayalpha $sec ] . [ :pick $arrayalpha $char ] . [ :pick $arrayalpha $char1 ] . [ :pick $arrayalpha $char2 ] . [ :pick $arrayalpha ($char+$char1+$char2) ]);

\

This line doesnt actulay make any difference to the code, it only determinate the array lengh and can be commented. it is only used for debugging purposes.

:local arrlengh [:len $arrayalpha];

:log warning "Our Array Lengh is $arrlengh Your New String is $new";

If you face any error executing the script, might be because of the comments so check comments to clear error.

Just forgot to mention on my script that you can do whatever you want with the string generated although i did not show that on my script. I actually send the generated string via email to my guests and i also use the same string to automaticaly update the wi-fi password.

Hi thanks for your code. :smiley:

I inspected a little bit your code, and add mine code so i upgraded your script with more features, like e-mail new password. Sound good right?

This is entire full script for generating new password, update specified security profile and e-mail the new password to specified email.

#################################################
###WiFi Password Generator Script mod by DrMTR ###
###  www.mikrotikmacedonia.net                 ###
#################################################

:local wifiprofile wifi_security_profile
:local toEmail yourmail@domain.com

:local time [:pick [/system clock get time] 0 2];
:local min [:pick [/system clock get time] 3 5];
:local sec [:pick [/system clock get time] 6 8];
:local char;
:set char ( $char . [ :pick $time 1 ] . [ :pick $time 0 ]);
:local char1;
:set char1 ( $char1 . [ :pick $min 1 ] . [ :pick $min 0 ]);
:local char2;
:set char2 ( $char2 . [ :pick $sec 1 ] . [ :pick $sec 0 ]);
:local arrayalpha ("2","C","h","Y","!","f","j","c","}","q","k",":","(","}","3","Y","T","|","C","v","}","n","8","I","r","4","p","V","6",":","S","V","=","p","Z","T","6",")","l",":","K","b","=","Y","7","X","c","P","@","g","m","U","T","_","g","v","N","j","E","g","f","D","h","!","W","p","U","z","T","S","}","h","M","Y","i","E","c","4","!","Q","(","<","Y","h","@","e","q","7","]","l","R","-","h","S","w","(",">","/","r","=","<","I","&","5","@","!","|","h","p","l","1","Y","U","2","J","R","T","Y","}","=","b","c","$","9","d","e","@","S","6","W","B","Q","$","S","b","p","}","3","m","X","g","8","y","j","h","S","Z","F","@","!","t","}","!","U","j","/","W","5","}","a","g","!","S","S","T","{","7","f","G","W","T","_","/","k","3","+","T","X","p","m","!","e","i","L","]","3","d","G","T","+","@","H","!","Q","k","X","@","P","Q","+","k","f","K","T","S","{","Q","W","S","<","S","z","z","k","V","@","X","{","4","g","t","X","1","h","5","k","T","_","f","i","{","]","(","Y","/","1","j","h","l","]","S","3","b","Y",":","i","T","j","h","T","7","w","g","D","X","g","T","T","i","|","S","R","!","}","D","d","c","h","d","T","l","N",")","V","u","Z","T","}",":","{","S","m","Z","d","}","Z","Z","S","Y","|","_","d","k","v","h","(","7","/","-");
:local new;
:set new ( $new . [ :pick $arrayalpha ($time+$min+$sec) ] . [ :pick $arrayalpha ($time+$sec) ] . [ :pick $arrayalpha ($min+$sec) ] . [ :pick $arrayalpha $sec ] . [ :pick $arrayalpha $char ] . [ :pick $arrayalpha $char1 ] . [ :pick $arrayalpha $char2 ] . [ :pick $arrayalpha ($char+$char1+$char2) ]);
:local arrlengh [:len $arrayalpha];
:log warning "Our Array Lengh is $arrlengh Your New String is $new";
:interface wireless security-profiles set $wifiprofile wpa-pre-shared-key="$new";
:interface wireless security-profiles set $wifiprofile wpa2-pre-shared-key="$new";
:tool e-mail send to=$toEmail subject="$[/system identity get name] WiFi Password" body="This Week's Wireless Password Is: $new";
:log info message="New WiFi Password Has Been Emailed"

And add one Scheduler to execute weekly.

/system scheduler
add comment="E-Mail New Wireless Password" disabled=no interval=1w name=MailPassword on-event=name_of_script policy=\
ftp,reboot,read,write,policy,test,winbox,password,sniff,sensitive start-date=nov/06/2015 start-time=00:00:00

Screens from executing script:

and new password sent to my Gmail account:

If you like my job, can you give me Rep+. Thanks !!

NOTE : Tested With RoS 6.20.

Hi deanMKD1

Thank you for adding that to the script however you will notice on my second post i do mention that send the generated string via email already and automatically update the wi-fi passwd i just did not include that on my script. Indeed i also send a copy via SMS message since my MK is integrated with a GSM modem.
I already have it scheduled to run weekly but i did not post on my script because what i wanted to bring is the string password generate that seems to be something new.

This was an old thread to comment.
To get a random password:
http://forum.mikrotik.com/t/one-line-password-generation-without-fetch-tool/141623/1