2 ms·
There are several scenarios where a cryptographic-quality random number generator might be needed within the context of a shell script. My favorite tool for th
by chasil 4y ago
There are several scenarios where a cryptographic-quality random number generator might be needed within the context of a shell script.
My favorite tool for this is "openssl rand" and I can find the manual page on rhel7 with "man rand."
Here are 8 bytes of randomness in base64:
$ openssl rand -base64 8
mGHxYcVOmA8=
If you like decimal:
$ printf %d\\n 0x$(openssl rand -hex 2)
21924
When I've got dozens of users to create, this is how I am assigning their initial passwords.
The shell's $RANDOM is somewhat less trustworthy than other sources here. It also does not appear to be POSIX, but comes from ksh88, where it is described as returning a random integer between 0 and 32767 each time it is referenced. If unset in a script, it cannot be used again for the life of the shell.
- TRiG_Ireland 4y agoI use this to create passwords when I need to manually: function password_generate { head -c ${1-12} /dev/urandom | base64; }
- chasil 4y agoSometimes, extreme portability makes us do desperate things. # uname -a HP-UX prdcrm B.10.20 A 9000/800 862741461 two-user license # ls -l /dev/urandom /dev/urandom not found # openssl rand -base64 8 v51U1RvSD2Q= I could run this on VAX VMS if you want.