Operating System - HP-UX
1846413 Members
2659 Online
110256 Solutions
New Discussion

Re: Shell script to compare the date !!

 
SOLVED
Go to solution
Chris Fung
Frequent Advisor

Shell script to compare the date !!

Hi all,

I would like to write a script for tracking those users who were not logged in to the server for more than 3 months. By comparing the /bin/last output for /var/adm/wtmp file and password file, I obtained a list of username and timestamps for their login and logout time. Now the problem is how can I compare the date specified in /var/adm/wtmp file with the current date?? I couldn't find any useful command for this task.

Appreciated it if anyone can help solving this problem with sample codes and/or url links.

Many thanks,

Chris,

4 REPLIES 4
Steven Sim Kok Leong
Honored Contributor
Solution

Re: Shell script to compare the date !!

Hi,

Try putting A. Clay Stephenson's caljd.sh script to good use, where possible:

http://forums.itrc.hp.com/cm/QuestionAnswer/1,,0x6e7a7d4cf554d611abdb0090277a778c,00.html

Hope this helps. Regards.

Steven Sim Kok Leong
A. Clay Stephenson
Acclaimed Contributor

Re: Shell script to compare the date !!

Hi:

This is one of those cases where caljd.sh (or the newer caljd.pl) is probably not the best choice. My approach would be to use that really big number (the epoch seconds timestamp) just before the month.

#!/usr/bin/sh
FIN=/var/adm/wtmp
CMD=/usr/sbin/acct/fwtmp

STAT=1
if [ -r ${FIN} -a -x ${CMD} ]
then
${CMD} < ${FIN} | perl wtmpparse.pl
STAT=$?
fi
exit ${STAT}

The attached wtmpparse.pl script will then look at the timestamp and append something like '32.45 days' to each input line. If you look at the perl script, you will see a commented line that will instead output an integer '32 days' instead.

This should be very close to your needs. You may also need to play with the substr() offset to locate the timestamp within your platform's fwtmp output.



If it ain't broke, I can fix that.
Sridhar Bhaskarla
Honored Contributor

Re: Shell script to compare the date !!

Chris,

One another way I can think of is to check the home directory using -mtime option of find.

1. Get the user list from /etc/passwd
2. For each user, determine if any of the files in his/her home directory have been modified since the desired number of days. .last_login is one such file by default gets modified when the user logs in.

A primitive script is below. You can modify it to your requirements.

#!/usr/bin/ksh

if [ $# != 1 ]
then
echo "Usage: $0 days_of_inactivity"
exit 1
fi

STAMP=$1

cat /etc/passwd |
while read entry
do
LOGIN=`echo $entry |awk '{FS=":";print $1}'`
HOME=`echo $entry |awk '{FS=":";print $6}'`

SUCCESS=`find $HOME -mtime -$STAMP -print -quit | wc -l`

if [ $SUCCESS -eq 0 ]
then
echo "$LOGIN "
fi
done
You may be disappointed if you fail, but you are doomed if you don't try
Chris Fung
Frequent Advisor

Re: Shell script to compare the date !!

Thanks for all of your suggestions. I've already made use of Clay's Caljd.sh to finish my task!!

I created a script which calls Caljd.sh to calculate the julian day for me. Then I compare the current Julian day and the returned Julian days from the /var/adm/acct/sum/loginlog file (suppose a cron job will run the lastlogin command everyday to update the logiinlog file).

If the current Julian day - returned Julian day >= 90 days (i.e 3 months roughly), the particular username will be printed out in a report.

Cheers,

Chris,