Skip to content

Commit fa5a94a

Browse files
authored
Update the password generator chapter (bobbyiliev#21)
1 parent cae8d98 commit fa5a94a

File tree

1 file changed

+28
-77
lines changed

1 file changed

+28
-77
lines changed
Lines changed: 28 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,23 @@
11
# Password Generator Bash Script
2+
23
It's not uncommon situation where you will need to generate a random password that you can use for any software installation or when you sign-up to any website.
34

45
There are a lot of options in order to achieve this. You can use a password manager/vault where you often have the option to randomly generate a password or to use a website that can generate the password on your behalf.
56

67
You can also use Bash in your terminal (command-line) to generate a password that you can quickly use. There are a lot of ways to achieve that and I will make sure to cover few of them and will leave up to you to choose which option is most suitable with your needs.
78

9+
## :warning: Security
10+
11+
**This script is intended to practice your bash scripting skills. You can have fun while doing simple projects with BASH, but security is not a joke, so please make sure you do not save your passwords in plain text in a local file or write them down by hand on a piece of paper.**
12+
13+
**I will highly recommend everyone to use secure and trusted providers to generate and save the passwords.**
14+
815
## Script summary
916

1017
Let me first do a quick summary of what our script is going to do.:
1118

1219
1. We will have to option to choose the password characters length when the script is executed.
13-
2. We will ask the user if they want to save the password in the log file and
14-
3. If the user wants to save the password we will ask for some details about the password before the password is saved in the log file.
20+
2. The script will then generate 5 random passwords with the length that was specified in step 1
1521

1622
## Prerequisites
1723

@@ -65,111 +71,56 @@ First we begin the script with the shebang. We use it to tell the operating syst
6571
```
6672
#!/bin/bash
6773
```
68-
Then we can define the log file path as variable which we're going to use later in the script:
69-
70-
```
71-
# Log file location
72-
log_file=~/pass_log.txt
73-
```
74-
We can also run a check if the log file exists and if not to create it in the user's home directory:
75-
76-
```
77-
# Check if the log file is present and if not, create it
78-
if [[ !log_file ]]; then
79-
touch ~/pass_log.txt
80-
fi
81-
```
8274
We can then continue and ask the user for some input. In this case we would like to know how many characters the password needs to be:
8375

8476
```
8577
# Ask user for password length
8678
clear
8779
printf "\n"
88-
read -p "How many characters you would like the password to have? " pass_lenght
80+
read -p "How many characters you would like the password to have? " pass_length
8981
printf "\n"
9082
```
91-
Generate the password and then print it so the user can copy it.
92-
```
93-
pass_output=$(tr -cd '[:alnum:]' < /dev/urandom | fold -w${pass_lenght} | head -n 1)
94-
# Print the password
95-
printf "$pass_output\n"
96-
```
97-
Ask the user if they want to save the password in the password log file:
98-
```
99-
read -p "Would you like to save the password in the pass-log? Answer with: y or n: " log_pass
100-
```
101-
Check if the user wants to save the password in the log. Log the file if the answer is yes else exit the script.
102-
```
103-
if [[ ${log_pass} == n ]]; then
104-
printf "Goodbye, ${USER}!\n"
105-
exit 0
106-
else
107-
read -p "What is this password for? " source && echo "${source} - ${pass_output}" >> ${log_file}
108-
printf "Password is saved in the pass-log. Goodbye, ${USER}!\n"
109-
fi
83+
Generate the passwords and then print it so the user can use it.
11084
```
111-
If we want to check the log file and see if the password was saved, all we need to do is ```cat ```the file:
112-
```
113-
cat ~/pass_log.txt
114-
```
115-
An example output will be:
85+
# This is where the magic happens!
86+
# Generate a list of 10 strings and cut it to the desired value provided from the user
11687
117-
```
118-
DevDojo - VyJ3Kn7ltN
88+
for i in {1..10}; do (tr -cd '[:alnum:]' < /dev/urandom | fold -w${pass_lenght} | head -n 1); done
89+
90+
# Print the strings
91+
printf "$pass_output\n"
92+
printf "Goodbye, ${USER}\n"
11993
```
12094

12195
## The full script:
12296
```
12397
#!/bin/bash
124-
#=======================================
98+
#=======================================
12599
# Password generator with login option
126-
#=======================================
127-
# Log file location
128-
log_file=~/pass_log.txt
129-
130-
# Check if the log file is present and if not, create it
131-
if [[ !log_file ]]; then
132-
touch ~/pass_log.txt
133-
fi
100+
#=======================================
134101
135-
# Ask user for password length
102+
# Ask user for the string length
136103
clear
137104
printf "\n"
138105
read -p "How many characters you would like the password to have? " pass_lenght
139106
printf "\n"
140107
141108
# This is where the magic happens!
142-
# Generate the password and cut it to the desired value provided from the user
143-
pass_output=$(tr -cd '[:alnum:]' < /dev/urandom | fold -w${pass_lenght} | head -n 1)
109+
# Generate a list of 10 strings and cut it to the desired value provided from the user
144110
145-
# Print the password so the user can copy it
146-
printf "$pass_output\n"
111+
for i in {1..10}; do (tr -cd '[:alnum:]' < /dev/urandom | fold -w${pass_lenght} | head -n 1); done
147112
148-
#Ask user if the password needs to be saved in the log file
149-
printf "\n"
150-
read -p "Would you like to save the password in the pass-log? Answer with: y or n: " log_pass
151-
152-
#Ask user if the password needs to be saved in the log file
153-
printf "\n"
154-
if [[ ${log_pass} == n ]]; then
155-
printf "Goodbye, ${USER}!\n"
156-
exit 0
157-
else
158-
read -p "What is this password for? " source && echo "${source} - ${pass_output}" >> ${log_file}
159-
printf "Password is saved in the pass-log. Goodbye, ${USER}!\n"
160-
fi
113+
# Print the strings
114+
printf "$pass_output\n"
115+
printf "Goodbye, ${USER}\n"
161116
```
162117

163118
## Conclusion
164-
This is pretty much how you can use simple bash script to generate random passwords that you can just use one time or save it in a log file.
119+
This is pretty much how you can use simple bash script to generate random passwords.
165120

166-
While the script is working fine, it expects that the user will provide the requested input. In order to prevent any issues you would need to do some more advance checks on the user input in order to make sure the script will continue to work fine even if the provided input does not match our needs.
167-
168-
I will make sure to cover the more advance checks or the user input in of my next blog posts.
121+
:warning: **As already mentioned, please make sure to use strong passwords in order to make sure your account is protected. Also whenever is possible use 2 factor authentication as this will provide additional layer of security for your account.**
169122

170-
I hope you find this useful and would like to hear what do you think about logging passwords using local scripts and if you have custom build scripts to generate and save passworss.
171-
172-
Let me know if you face any issues with the script or if you have any recommendations as well.
123+
While the script is working fine, it expects that the user will provide the requested input. In order to prevent any issues you would need to do some more advance checks on the user input in order to make sure the script will continue to work fine even if the provided input does not match our needs.
173124

174125
## Contributed by
175126
[Alex Georgiev](https://twitter.com/alexgeorgiev17)

0 commit comments

Comments
 (0)