From c2b123c2cedca2f6a07ec9f3b2708fd8addcde6b Mon Sep 17 00:00:00 2001 From: ApurvDevops Date: Wed, 2 Aug 2023 20:47:56 +0200 Subject: [PATCH 1/2] Day 3 challenge --- Challenges/Day_3/user_mamagement.sh | 127 ++++++++++++++++++++++++++++ 1 file changed, 127 insertions(+) create mode 100755 Challenges/Day_3/user_mamagement.sh diff --git a/Challenges/Day_3/user_mamagement.sh b/Challenges/Day_3/user_mamagement.sh new file mode 100755 index 0000000..358817e --- /dev/null +++ b/Challenges/Day_3/user_mamagement.sh @@ -0,0 +1,127 @@ +#Author : Apurv Samadder +#Date : Aug 2 2023 +#Version: v1 +#Day 3 Bash Scripting Challenge - User Account Management. + + + +#!/bin/bash + +#Create a user +add_user() +{ +echo "enter username " +read username + +#echo "Enter password" +#read pass + +if grep "${username}" /etc/passwd >/dev/null 2>&1; then + + echo "User alread present" + +else + echo "enter password" + read pass + useradd -m -p $pass $username && echo "Successfully added user" + +fi +} + +#delete user + +delete_user() +{ + +if [[ $(id -u) -ne 0 ]]; then + echo "This script must be run as root or with sudo privileges." + exit 1 +fi + +# Prompt the user for the username to delete +read -p "Enter the username you want to delete: " username + +# Check if the username exists +if id "$username" &>/dev/null; then + # Delete the user and their home directory + userdel -r "$username" + echo "User '$username' and home directory deleted." +else + echo "User '$username' does not exist." +fi +} + + +reset_passwd() +{ + +if [[ $(id -u) -ne 0 ]]; then + echo "This script must be run as root or with sudo privileges." + exit 1 +fi + +# Prompt the user for the username to changes password +read -p "Enter the username you want to delete: " username + +# Check if the username exists +if id "$username" &>/dev/null; then + # Changes the password if user exist + passwd "$username" + echo "User '$username' Password changed." +else + echo "User '$username' does not exist." +fi +} + +list_user() +{ + # Read uid column /etc/passwd + for userid in `awk -F: '{print $3}' /etc/passwd` + do + if ("$userid" >= 1000); then + echo "Valid User" :`cat /etc/passwd | grep $userid | awk -F: '{print $1,$3}'` + fi + done +} + + +user_mgmt() +{ +echo "options" +echo "-c,--create create a new user accoutn" +echo "-d, --delete Delete and existing user account" +echo "-r, --reset Reset a password for existing user" +echo "-l, --list List all the user in system" +echo "-h, --help Display this help and exit" + +read option + +case $option in + -c|--create) + add_user + ;; + -d|--delete) + echo "inside delete" + delete_user + ;; + -r|--reset) + echo "inside reset" + reset_passwd + ;; + -l|--list) + list_user + echo "inside list" + ;; + -h|--help) + echo "inside help" + ;; + *) + echo "please enter valid option" + user_mgmt + ;; +esac +} + +user_mgmt + + From c8f34ad62b767baa82a13ed98072dea836a95218 Mon Sep 17 00:00:00 2001 From: ApurvDevops Date: Thu, 3 Aug 2023 21:55:33 +0200 Subject: [PATCH 2/2] Day 3 task adding in seperate branch --- Challenges/Day_3/user_management.sh | 94 ----------------------------- 1 file changed, 94 deletions(-) delete mode 100644 Challenges/Day_3/user_management.sh diff --git a/Challenges/Day_3/user_management.sh b/Challenges/Day_3/user_management.sh deleted file mode 100644 index 6f77f5d..0000000 --- a/Challenges/Day_3/user_management.sh +++ /dev/null @@ -1,94 +0,0 @@ -#!/bin/bash - -# Function to display usage information and available options -function display_usage { - echo "Usage: $0 [OPTIONS]" - echo "Options:" - echo " -c, --create Create a new user account." - echo " -d, --delete Delete an existing user account." - echo " -r, --reset Reset password for an existing user account." - echo " -l, --list List all user accounts on the system." - echo " -h, --help Display this help and exit." -} - -# Function to create a new user account -function create_user { - read -p "Enter the new username: " username - - # Check if the username already exists - if id "$username" &>/dev/null; then - echo "Error: The username '$username' already exists. Please choose a different username." - else - # Prompt for password (Note: You might want to use 'read -s' to hide the password input) - read -p "Enter the password for $username: " password - - # Create the user account - useradd -m -p "$password" "$username" - echo "User account '$username' created successfully." - fi -} - -# Function to delete an existing user account -function delete_user { - read -p "Enter the username to delete: " username - - # Check if the username exists - if id "$username" &>/dev/null; then - userdel -r "$username" # -r flag removes the user's home directory - echo "User account '$username' deleted successfully." - else - echo "Error: The username '$username' does not exist. Please enter a valid username." - fi -} - -# Function to reset the password for an existing user account -function reset_password { - read -p "Enter the username to reset password: " username - - # Check if the username exists - if id "$username" &>/dev/null; then - # Prompt for password (Note: You might want to use 'read -s' to hide the password input) - read -p "Enter the new password for $username: " password - - # Set the new password - echo "$username:$password" | chpasswd - echo "Password for user '$username' reset successfully." - else - echo "Error: The username '$username' does not exist. Please enter a valid username." - fi -} - -# Function to list all user accounts on the system -function list_users { - echo "User accounts on the system:" - cat /etc/passwd | awk -F: '{ print "- " $1 " (UID: " $3 ")" }' -} - -# Check if no arguments are provided or if the -h or --help option is given -if [ $# -eq 0 ] || [ "$1" = "-h" ] || [ "$1" = "--help" ]; then - display_usage - exit 0 -fi - -# Command-line argument parsing -while [ $# -gt 0 ]; do - case "$1" in - -c|--create) - create_user - ;; - -d|--delete) - delete_user - ;; - -r|--reset) - reset_password - ;; - -l|--list) - list_users - ;; - *) - echo "Error: Invalid option '$1'. Use '--help' to see available options." - exit 1 - ;; - esac - shift -done