-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpassgen.sh
executable file
·192 lines (180 loc) · 5.27 KB
/
passgen.sh
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#!/bin/bash
#Author: Joshua Kroger
#https://github.com/ProbieK/
#Set Default Values
silent='false'
GUI='false'
char='graph'
engine='perl'
num_min='16'
num_max='24'
#Detect OS for later special uses
#(coming soon: native bash on WSL support)
if [ "$(uname)" == "Darwin" ]; then
HOSTOS='MACOS'
elif [ "$(expr substr $(uname -s) 1 5)" == "Linux" ]; then
HOSTOS='LINUX'
elif [ "$(expr substr $(uname -s) 1 10)" == "MINGW32_NT" ]; then
HOSTOS='WIN32'
elif [ "$(expr substr $(uname -s) 1 10)" == "MINGW64_NT" ]; then
HOSTOS='WIN64'
fi
#Detect if script is running in a GUI or not
if [ "$DISPLAY" ]; then
GUI='true'
fi
#Do help stuff
function show_help {
echo ""
echo "Usage:"
echo "$ ./passgen.sh [option] [value]"
echo "You may call the script without flags and it will generate a password"
echo "between 16 and 24 characters long in the \"graph\" character set."
echo ""
echo "Example:"
echo "$ ./passgen.sh"
echo "or"
echo "$ ./passgen.sh -n 16"
echo "or"
echo "$ ./passgen.sh -n 16 -c graph"
echo ""
echo "Sample Output:"
echo "Password Length: 16"
echo "Password Charset: graph"
echo "@hA}x8GQtNJ6u_)0"
echo ""
echo "Command Line Options:"
echo "-h or --help Shows this help and exits"
echo "-n or --number Modifies the length of the password"
echo "-c or --char Modifies the character set"
echo "-s or --silent Silences all optional terminal output"
echo "-m or --min Sets the minimum length of the password (must be used with --max or unexpected lengths will result)"
echo "-x or --max Sets the maximum length of the password"
echo "-g or --grep Uses grep as the engine to select random characters"
echo "-p or --perl Uses perl as the engine to select random characters"
echo "(perl is the defualt engine)"
echo ""
echo "Charactor Set Options:"
echo 'digit Digits: 0-9'
echo 'lower Lower-case letters: a-z'
echo 'upper Upper-case letters: A-Z'
echo 'alpha Alphabetic characters: lower and upper: A-Za-z'
echo 'alnum Alphanumeric characters: alpha and digit: 0-9A-Za-z'
echo 'punct Punctuation characters'
echo 'graph Graphical characters: alnum and punct'
echo 'print Printable characters: alnum, punct, and space'
echo ''
exit 0
}
function set_min_max {
num=$(awk -v min=$num_min -v max=$num_max 'BEGIN{srand(); print int(min+rand()*(max-min+1))}')
}
#Parse Command Line Options
ARGS=( "$@" )
for i in "${!ARGS[@]}"; do
case "${ARGS[i]}" in
'')
continue
;;
-h|--help)
show_help
;;
-g|--grep)
num="${ARGS[i+1]}"
engine='grep'
unset 'ARGS[i+1]'
;;
-p|--perl)
num="${ARGS[i+1]}"
engine='perl'
unset 'ARGS[i+1]'
;;
-n|--number)
num="${ARGS[i+1]}"
num_flag='1'
unset 'ARGS[i+1]'
;;
-c|--char)
char="${ARGS[i+1]}"
unset 'ARGS[i+1]'
;;
-s|--silent)
silent='true'
;;
-m|--min)
num_min="${ARGS[i+1]}"
num_min_flag='1'
unset 'ARGS[i+1]'
;;
-x|--max)
num_max="${ARGS[i+1]}"
num_max_flag='1'
unset 'ARGS[i+1]'
;;
--)
unset 'ARGS[i]'
break
;;
*)
continue
;;
esac
unset 'ARGS[i]'
done
#Error Checking
charsets=( digit lower upper alpha alnum punct graph print )
if [[ ! " ${charsets[@]} " =~ " $char " ]]; then
echo "Charset not recognized! Aborting!"
exit 1
fi
if [ "$num_min_flag" == '1' ]; then
if [ ! "$num_max_flag" == '1' ]; then
echo "When you set a minimum number, a maximum must also be used."
echo "Aborting!"
exit 1
fi
fi
if [ "$num_max_flag" == '1' ]; then
if [ ! "$num_min_flag" == '1' ]; then
echo "When you set a maximum number, a minimum must also be used."
echo "Aborting!"
exit 1
fi
fi
#Set min and max length values if it wasn't explicitly defined
if [ ! "$num_flag" == '1' ]; then
set_min_max
fi
#Password Stats
if [ $silent == "false" ]; then
echo "Password Length: $num"
echo "Password Charset: $char"
fi
#The meat and potatos of this password generator
if [ "$engine" == "grep" ]; then
var=$(strings - /dev/urandom | grep -o "[[:"$char":]]" | head -n "$num" | tr -d '\n')
elif [ "$engine" == "perl" ]; then
var=$(strings - /dev/urandom | perl -p -e "s/[^[:"$char":]]//g" | head -c "$num" | tr -d '\n' )
else
echo "No engine selected! How did you manage that?"
echo "Exiting"
exit 0
fi
echo "Engine: $engine"
printf '\033[0;36m%s\033[0m\n' "$var"
#Copy password to clipboard on MacOS
if [ $HOSTOS == "MACOS" ]; then
printf '%s' "$var" | pbcopy
fi
#Copy password to clipboard on Linux hosts with xclip
if [ $HOSTOS == "LINUX" ]; then
if command -v xclip > /dev/null; then
if [ $GUI == "true" ]; then
printf '%s' "$var" | xclip -selection c
fi
else
if [ $silent == "false" ]; then
echo "If you install xclip, I can copy the password right to your clipboard!"
fi
fi
fi