Last active
June 25, 2023 15:27
-
-
Save mttjohnson/43b7ccf44601889a74d4063048e9cb68 to your computer and use it in GitHub Desktop.
Generate a password from the shell
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Defined function for generating random password | |
genpasswd() { | |
local l=$1 | |
[ "$l" == "" ] && l=20 | |
LC_CTYPE=C tr -dc A-Za-z0-9_ < /dev/urandom | head -c ${l} | xargs | |
} | |
NEW_PASSWORD=$(genpasswd 16) | |
echo $NEW_PASSWORD | |
# Alternate condensed form | |
NEW_PASSWORD=$(LC_ALL=C tr -dc 'A-Za-z0-9_' < /dev/urandom | head -c 16) | |
NEW_PASSWORD=$(LC_ALL=C tr -dc 'A-Za-z0-9%&()*+,-./:;<>?@[]^_{}~' </dev/urandom | head -c 16) | |
echo $NEW_PASSWORD | |
# Generate 5 random words | |
WORDS=5; LC_ALL=C grep -x '[a-z]*' /usr/share/dict/words | shuf --random-source=/dev/urandom -n ${WORDS} | tr '\n' ' ' | sed 's/ $/\n/' | |
# Alternate using uuidgen | |
NEW_PASSWORD=$(uuidgen) | |
echo "NEW_PASSWORD: ${NEW_PASSWORD}" | |
# Alternate using pwgen | |
NEW_PASSWORD=$(pwgen -A 16 1) | |
echo "NEW_PASSWORD: ${NEW_PASSWORD}" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment