-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpwdgen.sh
More file actions
executable file
·49 lines (32 loc) · 1.18 KB
/
pwdgen.sh
File metadata and controls
executable file
·49 lines (32 loc) · 1.18 KB
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
#!/bin/bash
#to generate a random string of a given length
generate_random_string() {
local length=$1
tr -dc '[:graph;]' < /dev/urandom | head -c length
}
#to generate a pwd with specified criteria
generate_pwd() {
local length=$1
local num_letters=$2
local num_numbers=$3
local num_symbols=$4
local pwd=""
#generate random string of each category
local letters=$(generate_random_string $num_letters | tr -dc 'a-zA-z')
local numbers=$(generate_random_string $num_numbers | tr -dc '0-9')
local symbols=$(generate_random_string $num_symbols | tr -dc '!@#$%^&*()_-+=<>?')
#to insert the random strings into the blank pwd
pwd = "${letters}${numbers}${symbols}"
#to shuffle the pwd to make the order random
pwd=$(echo '$pwd" | fold -wi | shuf | tr -d '\n\')
#if pwd length is less that the desired one, fill in the remaning characters
while [ ${#password} -lt $length ]; do
pwd="${pwd}$(generate_random_string 1)"
done
# Trim the password to the desired length
pwd=${pwd:0:$length}
echo "$pwd"
}
# Generate a password of length 9 with 3 letters, 3 numbers, and 3 symbols
generated_pwd=$(generate_pwd 9 3 3 3)
echo "Generated Password: $generated_pwd"