(RHEL, Bash) List users and check if they have logged on during the last 2 months


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting (RHEL, Bash) List users and check if they have logged on during the last 2 months
# 1  
Old 10-03-2013
Lightbulb (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

3) Convert the wtmp file (which is of type data):
Code:
gacanepa@Gabriel-PC ~ $ file /var/log/wtmp
/var/log/wtmp: data

to a plain text file sorted by 1st field (user names) and filtered by last occurrence of user name:
Code:
last -f wtmp | sort -uk1,1 > wtmp.txt

4) Check one by one the list of users created in step #1 to see whether they appear in the wtmp.txt file. If they don't appear in this file, which lists the logins for the current and past month, it means they haven't logged on during the same period, and we can consider deleting them.
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
/var/log/wtmp {
    monthly
    create 0664 root utmp
    rotate 1
}

Unfortunately, we can't edit it.
2) For the same reasons as above we can't use chage either to disable accounts.
I hope I made myself clear enough Smilie. Any suggestions will be more than welcome.
# 2  
Old 10-03-2013
If the users are supposed to login localy, you could try (as root):
Code:
cat /etc/passwd|sed s,:,\ ,g|awk '{print $3" "$1}'|sort -r > var

while read line;do
    uid=$(echo $line|awk '{print $1}')
    usr=$(echo $line|awk '{print $2}')
    [[ $uid -ge 1000 ]] && \
        echo "User $usr, last login: $(ls -la /home/$usr/.xsession-errors|awk '{print $6$7}')"
done < var

Hope this helps
This User Gave Thanks to sea For This Post:
# 3  
Old 10-03-2013
Quote:
Originally Posted by sea
If the users are supposed to login localy, you could try (as root):
Code:
cat /etc/passwd|sed s,:,\ ,g|awk '{print $3" "$1}'|sort -r > var

while read line;do
    uid=$(echo $line|awk '{print $1}')
    usr=$(echo $line|awk '{print $2}')
    [[ $uid -ge 1000 ]] && \
        echo "User $usr, last login: $(ls -la /home/$usr/.xsession-errors|awk '{print $6$7}')"
done < var

Hope this helps
Unfortunately I can't login as root at work and this script is supposed to run under normal user permissions. But I will keep your suggestion in mind in case I need to do the same thing at home (where I DO have root privileges Smilie ).
In the meanwhile, my script is working just fine. I was just wondering if there was a more effective way of getting the job done with normal user permissionsSmilie.
# 4  
Old 10-03-2013
If you have sudo access, just place in sudo in front of the ls command, invoked in the echo line...

Sorry dont any wtmp files available..

Last edited by sea; 10-03-2013 at 11:11 PM..
# 5  
Old 10-03-2013
[SOLVED] (RHEL, Bash) List users and check if they have logged on during the last 2 months

No root and no sudo access either Smilie. Sorry I forgot to mention that in my first post.
Thank you sea for taking the time to share your knowledge with me. I will bookmark this thread for my future reference.
Someone over at linuxquestions.org pointed me in the right direction. With that information and sea's help, I believe I have my answer so I'll mark this thread as solved.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to show a list of currently logged in and logging out users?

Hi Guys! I am sure that this question might appeared previously, but I still don't know how to show a list of logged out users. Please help with this! Thanks in advance:) (5 Replies)
Discussion started by: saloliubliu
5 Replies

2. Shell Programming and Scripting

Bash Help: users who are not logged into the system to display

A Newbie here, I am working on a script and am having problems with the else part of the script. I can't get the users who are not logged into the system to display on the screen with their username and the text "The user is not logged in". I am sure it is something simple and stupid, but I... (5 Replies)
Discussion started by: rchirico
5 Replies

3. UNIX for Dummies Questions & Answers

How many users are logged in?

How do I find this out? I have a feeling its a simple command such as who, but I just don't know what it is. I've had a search on here but either I can't put it into the right search criteria or there isn't a topic on it. Thanks. EDIT: Delete this thread, as I posted it I noticed the... (0 Replies)
Discussion started by: chris_rabz
0 Replies

4. Programming

Get the list of logged in users

How can I get the list of logged in users in the system programmatically? I can get the list with 'who' or 'users' commands but I need to get the list programmatically... May someone help, please? Thanks in advance. (2 Replies)
Discussion started by: xyzt
2 Replies

5. UNIX for Dummies Questions & Answers

How to do a login time and date list of users never logged on?

Hello, I'm trying to do a list of user that never connected to a couple of servers. I want to do a diff between the servers lists, and print out only the users that never has logged on each server. Here my first step : SERVER01: # finger `egrep -v -e "^\s*#" /etc/passwd | awk '{ print $1 }' |... (4 Replies)
Discussion started by: gogol_bordello
4 Replies

6. Shell Programming and Scripting

Script to check users logged out

Hi, Here is the script that I have written to check if a particular user is has logged out, and if yes, then a mail needs to be sent to the management the details of whatever has been captured by the script command. echo "The current users are:" who | awk '{print $1}' | sort > temp1 cp... (1 Reply)
Discussion started by: ggayathri
1 Replies

7. Solaris

List all inactive users who has not logged on since last 90 days

I need actuall script which List all inactive users who has not logged on since last 90 days Thanks in advance. Di! (17 Replies)
Discussion started by: haridham
17 Replies

8. UNIX for Dummies Questions & Answers

List all inactive users who has not logged on since last 90 days

Hi, Can I get a script to list out all the users, who has not logged on since last 90 days. Last command in not working due due to /var/adm/wtmpx is more than 2 GB. Thanks in advance. Regards, Roni (10 Replies)
Discussion started by: manasranjanpand
10 Replies

9. Post Here to Contact Site Administrators and Moderators

logged out users

how to find out users who logged out within 5 minutes (1 Reply)
Discussion started by: roshni
1 Replies

10. Shell Programming and Scripting

Trying to get list of logged on users sorted

I'm trying to execute a single shell command that will give me a sorted list of all the users currently logged into the system, displaying the users name as it appears in /etc/passwd. I've tried awk -F: '{print $1}' /etc/passwd | xargs finger -s | cut -c11-28 | uniq This list whoever does... (7 Replies)
Discussion started by: kungfuice
7 Replies
Login or Register to Ask a Question