samedi 9 avril 2016

BASH - check singel character in array bash for password generator

I am trying to test User input with password rules.

must be at least 8 characters long but not longer than 16 characters.
must contain at least 1 digit (0-9).
must contain at least 1 lowercase letter.
must contain at least 1 uppercase letter.
must contain exactly one and only one of @ #  $  %  &  *  +  -  = 

and I'm trying to figure how I can compare the user string to the arrays that contain the password characters

    #!/bin/bash



    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""o""p""q""r""s""t""u""v""w""x""y""z")

    Numbers=("1""2""3""4""5""6""7""8""9")

    SpecialChar=("&""@""#""$""%""*""-""+""=")

Password Generator

    PassGen(){      # generate password if no user input


    Pwlength=`shuf -i 8-16 -n 1`  # holds the range/length of password

    Password=`< /dev/urandom tr -dc A-Za-z0-9$SpecialChar | head -c $Pwlength` 

    echo "Random Password is being generated for you"
    sleep 5

    echo "Your new password  is : $Password"

    echo exit?

    }

I want to make the following to work :

\#PassGen2(){ # generate password  2 if no user input
\#if [ $# -eq 0 ] ; then
\#for ((i=0;i<4;i++)) 
\#do    
\#password="${Upper[$random % ${#Upper[@]}" ] }
\#password="${Lower[$random % ${#Lower[@]}" ] }
\#password="${Numbers[$random % ${#Numbers[@]}" ] } 
\#done  
\#password="${SpecialChar[$random % ${#SpecialChar[@]}" ] }
\#password="${Upper[$random % ${#Upper[@]}" ] }

\#echo "Random Password is being generated for you"
\#sleep 5

\#echo "Your new password  is : $Password"

\#fi
\#echo exit?
\#}

Help menu

Usage(){ # Help menu 

echo "The password must meet the following :"
echo "> must be at least 8 characters long but not longer than 16 characters."
echo "> must contain at least 1 digit (0-9)."
echo "> must contain at least 1 lowercase letter."
echo "> must contain at least 1 uppercase letter."
echo "> must contain exactly one and only one of @ #  $  %  &  *  +  -  ="
echo ""
echo " * * IF NO PASSWORD IS ENTERED THE SYSTEM WILL GENERATE A PASSPORD FOR YOU * * "

echo exit?

}   

The main starts here

if [ $# -eq 0 ] ; then 

PassGen $SpecialChar

fi

if [ $# == "-h"] ; then

Usage

fi


UserPW=$1 # assigning the 1th positional parameter to veriable (password)

The reason I open the post

    PwCheck(){   

    if [[ ${#UserPW} < 8 && ${#UserPW} > 0 ]] ;then  #check the lower end of the password 

    echo "Password have to be more then 8 character"
    exit 1
    fi
    if [[ ${#UserPW} < 16 ]] ;then #checks if the password bigger then 16 character

    echo "Password too long ! - Password have to be between 8 - 16 characters"

    exit 1
    fi

    if [[ ${#UserPW < 17 && ${#UserPW} > 8 ]] ; then 

    Pwarr=($UserPW) # putting the variable into Array 

        for (( i=0; i < ${#Upper[@]};i++ ))  
        do

            if [[ ${Pwarr[0]:[i]:1} != ${Upper[i]} ]]; then
            echo "You have to enter at least 1 upper character letter" 
            exit 1
            fi
        done
        for (( i=0; i < ${#Lower[@]};i++ ))
        do
            if [[ ${Pwarr[0]:[i]:1} != ${Lower[i] ]]; then
            echo "You have to enter at least 1 Lower character letter"
            exit 1
            fi
        done
        for (( i=0; i < ${#SpecialChar[@];i++ ))
        do
            if [[ ${Pwarr[0]:[i]:1} != ${SpecialChar[i] ]] ;then
            echo " You have to enter at least 1 special character 
            exit 1
            fi
        done

    #}

  • 1) I couldnt make my PassGen2() to work beside "calling it" I know there must be typo or syntax errors
  • 2) For PwCheck() function I know I didnt call it and the IF statment should be in the main section



Aucun commentaire:

Enregistrer un commentaire