Created
December 8, 2022 09:02
-
-
Save wynnchel/62d81c84da42e3bac3e4a9ab4428854f to your computer and use it in GitHub Desktop.
v2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# This script MUST be run with proper priveleges | |
# Root/sudo is needed for the chage command! | |
# Defines the name of the file to be created | |
# The format is: hostname_date-of-today.csv | |
filename="$(hostname)_user_password_age_report_$(date '+%Y-%m-%d').csv" | |
# Creates the destination file and | |
# prepares the colomns used in the output file | |
# | |
# The output format will be: | |
# | |
# hostname,user,last changed, age in days | |
# xzy104,john,2022-01-02,155d,/bin/bash | |
echo "hostname;username;last changed;password age" > "$filename" | |
# The loop does the following steps: | |
# Read /etc/passwd | |
# Extract all users and shells | |
# Calculate the password age | |
# Write data to file | |
while read -r line; | |
do | |
# Extract the username from line which was read in | |
USR=$(echo "$line" | cut -f1 -d:) | |
# Extract the shell from line which was read in | |
SHLL=$(echo "$line" | rev | cut -d: -f 1 | rev) | |
# Get the date of the last password change | |
# DOLPC: Date OF the Last Password Change | |
DOLPC=$( \ | |
chage -l "$USR" | \ | |
head -n 1 | \ | |
cut -f 2 -d ':' | \ | |
cut -d ' ' -f 2- | \ | |
date +"%Y-%m-%d" -f - | |
) | |
# Calculate the password age | |
# PAID: Password Age In Days | |
PAID=$(( ( $(date +%s) - $(date --date="$DOLPC" +%s) ) / (60*60*24) )) | |
# Write data to file | |
{ | |
echo -n "$(hostname),"; # Hostname | |
echo -n "$USR," # Username | |
echo -n "$DOLPC," # Date of the last password age | |
echo -n "$PAID," # Password age in days | |
echo "$SHLL" # Shell of the user | |
} >> "$filename" | |
done < /etc/passwd |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment