LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   need help to generate dynamic password between 8 - 16 characters (https://www.linuxquestions.org/questions/linux-newbie-8/need-help-to-generate-dynamic-password-between-8-16-characters-4175521366/)

sherzod33 10-07-2014 04:40 PM

need help to generate dynamic password between 8 - 16 characters
 
i have password generator but i cant get it to generate dynamic length. any idead how to do it?

if [ $# -eq 0 ]; then
num=("0" "1" "2" "3" "4" "5" "7" "8" "9")
special=("@" "#" "$" "%" "*" "-" "+")
upper=(A B C D E F G H I J K L M N O P Q R S T U V W X Y Z)
lower=(a b c d e f g h i j k l m n o p q r s t u v w x y z)
lower2=${#lower[*]} #${lower[$((RANDOM%lower2))]}
upper2=${#upper[*]} #${upper[$((RANDOM%upper2))]}
num2=${#num[*]} #${num[$((RANDOM%num2))]}
special2=${#special[*]} #${special[$((RANDOM%special2))]}

echo "${special[$((RANDOM%special2))]}${num[$((RANDOM%num2))]}${lower[$((RANDOM%lower2))]}${upper[$((RANDOM%upper2))]}${lower[$((RANDOM%lower2))]}${upper[$((RANDOM%upper2))]}${lower[$((RANDOM%lower2))]}${upper[$((RANDOM%upper2))]}" | fold -w 1 | shuf | tr -d '\n'
echo
fi

Kustom42 10-07-2014 04:51 PM

A little off the wall, first can you encap your code in CODE tags, will help from getting a 10ft horizontal scroll bar :)

You can use random to generate a random number between a set of integers, then use that as your length for your password. Having a hard time decyphering this pw generator script so couldnt tell you how to implement in your current code, just thinking about this I might do it slightly different myself. I would just set one array and then use random in a while loop to append a character until it reached the pwlength value.


Code:

pwlength=$((RANDOM%16+8))

rknichols 10-07-2014 09:00 PM

Depending on how those passwords are intended to be used, you might want to filter out obscenities, threats against public officials, remarks that might be considered offensive to Belgians, etc.

How to do that?? Hmmmm,....

You could publish the password candidate on Facebook and see if anyone notices a problem with it. :rolleyes:

Or, just don't use any vowels.

eklavya 10-09-2014 02:04 AM

If you want to generate random password between 8-16 character. Try this.

Code:

#!/bin/bash
n=$(shuf -i 8-16 -n 1)
cat /dev/urandom | tr -dc 'a-zA-Z0-9-!@#$%^&*()_+~' | fold -w $n | head -n 1


But you want to use your code and you want modification in it, try this.
Code:

#/bin/bash
if [ $# -eq 0 ]; then
num=("0" "1" "2" "3" "4" "5" "7" "8" "9")
special=("@" "#" "$" "%" "*" "-" "+")
upper=(A B C D E F G H I J K L M N O P Q R S T U V W X Y Z)
lower=(a b c d e f g h i j k l m n o p q r s t u v w x y z)
lower2=${#lower[*]} #${lower[$((RANDOM%lower2))]}
upper2=${#upper[*]} #${upper[$((RANDOM%upper2))]}
num2=${#num[*]} #${num[$((RANDOM%num2))]}
special2=${#special[*]} #${special[$((RANDOM%special2))]}
n=$(shuf -i 8-16 -n 1)
echo "${special[$((RANDOM%special2))]}${num[$((RANDOM%num2))]}${lower[$((RANDOM%lower2))]}${upper[$((RANDOM%upper2))]}${lower[$((RANDOM%lower2))]}${upper[$((RANDOM%upper2))]}${lower[$((RANDOM%lower2))]}${upper[$((RANDOM%upper2))]}${special[$((RANDOM%special2))]}${num[$((RANDOM%num2))]}${lower[$((RANDOM%lower2))]}${upper[$((RANDOM%upper2))]}${lower[$((RANDOM%lower2))]}${upper[$((RANDOM%upper2))]}${lower[$((RANDOM%lower2))]}${upper[$((RANDOM%upper2))]}" | fold -w $n | head -n 1
fi


zhjim 10-09-2014 02:17 AM

Not really answering the question but did you check out the binary pwgen?


All times are GMT -5. The time now is 01:02 AM.