<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Sophie Ejikeme</title>
    <description>The latest articles on DEV Community by Sophie Ejikeme (@sophie_ejikeme).</description>
    <link>https://dev.to/sophie_ejikeme</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1725367%2Fa0afddc7-c52a-4437-b7cd-be815cb82bed.jpg</url>
      <title>DEV Community: Sophie Ejikeme</title>
      <link>https://dev.to/sophie_ejikeme</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/sophie_ejikeme"/>
    <language>en</language>
    <item>
      <title>Automating a Bash Script</title>
      <dc:creator>Sophie Ejikeme</dc:creator>
      <pubDate>Wed, 03 Jul 2024 18:45:13 +0000</pubDate>
      <link>https://dev.to/sophie_ejikeme/automating-a-bash-script-1ah1</link>
      <guid>https://dev.to/sophie_ejikeme/automating-a-bash-script-1ah1</guid>
      <description>&lt;p&gt;Ever wondered how you can simplify a monotonous task? Worry no more! But before we delve further, what do we actually mean by a script? And why do we have a script? In the context of Bash, a script is a text file containing a series of Bash commands to be executed sequentially.&lt;br&gt;
So, a Bash script will contain some commands intended to carry out a function or task.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;A Realistic Scenario&lt;/strong&gt;&lt;br&gt;
Imagine you have a repetitive task to carry out. Or imagine that you want to add a good number of employees to a group, a Bash script can automate this for you. Such script should be able to perform the tasks below:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Create users and groups&lt;/li&gt;
&lt;li&gt;Set up home directories with appropriate permissions and ownership&lt;/li&gt;
&lt;li&gt;Generate random passwords for the users&lt;/li&gt;
&lt;li&gt;Log all actions to /var/log/user_management.log.&lt;/li&gt;
&lt;li&gt;Store the generated passwords securely in /var/secure/user_passwords.txt&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Now, how can this be done?&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Explanation&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;Step 1: Check root privileges&lt;/strong&gt;&lt;br&gt;
Since user and group management commands require administrative access, the script verifies if it’s run as root.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Check if script is run as root&lt;/strong&gt;&lt;br&gt;
if [[ $EUID -ne 0 ]]; then&lt;br&gt;
   echo "This script must be run as root" &lt;br&gt;
   exit 1&lt;br&gt;
fi&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2: Read the user file&lt;/strong&gt;&lt;br&gt;
The script checks if the user file is provided as an argument and uses it when it is executed&lt;/p&gt;

&lt;p&gt;Check if the file with users and their corresponding groups exists&lt;br&gt;
if ["$#" -ne 1]; then&lt;br&gt;
    echo "Use: $0 "&lt;br&gt;
    exit 1&lt;br&gt;
fi&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 3: Initialize password and log file&lt;/strong&gt;&lt;br&gt;
We will initialize the values of the log and password file. If the files do not exist, we need to create them. For the password file, we need to set appropriate permissions where only the user can read and write it.&lt;/p&gt;

&lt;p&gt;INPUT_FILE=$1&lt;br&gt;
LOG_FILE="/var/log/user_management.log"&lt;br&gt;
PASSWORD_FILE="/var/secure/user_passwords.txt"&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Ensure log file exists and has correct permissions&lt;/strong&gt;&lt;br&gt;
touch "$LOG_FILE"&lt;br&gt;
chmod 600 "$LOG_FILE"&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Ensure password file exists and has correct permissions&lt;/strong&gt;&lt;br&gt;
touch "$PASSWORD_FILE"&lt;br&gt;
chmod 600 "$PASSWORD_FILE"&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 4: Create a user function&lt;/strong&gt;&lt;br&gt;
The purpose of the function is :&lt;/p&gt;

