Bash script to get list of expired local users on Linux

Scenario: How to get the list of expired local users on Linux.
System :
CentOS/RHEL 6/7
IP address: 192.168.99.11
Hostname : horse

Steps:
1) Create the bash script file with file name listchageusers.sh
$ vi /home/bachem/listchageusers.sh

#!/bin/bash
awk -F':' '{ system("echo " $1 " && chage -l " $1) }' /etc/passwd

3) Make the file executable
$ chmod +x /home/bachem/listchageusers.sh

4) Finish,run the script as root or sudoers and the output should like below..

[bachem@horse ~]$ sudo ./listchageusers.sh
[sudo] password for bachem:
root
Last password change : Apr 15, 2018
Password expires : never
Password inactive : never
Account expires : never
Minimum number of days between password change : 0
Maximum number of days between password change : 99999
Number of days of warning before password expires : 7
bin
Last password change : Nov 05, 2016
Password expires : never
Password inactive : never
Account expires : never
Minimum number of days between password change : 0
Maximum number of days between password change : 99999
Number of days of warning before password expires : 7
.....
.....
.....
bachem
Last password change : May 12, 2018
Password expires : never
Password inactive : never
Account expires : never
Minimum number of days between password change : 0
Maximum number of days between password change : 99999
Number of days of warning before password expires : 7
[bachem@horse ~]$

to save the listchageusers.sh output to a filename called hostname_chage.txt do below:
– Edit bash script that created above add red font in below
$ vi /home/bachem/listchageusers.sh

#!/bin/bash
awk -F':' '{ system("echo " $1 " && chage -l " $1) }' /etc/passwd >> /home/bachem/$(hostname)_chage.txt
echo "Generate Done,file save as /home/bachem/$(hostname)_chage.txt"

– Run the script the output will save into a filename $(hostname)_chage.txt

[bachem@horse ~]$ sudo ./listchageusers.sh
[sudo] password for bachem:
Generate Done,file save as /home/bachem/horse_chage.txt
[bachem@horse ~]$ ll /home/bachem/horse_chage.txt
-rw-rw-r--. 1 bachem bachem 9989 Jun 5 20:58 /home/bachem/horse_chage.txt
[bachem@horse ~]$

to save the listchageusers.sh output to a filename called ipaddress_chage.txt do below:
– Edit bash script that created above add red font in below
$ vi /home/bachem/listchageusers.sh

#!/bin/bash
# Generate active Ip Address
ipaddress=$(ip route get 1 | awk '{print $NF;exit}')

awk -F':' '{ system("echo " $1 " && chage -l " $1) }' /etc/passwd >> /home/bachem/"$ipaddress"_chage.txt"
echo "Generate Done,file save as /home/bachem/"$ipaddress"_chage.txt"

– Run the script the output will save into a filename “$ipaddress”_chage.txt

[bachem@horse ~]$ sudo ./listchageusers.sh
[bachem@horse ~]$ sudo ./listchageusers.sh
Generate Done,file save as /home/bachem/192.168.99.11_chage.txt
[bachem@horse ~]$ ll /home/bachem/192.168.99.11_chage.txt
-rw-rw-r--. 1 bachem bachem 9989 Jun 5 21:06 /home/bachem/192.168.99.11_chage.txt
[bachem@horse ~]$

Leave a Reply

Your email address will not be published. Required fields are marked *