cancel
Showing results for 
Search instead for 
Did you mean: 
Chetan_Tiwary_
Community Manager
Community Manager
  • 876 Views

Solve this Shell Scripting Challenge.

Write a Bash shell script that checks if a user exists in your system. If the user doesn't exist, the script should prompt the user to create the user with a strong password. The script should also ensure that the user's home directory is created.

More marks for clarity, reusability and readability!

Labels (2)
17 Replies
  • 264 Views

#!/bin/bash

# Function to check if a user exists
user_exists() {
local username=$1
if id "$username" &>/dev/null; then
return 0 # User exists
else
return 1 # User does not exist
fi
}

# Function to create a strong password using openssl
generate_strong_password() {
# Generate a 16-character password using openssl
local password
password=$(openssl rand -base64 16) # 16-byte base64 password
echo "$password"
}

# Function to create a user
create_user() {
local username=$1
# Check if user already exists
if user_exists "$username"; then
echo "User '$username' already exists on the system."
return
fi

# Ask for confirmation to create the user
echo "User '$username' does not exist."
read -p "Do you want to create this user? (y/n): " create_choice

if [[ "$create_choice" =~ ^[Yy]$ ]]; then
# Generate a strong password
password=$(generate_strong_password)
echo "Generated strong password: $password"

# Create the user with a home directory and set the password
sudo useradd -m -s /bin/bash "$username" # Create user with home dir
echo "$username:$password" | sudo chpasswd # Set the user's password

# Optionally, you can force the user to change password on first login
sudo chage -d 0 "$username"

echo "User '$username' created successfully with a strong password."
echo "Please store this password securely."
else
echo "User creation cancelled."
fi
}

# Main script logic

# Prompt for username
read -p "Enter the username to check: " username

# Create user if necessary
create_user "$username"

  • 249 Views

#!/bin/bash

# Function to check if a user exists
function check_user_exists {
# Check if the username is given as input
if [ -z "$1" ]; then
echo "No username provided."
exit 1
fi

# Checking if the user exists in the system
if id "$1" &>/dev/null; then
echo "User '$1' already exists."
exit 0
else
echo "User '$1' does not exist."
create_user "$1"
fi
}

