(RHEL, Bash) List users and check if they have logged on during the last 2 months
Hi everyone,
At work we were told to check the list of users of an application server and delete all those that have left the company or don't need access to the application anymore. Here's what I came up with. Would you be as kind as to tell me your opinion and whether there is a faster / easier way to accomplish the same thing? 1) Save the list of user names (1st field in /etc/passwd) in a text file (~500 users). 2) Merge /var/log/wtmp and /var/log/wtmp.1 (logrotate is configured to keep only 1 rotated wtmp log) into a single file with Code:
cat /var/log/wtmp.1 /var/log/wtmp > wtmp Code:
gacanepa@Gabriel-PC ~ $ file /var/log/wtmp Code:
last -f wtmp | sort -uk1,1 > wtmp.txt 5) Each "inactive user" is logged into Some points to consider: 1) Here's the section of /var/log/wtmp in our logrotate.conf file: Code:
# no packages own wtmp, or btmp -- we'll rotate them here 2) For the same reasons as above we can't use chage either to disable accounts. I hope I made myself clear enough :). Any suggestions will be more than welcome. |
I would run:
Code:
for i in $(cat users) ; do (last ;last -f /var/log/wtmp.1) | grep -q $i || echo $i ; done On a side topic, the package acct might interest you. |
If I am not mistaken, the users command only shows the users that are currently logged on to the current host (from man users), and I need to check the whole list of local users (as found in /etc/passwd) and see which ones have logged on at least once during the period of time recorded in wtmp (and wtmp.1, after wtmp is rotated).
So, IMHO (please feel free to correct me if I'm wrong) the following one-liner Code:
for i in $(cat users) ; do (last ;last -f /var/log/wtmp.1) | grep -q $i || echo $i ; done I will mark your thread as helpful because this will definitely come in handy one of these days ;). Also, I will be a follower of your blog. Nice job there! And that is so cool that you were a part of the team behind the Happy Feet 2 movie! |
Hey gacanepa
Thanks :) It was a good experience, some high end tech gear used to make animations. My mistake on the command, I meant to add a disclaimer that the "users" would be the file of users you mentioned you created from the passwd file. Something simple like: Code:
cut -f1 -d":" /etc/passwd Code:
for i in $(cut -f1 -d":" /etc/passwd) ; do (last ;last -f /var/log/wtmp.1) | grep -q $i || echo $i ; done |
Oh I see now ;). This works fairly well - I'll just need to add a little tweaking in order to consider only "real" users and not daemons (forgot to say that in my first post) - but that will be a piece of cake. You have pointed me in the right direction and that's enough for me. I will also mark your last post and helpful and this thread as solved.
On a side note, if you don't mind, I'll send you my personal email address so you can -when you have one minute or 2- fill me in a little of your work in the Happy Feet movie. I am always curious of the "real world applications" (which distros, applications for animations used in the movie, etc) of Linux besides the enterprise environment - which is what I have the most experience on. |
No worries at all.
|
Great. Sent you an email to the address listed in your blog. Thanks again!
|
All times are GMT -5. The time now is 09:34 AM. |