Introduction
In this project, you'll learn how to create a random password generator script that meets specific security requirements. The script will generate a 12-character password that includes at least one digit, one uppercase letter, one lowercase letter, and one special character from the set ><+-{}:.&;. This project will provide you with hands-on experience in scripting and working with random data.
👀 Preview
$ cd /home/labex/project
$ sh genpass.sh
## Example
2Dsxw9+xS:27
🎯 Tasks
In this project, you will:
- Set up the project environment and create the necessary script file.
- Implement the logic to generate a random password that meets the specified complexity requirements.
- Test the password generator script to ensure it functions correctly.
🏆 Achievements
Upon completing this project, you will be able to:
- Understand the process of creating a random password generator script.
- Implement a password generation algorithm that satisfies specific character type requirements.
- Test and validate the generated passwords to confirm they meet the desired criteria.
Set Up the Project Environment
In this step, you'll prepare your working environment and create the necessary file for the password generator script.
Open your terminal and navigate to the
/home/labex/projectdirectory using thecdcommand:cd /home/labex/projectCreate a new file named
genpass.shusing thetouchcommand. This will be the file where you write your password generator script:touch genpass.shOpen the
genpass.shfile in a text editor. You can usenano,vim, or any text editor you prefer. Add the following shebang line at the very beginning of the file:#!/bin/zsh ## Random Password Generator ## This script generates a random password that meets the specified requirements.This line tells the system to use the zsh shell to execute the script. The comments following the shebang line provide a brief description of the script.
Implement the Password Generation Logic
In this step, you'll add the core logic to the genpass.sh script to generate random passwords that meet the complexity requirements.
Inside the
genpass.shfile, add the following function to generate the password:## Function to generate a random password generate_password() { local length=12 local password='' ## Special characters local special_chars='><+-{}:.&;' local special_char="${special_chars:$RANDOM%${#special_chars}:1}" password+="$special_char" ## Digits local digits='0123456789' local digit="${digits:$RANDOM%${#digits}:1}" password+="$digit" ## Uppercase letters local upper_case='ABCDEFGHIJKLMNOPQRSTUVWXYZ' local upper="${upper_case:$RANDOM%${#upper_case}:1}" password+="$upper" ## Lowercase letters local lower_case='abcdefghijklmnopqrstuvwxyz' local lower="${lower_case:$RANDOM%${#lower_case}:1}" password+="$lower" ## Remaining characters local remaining_length=$((length - 4)) local characters='><+-{}:.&;0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz' local num_characters=${#characters} for ((i = 0; i < remaining_length; i++)); do local random_char="${characters:$RANDOM%$num_characters:1}" password+="$random_char" done ## Shuffle the order of password characters password=$(echo "$password" | fold -w1 | shuf | tr -d '\n') echo "$password" }This function works by:
- Initializing the password with one special character, one digit, one uppercase letter, and one lowercase letter to meet the basic criteria.
- Calculating the number of remaining characters to reach the desired length of 12.
- Randomly selecting the remaining characters from a pool of digits, letters (both upper and lower case), and special characters.
- Shuffling the password to ensure the specific characters are not always at the start.
Add the following code to the end of the
genpass.shfile. This part of the script calls thegenerate_passwordfunction and prints the generated password to the console:## Generate password and output generate_password
Test the Password Generator
In this step, you'll test the genpass.sh script to ensure it generates passwords that meet all the requirements.
Save the
genpass.shfile in your text editor.In the terminal, make the script executable using the
chmodcommand:chmod +x genpass.shThis command adds execute permissions to the file, allowing it to be run as a program.
Run the script using the following command:
sh genpass.shThe script should output a random 12-character password to the console. The output should contain at least one special character, one digit, one uppercase letter, and one lowercase letter.
## Example 2Dsxw9+xS:27Run the script several more times. Each time, a different password should be generated. This verifies that the randomness in the password generator works correctly.
Congratulations! You have successfully implemented and tested a random password generator that meets the specified complexity requirements.
Summary
Congratulations! You have completed this project and created a random password generator script. You now have a foundation to build on for future scripting projects. You can continue practicing with more labs on LabEx to further enhance your skills.