# Function to create a user
function create_user {
# Prompt for the username
local username="$1"

# Prompt for a strong password
while true; do
read -s -p "Enter password for user '$username': " password
echo
read -s -p "Confirm password: " password2
echo

if [[ "$password" == "$password2" ]]; then
# Check for password strength (at least 8 characters, 1 uppercase, 1 number, and 1 special character)
if [[ ${#password} -ge 8 && "$password" == *[[:upper:]]* && "$password" == *[[:digit:]]* && "$password" == *[![:alnum:]]* ]]; then
# Create the user with the provided password
sudo useradd -m -p "$(openssl passwd -1 $password)" "$username"

if [ $? -eq 0 ]; then
echo "User '$username' created successfully with home directory."
else
echo "Failed to create user '$username'."
fi
break
else
echo "Password is not strong enough. Please try again."
fi
else
echo "Passwords do not match. Please try again."
fi
done
}

# Main Script Execution
# Prompt for a username
read -p "Enter the username to check: " user_input
check_user_exists "$user_input"

ipalagin
Flight Engineer Flight Engineer
Flight Engineer
  • 219 Views

Here is my version:

~~~

#!/bin/bash
PATH=/usr/bin:/usr/sbin

# Ask for the user name
read -p "Enter the user name: " _USERNAME  

# Exit if the user exists
getent passwd ${_USERNAME} >/dev/null && echo -e "\nUser \"${_USERNAME}\" exists\n"

# Prompt to create new user if it doesn't exist
read -p "User \"${_USERNAME}\" doesn't exist, do you want it created? (y/n) " _CREATEYN
if [[ ${_CREATEYN} != 'y' ]]
then
 echo -e "\nNOTHING TO DO: New user won't be created\n"
 exit 0
fi

# Create new user
sudo useradd -m ${_USERNAME}

# Set the password
while true
do

 read -p "Enter a strong password for the new account: " -s _NEWPASSWD
  
 # Analyze the password with cracklib
 _passwdcheck=$(echo ${_NEWPASSWD} | cracklib-check | awk '{print $2}' )

 if [[ ${_passwdcheck} == "OK" ]]
 then
   echo -e "\nOK: The password is strong, proceeding...\n"
   echo "${_USERNAME}:${_NEWPASSWD}" | sudo chpasswd
   break
 else
   echo -e "\n ERROR: You've entered a weak password, please try again!\n"
 fi  
done

echo -e "DONE: ${_USERNAME} was created, and the password was set\n"
exit 0

~~~

 

Numan
Cadet
Cadet
  • 190 Views

# vim user-creation-script.sh


#!/bin/bash

check_user_exists() {
    id "$1" &>/dev/null
}

create_password() {
     local password=$(tr -dc 'A-Za-z0-9!@#$%^&*()_+~' </dev/urandom | head -c 12)
     echo "$password"
}

read -p "Enter the username to check: " username

if check_user_exists "$username"; then
         echo "User '$username' exists in the system."
else
         echo "User '$username' does not exist."

         read -p "Do you want to create this user? (y/n): " response
         if [[ "$response" =~ ^[Yy]$ ]]; then
                password=$(create_password)
                useradd -m "$username"
                echo "$username:$password" | chpasswd
                echo "User '$username' has been created successfully."
                echo "The generated password is: $password"
                echo "Please make sure to note down the password or change it later."
         else
                echo "User creation aborted."
         fi
fi

wq!

# chmod +x user-creation-script.sh
# ./user-createion-script.sh

FelipeHenriquez
Mission Specialist
Mission Specialist
  • 152 Views

 

#!/bin/bash

# checking  if a user exists
user_exists() {
id "$1" &>/dev/null
}

# checking strong passwords
is_strong_password() {
local password="$1"
[[ ${#password} -ge 8 && "$password" =~ [A-Z] && "$password" =~ [a-z] && "$password" =~ [0-9] && "$password" =~ [^a-zA-Z0-9] ]]
}

# enter  username
read -p "give username : " username

if user_exists "$username"; then
echo "User '$username' already exists."
else
echo "User '$username' does not exist."

# create useer
read -p "create this user? (yes/no): " response
if [[ "$response" =~ ^[Yy][Ee][Ss]$ || "$response" =~ ^[Yy]$ ]]; then
# Loop for strong password 
while true; do
read -s -p "give  a strong pass for the  new user: " password
echo
if is_strong_password "$password"; then
break
else
echo "Password must be at least 8 characters long and include uppercase, lowercase, a number, and a special character."
fi
done

# Create the user with home directory
sudo useradd -m "$username"
echo "$username:$password" | sudo chpasswd
echo "a new User '$username' was created  "
else
echo "cancelled."
fi
fi

  • 85 Views

#!/bin/bash

# Check if a user exists
if id "$1" &>/dev/null; then
echo "User '$1' already exists."
else
# If the user doesn't exist, prompt for creation
echo "User '$1' does not exist."
read -p "Do you want to create this user? (y/n): " option

if [[ "$option" == "y" || "$option" == "Y" ]]; then
# Generate a strong password (16 random base64 characters)
password=$(openssl rand -base64 16)

# Create the user with a home directory and set password
useradd -m -s /bin/bash "$1" && echo "$1:$password" | chpasswd

echo "User '$1' created successfully!"
echo "Password: $password"
echo "Home directory: /home/$1"
else
echo "User creation aborted."
fi
fi

exit 0

0 Kudos
cyber43
Cadet
Cadet
  • 58 Views

#!/bin/bash

# Function to check and create a user
check_and_create_user() {
read -p "Enter the username to check: " username

# Check if the user exists
if id "$username" &>/dev/null; then
echo "The user '$username' already exists."
else
echo "The user '$username' does not exist."
echo "Creating a new user."

# Generate a strong password
password=$(openssl rand -base64 12)
echo "Generated strong password: $password"

# Create the user and home directory
sudo useradd -m "$username"
echo "$username:$password" | sudo chpasswd

echo "User '$username' has been created. A home directory has been created."
fi
}

# Main execution
if [ "$(id -u)" -ne 0 ]; then
echo "This script requires root privileges to run."
exit 1
fi

check_and_create_user

0 Kudos
pamtally
Cadet
Cadet
  • 48 Views

#!/bin/bash

# Function to check if the provided password is strong
detect_strong_password() {
local password="$1"

if [[ ${#password} -lt 8 || ! "$password" =~ [A-Z] || ! "$password" =~ [a-z] || ! "$password" =~ [0-9] || ! "$password" =~ [^a-zA-Z0-9] ]]; then
return 1 # Password is not strong
else
return 0 # Password is strong
fi
}

# Check if the script is run as root
if [[ $EUID -ne 0 ]]; then
echo "This script must be run as root. Please try again with sudo." >&2
exit 1
fi

# Prompt for the username
read -p "Enter the username to check: " username

# Check if the user exists
if id "$username" &>/dev/null; then
echo "User '$username' already exists."
else
echo "User '$username' does not exist. Creating the user..."

# Prompt for a strong password
while true; do
read -s -p "Enter a strong password for the user: " password
echo
read -s -p "Confirm the password: " confirm_password
echo

if [[ "$password" != "$confirm_password" ]]; then
echo "Passwords do not match. Please try again."
elif ! detect_strong_password "$password"; then
echo "Password is not strong enough. It must be at least 8 characters long and include upper-case letters, lower-case letters, numbers, and special characters."
else
break
fi
done

# Create the user with the home directory and set the password
useradd -m "$username"
echo "$username:$password" | chpasswd

if [[ $? -eq 0 ]]; then
echo "User '$username' has been created successfully with a home directory."
else
echo "Failed to create user '$username'. Please check for errors."
exit 1
fi
fi

0 Kudos
Join the discussion
You must log in to join this conversation.