DaysSinceLastPasswdChange.pl

#!/usr/bin/perl

# DaysSinceLastPasswdChange.pl

# Checks to see when users' passwords
# were last changed.

# - Jim Maher

# Input File Handlers:
my $IFile1="/etc/security/passwd";

# Hash table to store user data.
my %Users;

# Variables:

# Save the date as the number of seconds since January 1, 1970.
$DateInSeconds = time();

# Open $IFile1 for reading only.
open (LastChange, '<', $IFile1) or die ("Unable to open \"$IFile1\" due to error: \"$!\"");

# Read in (one line at a time) the file which has the data about
# when the last time a user logged in.
while (defined ($_ = ))
{

  # If you find a user's name (which always starts in
  # column 1), then print it.

  # (\w+) will hold the user's name.
  # $1 = (\w+).  Save the user's name.
  if ( $_ =~/^(\w+):/)
  {
     # Save the memory value.
     $user="$1";
     # Add the user to the hash table.
     $Users{$user}="*";
  }

  # If you find a time stamp which will
  # be on the line "lastupdate=", do some math to find
  # out when the password was changed.

  if ( $_ =~ /lastupdate/)
  {

    # Find the time stamp and save it in $1.
    $_ =~ /(\d+)/;

    $DateInDays = ($DateInSeconds - $1)/60/60/24;

    # Remove the fraction portion from the variable.
    # Store the value in $1.
    $DateInDays =~ (/(\d+)\./);

    # You now have the time in days stored in $1.
    # Save it to the hash table.
    $Users{$user}=$1;
  }
}

# Display the data.

# Print out a heading.

print "Days Just Last Password Change\n\n";
printf "%8s         %4s\n", "User", "Days";
printf "%8s         %4s\n", "----", "----";

# Print out the hash table.

for my $key (sort keys (%Users) )
{
  printf "%8s          %3s\n", $key,$Users{$key};
}

print "\n\"\*\" means the password has never been set.\n\n";
close LastChange;