Shell script to display user account expiry details
Q.Can we write a shell script to send notification to administrator or root about the accounts which are going to expire?
Ans : Here is a small script which will give you mail on the accounts which are expired and which are going to expire in 7 days. You are free to use and modify this script for your work.
#Author:Surendra Kumar Anne#Created on:09-02-2012
#Purpose:To check the user account expire status in Linux, unix, BSD etc
cat /etc/shadow | cut -d: -f1,8 | sed /:$/d > /tmp/expirelist.txt
totalaccounts=`cat /tmp/expirelist.txt | wc -l`
for((i=1; i<=$totalaccounts; i++ ))
tuserval=`head -n $i /tmp/expirelist.txt | tail -n 1`
username=`echo $tuserval | cut -f1 -d:`
userexp=`echo $tuserval | cut -f2 -d:`
userexpireinseconds=$(( $userexp * 86400 ))
#check if the user expired or not?
if [ $userexpireinseconds -ge $todaystime ] ;
timeto7days=$(( $todaystime + 604800 ))
if [ $userexpireinseconds -le $timeto7days ];
mail -s "The account $username will expire less than 7 days" root
mail -s "The user account $username already expired" root
doneThis script will send multiple mails to root about the status of expired and going to expire user accounts.The following two tabs change content below.Mr Surendra Anne is from Vijayawada, Andhra Pradesh, India. He is a Linux/Open source supporter who believes in Hard work, A down to earth person, Likes to share knowledge with others, Loves dogs, Likes photography. He works as Devops Engineer with Taggle systems, an IOT automatic water metering company, Sydney . You can contact him at surendra (@) linuxnix dot com.
Latest posts by Surendra Anne (see all)
- FREE: JOIN OUR DEVOPS TELEGRAM GROUPS - August 2, 2019
- Review: Whizlabs Practice Tests for AWS Certified Solutions Architect Professional (CSAP) - August 27, 2018
- How to use ohai/chef-shell to get node attributes - July 19, 2018
- wget download a file to a directory in Linux/Unix - June 4, 2018
- GIT: How to compare two GIT branches? - June 3, 2018