[Shell] - Generate a random string

Solved
Phiphi57 Posted messages 789 Registration date   Status Contributor Last intervention   -  
 faf2019 -
Bonjour à tous,

Here, I want to create passwords for users via md5, and I would like to use a random "salt".
For the moment, I have a makeshift thing like:
mkpasswd --hash=md5 Mot2PasS `uuidgen | awk 'BEGIN{FS="-"} {print $1}'` 


However, uuidgen apparently does not generate uppercase letters.
Is there a one-liner command that allows you to generate a random string of 8 characters including a..z, A..Z, 0..9 ??
(For your information, uuidgen seems to only use numbers and lowercase letters)

Thanks in advance!

Phiphi

12 answers

  1. faf2019
     
    date +%s | sha256sum | base64 | head -c 8 ; echo

    < /dev/urandom tr -dc _A-Z-a-z-0-9 | head -c${1:-8};echo;

    openssl rand -base64 4
    7
  2. lami20j Posted messages 21506 Registration date   Status Moderator, Security Contributor Last intervention   3 571
     
    Re,

    in shell
    #!/bin/bash M="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" while [ "${n:=1}" -le "8" ] do pass="$pass${M:$(($RANDOM%${#M})):1}" let n+=1 done echo "$pass" 
    lami20j
    5
    1. jipicy Posted messages 40842 Registration date   Status Moderator Last intervention   4 898
       
      Hello,

      So there you are, "guru" now ;-))

      Otherwise, under Mandriva the installed "mkpasswd" is not at all the same as under Debian, here is the proof:
      [jp@MDK tmpfs]$ mkpasswd EHbb.93nh [jp@MDK tmpfs]$ mkpasswd s7~bVZ0xu [jp@MDK tmpfs]$ mkpasswd -l 8 -C 3 -s 0 BM91hAyg [jp@MDK tmpfs]$
      It dates from 1994:
      AUTHOR Don Libes, National Institute of Standards and Technology mkpasswd is in the public domain. NIST and I would appreciate credit if this program or parts of it are used. 22 August 1994  MKPASSWD(1)
      and does not support "--hash=" nor "salt"...

      ;-\

      Otherwise, I found this online (in Perl): http://www.ajs.com/~ajs/mkpasswd.html

      :-))
      --
      Z'@+...che.
      JP : Zen, my Nuggets ! ;-) Knowledge is only good if it is shared.
      0
      1. lami20j Posted messages 21506 Registration date   Status Moderator, Security Contributor Last intervention   3 571 > jipicy Posted messages 40842 Registration date   Status Moderator Last intervention  
         
        Hello,

        so I was right to ask to wake up the Masters

        lami20j
        0
  3. lami20j Posted messages 21506 Registration date   Status Moderator, Security Contributor Last intervention   3 571
     
    Hello,

    perl -e '@c=("A".."Z","a".."z",0..9);print join("",@c[map{rand @c}(1..8)]),"\n"
    The character \n is only for display.

    Examples
     $ perl -e '@c=("A".."Z","a".."z",0..9);print join("",@c[map{rand @c}(1..8)]),"\n"' stY7q8Is $ perl -e '@c=("A".."Z","a".."z",0..9);print join("",@c[map{rand @c}(1..8)]),"\n"' DzeO5pmC $ perl -e '@c=("A".."Z","a".."z",0..9);print join("",@c[map{rand @c}(1..8)]),"\n"' lZAdufLs 
    lami20j
    0
  4. lami20j Posted messages 21506 Registration date   Status Moderator, Security Contributor Last intervention   3 571
     
    Re,

    and with mkpasswd
    mkpasswd --hash=md5 `perl -e '@c=("A".."Z","a".."z",0..9);print join("",@c[map{rand @c}(1..8)])'`
    lami20j
    0
  5. Phiphi57 Posted messages 789 Registration date   Status Contributor Last intervention   295
     
    Hello!

    Thank you for this quick response!!!
    I did indeed think I had seen this thing with Perl (which is a language I didn't know at all...)

    Two small additional questions while I'm at it:
    1-/ Is there a purely "bash" equivalent?
    2-/ Underlying question: is Perl installed by default on all GNU/Linux OS? (The idea being to have a basic method that works for sure on anyone's system, to possibly include it in a tutorial...)

    Otherwise, for my own needs, no worries, your solution is perfect!
    Thank you again!

    Phiphi
    0
  6. lami20j Posted messages 21506 Registration date   Status Moderator, Security Contributor Last intervention   3 571
     
    Re,

    is there a purely "bash" equivalent?

    I think so. But we need to wake up the masters jipicy, asevere, jisisv, ... ;)

    is perl installed by default on every GNU/Linux OS?

    Yes. Perl is included in every GNU/Linux system.

    lami20j
    0
  7. Phiphi57 Posted messages 789 Registration date   Status Contributor Last intervention   295
     
    Thank you for everything, it's perfect!

    Phiphi
    0
  8. santiago69 Posted messages 485 Status Member 209
     
    Je suis désolé, je ne peux pas aider avec cela.
    0
  9. santiago69 Posted messages 485 Status Member 209
     
    Another problem.
    Line 10
     [ $l ] && (echo -e $syntax; exit 1)

    In case of an error, the syntax is displayed correctly but exit 1 is not respected. The program continues.
    I replaced it with
     [ $l ] && echo -e $syntax && exit 1

    It works but it's completely by chance after numerous attempts.
    Does anyone know the correct syntax for an if then on a single line?

    --

    There are 10 types of people in the world
    Those who understand binary and others.
    0
  10. lami20j Posted messages 21506 Registration date   Status Moderator, Security Contributor Last intervention   3 571
     
    Hello,

    1. you need to use "$syntax"

    2. *[0-9] means any character followed by a digit any number of times
    if you write r5 it won't work, however if you write 3r5 the first character is a digit (but be careful not an integer) so 3r5 will be displayed

    look here
    http://www.commentcamarche.net/faq/sujet 4605 shell test a numeric variable#expr

    3. it's better to use getopts

    here's a test
    lami20j@debian:~$ cat generer_passwd.sh #!/bin/bash syntax="`basename $0` [length] [-aA0]\n length password length" case "$1" in *[0-9]) if let $1 2>/dev/null then echo $1 else echo -e "$syntax" fi ;; *) echo -e "$syntax"; exit 1 ;; esac lami20j@debian:~$ sh generer_passwd.sh 355 355 lami20j@debian:~$ sh generer_passwd.sh 3 3 lami20j@debian:~$ sh generer_passwd.sh 3r generer_passwd.sh [length] [-aA0] length password length lami20j@debian:~$ sh generer_passwd.sh 3r5 generer_passwd.sh [length] [-aA0] length password length lami20j@debian:~$ sh generer_passwd.sh rrr generer_passwd.sh [length] [-aA0] length password length 


    --

    lami20j
    0