Home > bash > bash – Generating Random Passwords

bash – Generating Random Passwords

Problem:
Someone asked me to write them a script to generate random passwords based on words. They normally have to do this periodically for more than 10 accounts. The passwords are just random words separated by spaces.

Solution:
I used pwgen and /usr/share/dict/words

#! /bin/bash
# Password Generator - password_generator.sh
# Create simple word based passwords
# Usage - bash password_generator.sh [number of words]

no_of_words="$1"

# Get the first letter of each word
word=$(pwgen 30 | head)

echo $word | sed 's|\([a-z]\)|\1\n|g' | sort -u |\
sed '/^$/d' | sort -R | head -n ${no_of_words:=5} |\
while read letter
do
    #echo $letter
    # Pick 7 letter words from dictionary
    egrep  "^${letter}[a-z]{6}$" /usr/share/dict/words | sort -R | head -n 1
done > secret_message

sed ':mylbl;N;s/\n/ /;t mylbl' secret_message


Sample output

$ bash password_generator.sh 4
suckles veering hallway entails
$ 
$ bash password_generator.sh 4
humaner aptness ethnics privacy
$ 
$ bash password_generator.sh 4
staffer incised adjured orchids
$ 
$ bash password_generator.sh 4
abjured parsley insists boggled
$ 
$ bash password_generator.sh 2
exiling grouper
$ 
$ bash password_generator.sh 2
emetics impress
$ 
$ bash password_generator.sh 6
extorts prefect abstain legible upgrade tensest
$ 
$ bash password_generator.sh
yeshiva infuses gabbing aphelia kissing
$ 
$ bash password_generator.sh
prudery chapels alarmed hansoms skaters
$ 
$ bash password_generator.sh 8
horizon outgrow knocker zippers approve engrave cremate munched
$ 

Source:
Got some tips from here
http://www.linuxjournal.com/article/8919

Advertisements
Categories: bash
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s