Password Generator (BASH)
A password generator in BASH. It uses /dev/urandom to help generate a random alphanumeric password
By default it will generate a 16 character password with special characters, but accepts various options to control what the password is comprised of
gen_passwd [Length] [nc (No special chars) | lc (lower case no specials) | pin (numeric only) | mixed (default)
Based upon the encryption key generator in my backup encryption scripts
Similar To
pwgen
Details
- Language: BASH
- License: GNU GPL V2
Snippet
gen_passwd(){
keylen=${1-16}
charflag=$2
if [ "$keylen" == "--help" ] || [ "$keylen" == "-h" ]
then
echo "$0 [Length] [nc (No special chars) | lc (lower case no specials) | pin (numeric only) | mixed (default)]"
return 1
fi
if [ "$charflag" == "nc" ]
then
chars='a-zA-Z0-9'
elif [ "$charflag" == "lc" ]
then
chars='a-z0-9'
elif [ "$charflag" == "pin" ]
then
chars='0-9'
else
chars='a-zA-Z0-9-_!@#$%^&*()_+{}|:<>?='
fi
cat /dev/urandom | tr -dc "$chars" |fold -w $keylen| head -n 1
}
Usage Example
gen_passwd # 16 Char alphanumeric password, mixed characters
gen_passwd 12 # 12 Char alphanumeric password, mixed characters
gen_passwd 12 nc # 12 Char alphanumeric password, no special characters
gen_passwd 12 lc # 12 Char alphanumeric password, lower case chars, no special characters
gen_passwd 6 pin # 6 Char PIN (numbers only)