|
1 | 1 | # Password Generator Bash Script
|
| 2 | + |
2 | 3 | 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.
|
3 | 4 |
|
4 | 5 | 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.
|
5 | 6 |
|
6 | 7 | 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.
|
7 | 8 |
|
| 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 | + |
8 | 15 | ## Script summary
|
9 | 16 |
|
10 | 17 | Let me first do a quick summary of what our script is going to do.:
|
11 | 18 |
|
12 | 19 | 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 |
15 | 21 |
|
16 | 22 | ## Prerequisites
|
17 | 23 |
|
@@ -65,111 +71,56 @@ First we begin the script with the shebang. We use it to tell the operating syst
|
65 | 71 | ```
|
66 | 72 | #!/bin/bash
|
67 | 73 | ```
|
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 |
| -``` |
82 | 74 | 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:
|
83 | 75 |
|
84 | 76 | ```
|
85 | 77 | # Ask user for password length
|
86 | 78 | clear
|
87 | 79 | 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 |
89 | 81 | printf "\n"
|
90 | 82 | ```
|
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. |
110 | 84 | ```
|
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 |
116 | 87 |
|
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" |
119 | 93 | ```
|
120 | 94 |
|
121 | 95 | ## The full script:
|
122 | 96 | ```
|
123 | 97 | #!/bin/bash
|
124 |
| -#======================================= |
| 98 | +#======================================= |
125 | 99 | # 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 | +#======================================= |
134 | 101 |
|
135 |
| -# Ask user for password length |
| 102 | +# Ask user for the string length |
136 | 103 | clear
|
137 | 104 | printf "\n"
|
138 | 105 | read -p "How many characters you would like the password to have? " pass_lenght
|
139 | 106 | printf "\n"
|
140 | 107 |
|
141 | 108 | # 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 |
144 | 110 |
|
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 |
147 | 112 |
|
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" |
161 | 116 | ```
|
162 | 117 |
|
163 | 118 | ## 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. |
165 | 120 |
|
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.** |
169 | 122 |
|
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. |
173 | 124 |
|
174 | 125 | ## Contributed by
|
175 | 126 | [Alex Georgiev](https://twitter.com/alexgeorgiev17)
|
0 commit comments