Random line in linux by system time

I work with bash. I want to generate a randrom string by system time. The unique string must be between 10 and 30 characters long. Can anybody help me?

+6
source share
3 answers

There are many ways to do this, my favorite using a urandom device:

 burhan@sandbox :~$ tr -cd '[:alnum:]' < /dev/urandom | fold -w30 | head -n1 CCI4zgDQ0SoBfAp9k0XeuISJo9uJMt 
  • tr (translation), make sure that only alphanumeric characters are displayed
  • fold wrap it up to 30 characters wide
  • head ensures that we get only the first line

To use the current system time (since you have this specific requirement):

 burhan@sandbox :~$ date +%s | sha256sum | base64 | head -c30; echo NDc0NGQxZDQ4MWNiNzBjY2EyNGFlOW 
  • date +%s = this is our settlement based on date.
  • We run it through several hashes to get a "random" string
  • Finally, we trim it to 30 characters.

Other methods (including two of the above) are available on this page and others if you are just google .

+10
source

Perhaps you can use uuidgen -t .

Create a UUID based on time. This method creates a UUID based on the system clock plus the system address of the ethernet equipment, if present.

+4
source

I recently put together a script for processing, the output is a 33-digit md5 checksum, but you can trim it with sed to 10-30.

eg. gen_uniq_id.bsh | sed 's/\(.\{20\}\)\(.*$\)/\1/'

The script is quite reliable - it uses the current time for nanoseconds, / dev / urandom, mouse movement data and allows, if necessary, changing the data collection time for data collection using random and mice.

It also has the -s option, which allows you to include an optional string argument so that you can randomly select anything.

https://code.google.com/p/gen-uniq-id/

0
source

Source: https://habr.com/ru/post/949452/


All Articles