Random Password Generator Development

LinuxLinuxBeginner
Practice Now

Introduction

In this project, you will learn how to create a random password generator script that meets specific requirements. The password generator will create a 12-character password that includes at least one digit, one uppercase letter, one lowercase letter, and one special character from the set ><+-{}:.&;.

👀 Preview

$ cd /home/labex/project
$ sh genpass.sh
## Example
2Dsxw9+xS:27

🎯 Tasks

In this project, you will learn:

  • How to set up the project environment and create the necessary script file
  • How to implement the logic to generate a random password that meets the specified requirements
  • How to test the password generator script to ensure it works as expected

🏆 Achievements

After completing this project, you will be able to:

  • Understand the process of creating a random password generator script
  • Implement a password generation algorithm that meets specific requirements
  • Test and validate the generated passwords to ensure they meet the desired criteria

Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/BasicSystemCommandsGroup(["`Basic System Commands`"]) linux(("`Linux`")) -.-> linux/InputandOutputRedirectionGroup(["`Input and Output Redirection`"]) linux(("`Linux`")) -.-> linux/TextProcessingGroup(["`Text Processing`"]) linux(("`Linux`")) -.-> linux/FileandDirectoryManagementGroup(["`File and Directory Management`"]) shell(("`Shell`")) -.-> shell/BasicSyntaxandStructureGroup(["`Basic Syntax and Structure`"]) shell(("`Shell`")) -.-> shell/VariableHandlingGroup(["`Variable Handling`"]) shell(("`Shell`")) -.-> shell/ControlFlowGroup(["`Control Flow`"]) shell(("`Shell`")) -.-> shell/FunctionsandScopeGroup(["`Functions and Scope`"]) shell(("`Shell`")) -.-> shell/AdvancedScriptingConceptsGroup(["`Advanced Scripting Concepts`"]) linux/BasicSystemCommandsGroup -.-> linux/echo("`Text Display`") linux/InputandOutputRedirectionGroup -.-> linux/pipeline("`Data Piping`") linux/InputandOutputRedirectionGroup -.-> linux/redirect("`I/O Redirecting`") linux/TextProcessingGroup -.-> linux/tr("`Character Translating`") linux/FileandDirectoryManagementGroup -.-> linux/cd("`Directory Changing`") shell/BasicSyntaxandStructureGroup -.-> shell/shebang("`Shebang`") shell/BasicSyntaxandStructureGroup -.-> shell/comments("`Comments`") shell/BasicSyntaxandStructureGroup -.-> shell/quoting("`Quoting Mechanisms`") shell/VariableHandlingGroup -.-> shell/variables_decl("`Variable Declaration`") shell/VariableHandlingGroup -.-> shell/variables_usage("`Variable Usage`") shell/VariableHandlingGroup -.-> shell/str_manipulation("`String Manipulation`") shell/VariableHandlingGroup -.-> shell/param_expansion("`Parameter Expansion`") shell/ControlFlowGroup -.-> shell/for_loops("`For Loops`") shell/FunctionsandScopeGroup -.-> shell/scope_vars("`Scope of Variables`") shell/AdvancedScriptingConceptsGroup -.-> shell/arith_expansion("`Arithmetic Expansion`") shell/AdvancedScriptingConceptsGroup -.-> shell/cmd_substitution("`Command Substitution`") shell/AdvancedScriptingConceptsGroup -.-> shell/subshells("`Subshells and Command Groups`") shell/AdvancedScriptingConceptsGroup -.-> shell/adv_redirection("`Advanced Redirection`") subgraph Lab Skills linux/echo -.-> lab-301485{{"`Random Password Generator Development`"}} linux/pipeline -.-> lab-301485{{"`Random Password Generator Development`"}} linux/redirect -.-> lab-301485{{"`Random Password Generator Development`"}} linux/tr -.-> lab-301485{{"`Random Password Generator Development`"}} linux/cd -.-> lab-301485{{"`Random Password Generator Development`"}} shell/shebang -.-> lab-301485{{"`Random Password Generator Development`"}} shell/comments -.-> lab-301485{{"`Random Password Generator Development`"}} shell/quoting -.-> lab-301485{{"`Random Password Generator Development`"}} shell/variables_decl -.-> lab-301485{{"`Random Password Generator Development`"}} shell/variables_usage -.-> lab-301485{{"`Random Password Generator Development`"}} shell/str_manipulation -.-> lab-301485{{"`Random Password Generator Development`"}} shell/param_expansion -.-> lab-301485{{"`Random Password Generator Development`"}} shell/for_loops -.-> lab-301485{{"`Random Password Generator Development`"}} shell/scope_vars -.-> lab-301485{{"`Random Password Generator Development`"}} shell/arith_expansion -.-> lab-301485{{"`Random Password Generator Development`"}} shell/cmd_substitution -.-> lab-301485{{"`Random Password Generator Development`"}} shell/subshells -.-> lab-301485{{"`Random Password Generator Development`"}} shell/adv_redirection -.-> lab-301485{{"`Random Password Generator Development`"}} end

Set Up the Project Environment

In this step, you will set up the project environment and create the necessary file for the password generator script.

  1. Open the terminal and navigate to the /home/labex/project directory.
cd /home/labex/project
  1. Create a new file named genpass.sh using the touch command.
touch genpass.sh
  1. Open the genpass.sh file in a text editor and add the following shebang line at the 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.

Implement the Password Generation Logic

In this step, you will implement the logic to generate a random password that meets the specified requirements.

  1. In the genpass.sh file, 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 generates a random password that meets the specified requirements.

  1. Add the following code to the end of the genpass.sh file to call the generate_password function and output the generated password:
## Generate password and output
generate_password

Test the Password Generator

In this step, you will test the password generator script to ensure it works as expected.

  1. Save the genpass.sh file.
  2. In the terminal, run the script to generate a password:
sh genpass.sh

The script should output a random password that meets the requirements:

## Example
2Dsxw9+xS:27
  1. Run the script a few more times to ensure it generates different passwords each time.

Congratulations! You have successfully implemented a random password generator that meets the specified requirements.

Summary

Congratulations! You have completed this project. You can practice more labs in LabEx to improve your skills.

Other Linux Tutorials you may like