#!/bin/bash
#This program reads the user file then,
#sets a random password the user must change at login.
#results of username / password are pushed to a file,
#which seems insecure, but it was requested.
# Define the input file containing usernames, one per line
input_file="users.txt"
# Define the output file for username and password pairs
output_file="user_passwords.txt"
# Check if the input file exists
if [ ! -f "$input_file" ]; then
echo "Input file '$input_file' not found."
exit 1
fi
# Check if the output file exists and remove it if it does
if [ -f "$output_file" ]; then
rm "$output_file"
fi
# Read each name from the input file and create a user
while IFS= read -r name; do
# Generate a random password (change this to fit your requirements)
password=$(openssl rand -base64 12)
#write a thing to see if the username already exists.
#if it exists, list , move on, if it doesnt exist, add it with parameters
chage -l "$name" >> "$output_file"
# Create the user with a random password
sudo useradd -m -s /bin/bash -p "$(echo "$password" | openssl passwd -1 -stdin)" "$name"
# Force the user to change their password on first login and every 60 days there after
sudo chage -d 0 -M 60 "$name"
# Write the username and password to the output file
echo "Username: $name" >> "$output_file"
echo "Password: $password" >> "$output_file"
echo "====================" >> "$output_file"
echo "User '$name' created with a random password."
done < "$input_file"
echo "Usernames and passwords are saved in '$output_file'."