j0h

userPass.sh

j0h
Dec 15th, 2023
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.63 KB | None | 0 0
  1. #!/bin/bash
  2. #This program reads the user file then,
  3. #sets a random password the user must change at login.
  4. #results of username / password are pushed to a file,
  5. #which seems insecure, but it was requested.
  6.  
  7. # Define the input file containing usernames, one per line
  8. input_file="users.txt"
  9.  
  10. # Define the output file for username and password pairs
  11. output_file="user_passwords.txt"
  12.  
  13. # Check if the input file exists
  14. if [ ! -f "$input_file" ]; then
  15.     echo "Input file '$input_file' not found."
  16.     exit 1
  17. fi
  18.  
  19. # Check if the output file exists and remove it if it does
  20. if [ -f "$output_file" ]; then
  21.     rm "$output_file"
  22. fi
  23.  
  24. # Read each name from the input file and create a user
  25. while IFS= read -r name; do
  26.     # Generate a random password (change this to fit your requirements)
  27.     password=$(openssl rand -base64 12)
  28.    
  29.     #write a thing to see if the username already exists.
  30.     #if it exists, list , move on, if it doesnt exist, add it with parameters
  31.     chage -l "$name" >> "$output_file"
  32.    
  33.     # Create the user with a random password
  34.     sudo useradd -m -s /bin/bash -p "$(echo "$password" | openssl passwd -1 -stdin)" "$name"
  35.        
  36.     # Force the user to change their password on first login and every 60 days there after
  37.     sudo chage -d 0 -M 60 "$name"
  38.    
  39.     # Write the username and password to the output file
  40.     echo "Username: $name" >> "$output_file"
  41.     echo "Password: $password" >> "$output_file"
  42.     echo "====================" >> "$output_file"
  43.    
  44.     echo "User '$name' created with a random password."
  45. done < "$input_file"
  46.  
  47. echo "Usernames and passwords are saved in '$output_file'."
Add Comment
Please, Sign In to add comment