Subjectively

dd if=/dev/random | kirk > blog

Subjectively header image 2

Simple bash password generator

November 24th, 2009 · No Comments · Linux, OS X

This is just a quick way to create arbitrary passwords that may contain letters, numbers, and symbols. It takes an optional length parameter.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/bin/bash
#===============================================================================
#
#          FILE:  password
# 
#         USAGE:  ./password [ length ]
# 
#   DESCRIPTION:  Generate a secure password using /dev/urandom
# 
#       OPTIONS:  ---
#  REQUIREMENTS:  ---
#          BUGS:  ---
#         NOTES:  ---
#       VERSION:  1.0
#       CREATED:  10/13/2009 11:10:39 CDT
#      REVISION:  ---
#===============================================================================
 
function usage () {
cat <<EOS
    Usage: `basename $0` [ length ] [ --usage | --help ]
    Default password length is 12
EOS
 
}
 
while [ ! -z $1 ]
do
  case $1 in 
    '--usage')
      usage
      exit
      ;;
    '--help')
     usage
     exit
      ;;
    *)
    length=$1
    [[ $length -lt 1 ]] && {
      usage
      exit
    } 
    ;;
  esac 
  shift
done
 
[[ -z $length ]] && length=12
[[ $length -lt 4 ]] && {
  echo password is too weak to produce
  exit 1
}
[[ $length -lt 8 ]] && {
  echo password is weak
}
declare password
while [[ ${#password} -lt $length ]]
do
  char=`dd if=/dev/urandom bs=1 count=1 2> /dev/null | mimencode`
  char=${char:0:1}
  password="$password$char"
done
echo $password

Tags:

No Comments so far ↓

There are no comments yet...Kick things off by filling out the form below.

Leave a Comment

You must log in to post a comment.