-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen_pass
executable file
·47 lines (39 loc) · 1.68 KB
/
gen_pass
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
#!/bin/bash
generate_password() {
local password_length=$1
local charset_upper="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
local charset_lower="abcdefghijklmnopqrstuvwxyz"
local charset_digits="0123456789"
local charset_special="!@#$%^&*()_+-=<>?{}[]"
# Ensure at least one character of each type in the password
password=""
password="${password}${charset_upper:$(($RANDOM % ${#charset_upper})):1}"
password="${password}${charset_lower:$(($RANDOM % ${#charset_lower})):1}"
password="${password}${charset_digits:$(($RANDOM % ${#charset_digits})):1}"
password="${password}${charset_special:$(($RANDOM % ${#charset_special})):1}"
# Fill the rest of the password with random characters from all sets
remaining_length=$((password_length - 4))
charset_all="$charset_upper$charset_lower$charset_digits$charset_special"
for i in $(seq 1 "$remaining_length"); do
# Get a random character from the combined charset
random_char=$(echo "$charset_all" | fold -w1 | shuf | head -n1)
password="${password}${random_char}"
done
# Shuffle the password to make it less predictable
shuffled_password=$(echo "$password" | fold -w1 | shuf | tr -d '\n')
echo "$shuffled_password"
}
# Check if the script is called with the correct number of arguments
if [ $# -ne 1 ]; then
echo "Usage: $0 <password_length>"
exit 1
fi
# Check if the argument is a positive integer
if ! [[ $1 =~ ^[1-9][0-9]*$ ]]; then
echo "Error: The password length must be a positive integer."
exit 1
fi
password_length=$1
strong_password=$(generate_password "$password_length")
echo "Generated strong password with length $password_length:"
echo "$strong_password"