0

I have to audit the users accounts on our linux servers in each environment so I want to script checking whether each account is expired or not. I have a for loop that's pulling user accounts listed in /etc/passwd and stores them in an array.

#!/bin/bash
users = $(awk -F: '{ print $1}' /etc/passwd)
uservar=($users)

for value in "${uservar[0]}"
do chage -l $value
done

Ideally the script would display a username first then on a new line it would show the account expiration date and other info for that user account. The process would repeat for each account. At the moment its just displaying the account expiration and dates for all user accounts without the username so I can't tell the accounts apart. Everything is also stacked on top of each other so its difficult to read. Ideally the output would look like this:

userA
Last Password Change:    1/2/34
Password Expires:        1/2/34
Password Inactive:       1/2/34
.....

userB
Last Password Change:    1/2/34
Password Expires:        1/2/34
Password Inactive:       1/2/34
.....

I would appreciate any help/suggestions on this.

jrd1989
  • 698
  • 15
  • 48
  • 1
    Just my short google skills: https://www.2daygeek.com/linux-check-user-password-expiration-date - i think this can be extened to your needs? – djdomi May 18 '21 at 15:03

1 Answers1

1

The literal answer to your question:

#!/bin/bash
users=$(awk -F: '{ print $1}' /etc/passwd)

for value in $users
do 
      echo $value
      chage -l $value
      echo "....."
      echo ""
 done

should generate output in the form of

userA
Last Password Change:    1/2/34
Password Expires:        1/2/34
Password Inactive:       1/2/34
.....

userB
Last Password Change:    1/2/34
Password Expires:        1/2/34
Password Inactive:       1/2/34
.....

Note that the source of the information chage displays is encoded in the fields of the /etc/shadow file (expressed as the number of days since Jan 1, 1970) .
Parsing that file directly might be significantly more efficient than using chage and/or other tools that convert those fields to human readable output and parsing that.

Bob
  • 5,805
  • 7
  • 25
  • That worked perfectly. Is it possible to add passwd --status to a separate line? When I try to run it similar to the chage -l cmd it complains about a syntax error. I'm assuming it has to do with --status? – jrd1989 May 19 '21 at 12:33
  • Disregard, I realized I can use 'passwd -S' which accomplishes the same thing. Thanks again for the help! – jrd1989 May 19 '21 at 14:52