Skip to content

Instantly share code, notes, and snippets.

@ovidiucp
Last active May 21, 2018 07:27
Show Gist options
  • Save ovidiucp/deacc9010445ccfa8307f81b6348b3a0 to your computer and use it in GitHub Desktop.
Save ovidiucp/deacc9010445ccfa8307f81b6348b3a0 to your computer and use it in GitHub Desktop.
Generate random password of specified length (default 12 characters)
# Put this file in your ~/.bashrc. To invoke type in the command line:
#
# genpasswd
#
# The default password length is 12 characters. If need it longer:
#
# genpasswd 24
function genpasswd() {
local numchars=$1
if [ -z "$numchars" ]; then
numchars=12
fi
numchars=$(expr $numchars + 1 )
numbytes=$(expr $numchars + 20 )
openssl rand -base64 $numbytes | \
xargs echo | \
sed -e 's%/%%g' -e 's%=%%g' -e 's%\+%%g' -e 's% %%g' | \
tail -c -$numchars
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment