-
Notifications
You must be signed in to change notification settings - Fork 1
/
pwgen
executable file
·64 lines (58 loc) · 1.21 KB
/
pwgen
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
#via: http://thdonline.wordpress.com/2009/03/14/shell-script-to-generate-random-password/
#with a slight modification for my preferences
CHAR_ARRAY=( {a..z} {A..Z} {0..9} \! \@ \# \% \^ \& \* \( \) \[ \] \{ \} \: \; \, \. \< \>)
get_words() {
a=`cat /usr/share/dict/words | perl -MList::Util=shuffle -wne "chomp(@a=shuffle(<>));print join('-',splice @a,0,$1)"`
echo $a
}
while getopts "wndhc?" opt; do
case $opt in
w)
words=true
;;
n)
CHAR_ARRAY=( {a..z} {A..Z} {0..9} )
;;
d)
CHAR_ARRAY=( {0..9} )
;;
h)
CHAR_ARRAY=( {0..9} {a..f} )
;;
c)
CHAR_ARRAY=( {a..z} {A..Z} )
;;
?)
echo "Usage: $0 [options] [length]
-w <length> number of words as a phrase
-n 'nice' output, Alphanumberic only
-d digits 0-9
-h hex 0-9 and a-f
-c characters only, A-Z and a-z"
exit
;;
esac
done
shift $(($OPTIND - 1))
MAXSIZE=$1
if [ "$MAXSIZE" != "" -a "$MAXSIZE" == "${MAXSIZE//[^0-9]/}" ]
then
#I don't like the extra output
MAXSIZE=$MAXSIZE
else
MAXSIZE=16
fi
if [ $words ]
then
pw=$(get_words $MAXSIZE)
echo -n "$pw"
else
MODNUM=${#CHAR_ARRAY[*]}
for ((i=0; i < $MAXSIZE; i++ ))
do
NUM=$(( RANDOM % $MODNUM ))
echo -n "${CHAR_ARRAY[$NUM]}"
done
fi
echo ""