I tried pwgen and makepasswd to generate a password...
but these are not enough to generate with exact count of literals and numbers...

pwgen -c -n -y -B  12 1

This is not working as expected as I cannot define min numbers count...
I want to define min and max chars allowed in each type of literals...
does crunch have this feature?
is there any other tool you suggest for this?
shell scripts are most welcome... (with regex)

Thanks in advance


Just run it until it works (in a newer version of bash that supports the =~ for regex matching):

unset pass; 
until [[ \
        $pass =~ [0-9].*[0-9] && 
        $pass =~ [A-Z].*[A-Z] && 
        $pass =~ [^A-Za-z0-9] \
      ]]; do 
    pass=$(LC_ALL=C pwgen -c -n -y -B  12 1) 

For example:

$ unset pass; until [[ $pass =~ [0-9].*[0-9] && $pass =~ [A-Z].*[A-Z] && $pass =~ [^A-Za-z0-9] ]]; do pass=$(LC_ALL=C pwgen -c -n -y -B  12 1); done; echo $pass

Note that I am defining "symbol" as "not a letter and not a number". You can choose something more restrictive if you like.

If your shell doesn't support regex matching, try:

$ perl -le 'until ($pass =~ /\d.*\d/ && $pass =~ /[A-Z].*[A-Z]/ && $pass =~ /[^A-Za-z0-9]/){chomp($pass=`LC_ALL=C pwgen -c -n -y -B  12 1`)}; print "$pass"'
| improve this answer | |

if you have GNU grep with Perl mode installed, this can generate a password as per constraints.

$ < /dev/urandom \
   tr -cd '[:alnum:][:punct:]' | fold -w 12 |
   grep -Pm1 '(?=.*[[:upper:]].*[[:upper:]])(?=.*[[:digit:]].*[[:digit:]])(?=.*[[:punct:]])'

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.