&lt;p&gt;Checking if the user already exists.&lt;br&gt;
Creating a personal group for each user.&lt;br&gt;
Creating the user and adding them to specified groups.&lt;br&gt;
Generating a random password.&lt;br&gt;
Setting the user’s password.&lt;br&gt;
Logging the action and storing the password securely.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;script to create users and groups from a file&lt;/strong&gt;&lt;br&gt;
if [ $# -ne 1 ]; then&lt;br&gt;
    echo "Usage: $0 "&lt;br&gt;
    exit 1&lt;br&gt;
fi&lt;/p&gt;

&lt;p&gt;INPUT_FILE=$1&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Log and password file paths&lt;/strong&gt;&lt;br&gt;
LOG_FILE="/var/log/user_management.log"&lt;br&gt;
PASSWORD_FILE="/var/secure/user_passwords.txt"&lt;/p&gt;

&lt;p&gt;!/bin/bash&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Script: create_users.sh&lt;/strong&gt;&lt;br&gt;
 Description: This script Creates users and groups based on input file, sets up home directories,&lt;br&gt;
              generates random passwords, and logs all actions.&lt;br&gt;
 Usage: ./create_users.sh &lt;/p&gt;

&lt;p&gt;Check if script is run as root&lt;br&gt;
if [[ $EUID -ne 0 ]]; then&lt;br&gt;
   echo "This script must be run as root" &lt;br&gt;
   exit 1&lt;br&gt;
fi&lt;/p&gt;

&lt;p&gt;Check if input file is provided&lt;br&gt;
if [[ $# -eq 0 ]]; then&lt;br&gt;
    echo "Usage: $0 "&lt;br&gt;
    exit 1&lt;br&gt;
fi&lt;/p&gt;

&lt;p&gt;INPUT_FILE=$1&lt;br&gt;
LOG_FILE="/var/log/user_management.log"&lt;br&gt;
PASSWORD_FILE="/var/secure/user_passwords.txt"&lt;/p&gt;

&lt;p&gt;Ensure log file exists and has correct permissions&lt;br&gt;
touch "$LOG_FILE"&lt;br&gt;
chmod 600 "$LOG_FILE"&lt;/p&gt;

&lt;p&gt;Ensure password file exists and has correct permissions&lt;br&gt;
touch "$PASSWORD_FILE"&lt;br&gt;
chmod 600 "$PASSWORD_FILE"&lt;/p&gt;

&lt;p&gt;Function to log messages&lt;br&gt;
log_message() {&lt;br&gt;
    echo "$(date '+%Y-%m-%d %H:%M:%S') - $1" &amp;gt;&amp;gt; "$LOG_FILE"&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;Function to generate a random password&lt;br&gt;
generate_password() {&lt;br&gt;
    openssl rand -base64 12 | tr -d '=+/'&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;Read input file line by line&lt;br&gt;
while IFS=';' read -r username groups; do&lt;br&gt;
    # Skip empty lines&lt;br&gt;
    [[ -z "$username" ]] &amp;amp;&amp;amp; continue&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; Create user if it doesn't exist
if id "$username" &amp;amp;&amp;gt;/dev/null; then
    log_message "User $username already exists. Skipping user creation."
else
    useradd -m -s /bin/bash "$username"
    if [[ $? -eq 0 ]]; then
        log_message "User $username created successfully."
    else
        log_message "Failed to create user $username."
        continue
    fi
fi

 Set up home directory permissions
chmod 700 "/home/$username"
log_message "Set permissions for /home/$username"

 Generate and set random password
password=$(generate_password)
echo "$username:$password" | chpasswd
echo "$username:$password" &amp;gt;&amp;gt; "$PASSWORD_FILE"
log_message "Set password for user $username"

 Create and add user to groups
IFS=',' read -ra group_array &amp;lt;&amp;lt;&amp;lt; "$groups"
for group in "${group_array[@]}"; do
    if ! getent group "$group" &amp;amp;&amp;gt;/dev/null; then
        groupadd "$group"
        log_message "Group $group created."
    fi
    usermod -aG "$group" "$username"
    log_message "Added user $username to group $group"
done
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;done &amp;lt; "$INPUT_FILE"&lt;/p&gt;

&lt;p&gt;log_message "User creation process completed."&lt;br&gt;
echo "User creation process completed. Check $LOG_FILE for details."&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Running the Script&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Create users file
Add your users and their groups in the format user;groups. Save and close the file.&lt;/li&gt;
&lt;li&gt;Make the file and the script executable&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;chmod +x users.txt&lt;/p&gt;

&lt;p&gt;chmod +x create_script.sh&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Run the Script&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;sudo ./create_script.sh users.txt&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Points to Consider&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Save this script as create_users.sh. Open your terminal, for example, Ubuntu, type nano create_users.sh&lt;br&gt;
Ensure you save and exit (just type ctrl o, click enter and then ctrl x).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Make it executable e.g chmod +x create_users.sh&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Create a text file where you'll have the names of the users and their groups e.g nano users.txt&lt;br&gt;
type names of employees using this format - Jones;Dev,Audit&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;br&gt;
This automation promotes time saving, productivity and efficiency.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;About HNG Internship&lt;/strong&gt;&lt;br&gt;
HNG Internship is a fast-paced bootcamp for learning digital skills. It's focused on advanced learners and those with some pre-knowledge, and it gets people into shape for job offers. In the HNG bootcamp, you work in teams to build apps and solve problems. For more information on what they offer, you can reach them through the link below. &lt;br&gt;
&lt;a href="https://hng.tech/internship"&gt;https://hng.tech/internship&lt;/a&gt;, &lt;a href="https://hng.tech/hire"&gt;https://hng.tech/hire&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
