1834156 Members
2607 Online
110064 Solutions
New Discussion

last login script help

 
SOLVED
Go to solution
Nick D'Angelo
Super Advisor

last login script help

I have an awk script that I have created to check the last time users have logged in.

awk -F: '{print "last | grep "$1" | head -1"}' /etc/passwd|ksh - > /home1/nickd/lastlogin

The missing piece is to sort the output file so that the file is sorted from newest to oldest.

Can anyone suggest how I might be able to do that?

All help is appreciated.

Always learning
12 REPLIES 12
James R. Ferguson
Acclaimed Contributor

Re: last login script help

Hi Nick:

A while back I wrote a script for someone that did this, taking care of sorting dates from 'last'. I've attached it here. Run it with '-v' if you want verbose output. Without that option, you get a summary of login per user.

...JRF...

boley janowski
Trusted Contributor

Re: last login script help

i dont see a quick easy way, how about grep for the month, then redirect those to there own month file, then sort each one of those with a for loop by the day, and finally cat all of them to your lastlogin file.


for i in `cat monthfile`
do
<script> | grep ${i} >> ${i}
done

for i in `cat monthfile`
do
sort -k 5n,5 ${i} > ${i}x
done

for i in `cat monthfile`
do
cat ${i}x >> lastlogin
rm ${i} ${i}x
done

Im sure there are less painful ways of doing this but its a way.

good luck
boley janowski
Trusted Contributor

Re: last login script help

Nick,

Like I said there are less painful ways of doing what you want, James does what you want, and you can cut out what you dont, that it reports.

James,

Pretty cool script, Thanks!!
Nick D'Angelo
Super Advisor

Re: last login script help

Gents,

Thank you but, it still does not sort in either direction, either oldest to newest or newest to oldest. In fact, if you don't mind me saying so, my script is actually a little cleaner in the output. For example, here is a sample:
root ftp Wed May 2 12:46 - 12:47 (00:01)
cherubin ttyp1 Tue May 8 15:48 - 16:14 (00:26)
admin ttypd Mon Mar 12 15:15 - 15:22 (00:06)
lpiscate ttyp9 Thu Mar 22 10:28 - 12:51 (02:23)
archer ttyp6 Tue May 8 17:07 - 17:52 (00:45)
stephens pty/ttys3 Tue May 8 11:44 - 13:16 (01:32)
barthele ttyq7 Tue May 8 08:36 - 16:48 (08:11)
toigo ttyr6 Tue May 8 08:43 - 17:37 (08:53)
petrecca ttyq2 Tue May 8 13:35 - 15:45 (02:10)

Thanks, I will keep looking.
Always learning
James R. Ferguson
Acclaimed Contributor

Re: last login script help

Hi Nick:

If you run the '-v(erbose)' variation of the first script I offered, you would see that the sort order is ascending last login (oldest date and time to most recent date and time) within user as ordered in /etc/passwd.

In any event, if all that you want is one line per user, from most recent date and time of login to the oldest, with an ascending user (account) bias, then try the attached script.

...JRF...
Nick D'Angelo
Super Advisor

Re: last login script help

James,

Hmm, I am not sure about this now. Where do I put the -v (verbose)?

In the last or in the sort?

Nickd
Always learning
James R. Ferguson
Acclaimed Contributor

Re: last login script help

Hi Nick:

In my first attachment, you could run the script with or without the '-v' option as:

# /tmp/jrf_last
# /tmp/jrf_last -v

Simply run the second script in the second attachment as:

# /tmp/jrf_lastuser

This last script, I believe, is very close to that which you are looking.

Regards!

...JRF...
boley janowski
Trusted Contributor

Re: last login script help

the combinations of for loops i initially suggested uses your script hence "<script>" i just went through and ran based on a file with the months in it, you can modify that all you want to get them in the order you want, however when i ran the other script that was suggested i got what i thought you were looking for, but if not you can take the output from that and run it through the for loops that i had suggested previously, just modify the search file as you see fit to serve your need.
Nick D'Angelo
Super Advisor

Re: last login script help

James,

Close, but not yet. Am I missing something?
This is the script that I am using.
Unfortunately, it does not sort by date, which is what I was looking for.

Should I use runacct instead?

Thanks,

#!/usr/bin/sh
#
LOG=/tmp/last.log
IFS=:
#
rm -f $LOG
#
while read USER NULL
do
last -1 $USER | awk 'NF > 6 {print $0}' >> $LOG
done < /etc/passwd
#
sort -o $LOG -Mrk4 -krn5 -krn6.1,6.2 -krn6.4 -kb1 $LOG
#
#.jrf.



Always learning
James R. Ferguson
Acclaimed Contributor
Solution

Re: last login script help

Hi Nick:

OK, you have me confused. The script I gave you *does* sort by date. The output is in descending date order (i.e. most recent date, first) with *one* entry (if available) from the /var/adm/wtmp file for every user defined in /etc/passwd.

What do you want?

...JRF...
...JRF...
Nick D'Angelo
Super Advisor

Re: last login script help

James,

You are absolutely right.

The script is perfect for me. I must have been running the wrong version or something.

Thanks and don't mind my pointy headedness.

<:-)

Nickd
Always learning
James R. Ferguson
Acclaimed Contributor

Re: last login script help

Hi Nick:

No problem; I've certainly been there too...just glad to have been of help.

Regards!

...JRF...