Sponsored Content
Top Forums UNIX for Advanced & Expert Users Finding user accounts not accessed for a specific number of days Post 302556157 by admin_xor on Saturday 17th of September 2011 04:13:08 PM
Old 09-17-2011
Java

Thank you all for your reply. This weekend I'm planning to sit tight and write a script for this. Once done, I'll post that here and would love to get your reviews. Smilie

---------- Post updated 09-17-11 at 04:13 PM ---------- Previous update was 09-16-11 at 07:45 PM ----------

Well thanks for your input guys. Here is the bash script I have written for our Linux and Solaris servers. It's almost similar to the ksh script I wrote for the same purpose on AIX servers. The difference is with AIX's lsuser command, it was easy for me to get user details.

Review the code and let me know your thoughts Smilie
Code:
#!/bin/bash

# this script searches for the user ids which have not been used for a specific
# number of days


# the number of days to be monitored
expdays=30
expiry=$(( 86400*$expdays ))

LOG1=/tmp/inactive_usr_$expdays_days.$( date '+%m-%d-%Y' ).log
LOG2=/tmp/never_logged_usrs.$( date '+%m-%d-%Y' ).log
tmp1=/tmp/tmp1.log.$$

# list out the accounts which can be used to log in to the server

cat /etc/passwd | grep -Ev ^"root|daemon|nobody|adm|sys" | cut -d ":" -f1 >>$tmp1

# date and time calculation with respect to unix epoch

year=$( date +%Y )

today=$( date +%s )

# now, for each of the user listed above, run a check if the last logged in time exceedes the specified number of days.

cat $tmp1 | while read usr; do
        umnt=$( last -1 $usr | head -1 | awk '{ print $5 }' )
        uday=$( last -1 $usr | head -1 | awk '{ print $6 }' )
        uhour=$( last -1 $usr | head -1 | awk '{ print $7 }' | cut -d ":" -f1 )
        umin=$( last -1 $usr | head -1 | awk '{ print $7 }' | cut -d ":" -f2 )

        # the below line checks and stores the IDs which were never used
        if [ ! $uday ] || [ ! $uhour ] || [ ! $umin ]; then
            echo "$usr:__NeveR__" >>$LOG2
        else
            llogin=$( date +%s -d"$umnt $uday, $year $uhour:$umin:01" )
            mini=$(( $today-$llogin ))
            if [[ $mini -gt $expiry ]]; then
                login=$(( ($today-$llogin)/86400 ))
                echo "$usr:$login" >>$LOG1
            fi
        fi
done

rm -f $tmp1

 

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

User Accounts

I have found a script to create user accounts. But there are a few lines i dont understand. Can someone help me with this? Here's the code: ###################################### while ; do ACCT=${USER_ACCT}${START} START=`expr $START + 1` START=`echo ${START} | awk... (3 Replies)
Discussion started by: Sensor
3 Replies

2. Shell Programming and Scripting

Counting Number of times a File is accessed

Hi, I need to count the number of times a script is accessed from within the script. Is it possible ? Example: I have a script called lo.sh and i execute the script for the first time, then the counter variable declared inside the lo.sh should increment by 1. For every execution the... (1 Reply)
Discussion started by: pathanjalireddy
1 Replies

3. Shell Programming and Scripting

Finding The Number Of Programs That A Given User Running On A TERMINAL

How To Find The Number Of Programs That A User Running ON A GIVEN TERMINAL (4 Replies)
Discussion started by: venkata.ganesh
4 Replies

4. UNIX for Dummies Questions & Answers

single user mode - user accounts passwords

hello ppl, someone must be able to help with this --> I have an old NCR tower 32 with an ADDS terminal running a unix version 020102 (Im not sure if thats correct but its unix for sure). I have no user names and no passwords and need to login to read a tape. Is there any way to do that? I hear... (3 Replies)
Discussion started by: orestis
3 Replies

5. UNIX for Dummies Questions & Answers

Difference between : Locked User Account & Disabled User Accounts in Linux ?

Thanks AVKlinux (3 Replies)
Discussion started by: avklinux
3 Replies

6. UNIX for Dummies Questions & Answers

Number of files accessed this week

I have looked around on the internet and still i am no wiser as to how to show the number of files in a directory that have been accessed this week and also that as a percentage. Any help would be much appreciated. (5 Replies)
Discussion started by: RAFC_99
5 Replies

7. Shell Programming and Scripting

Number of days between the current date and user defined date

I am trying to find out the number of days between the current date and user defined date. I took reference from here for the date2jd() function. Modified the function according to my requirement. But its not working properly. Original code from here is working fine. #!/bin/sh... (1 Reply)
Discussion started by: hiten.r.chauhan
1 Replies

8. Shell Programming and Scripting

getting timestamp of a file and if it was accessed today then finding a line in it

i have my files and the variables value extracted from db is taken as in1=slot0312 in2=best in3=it is :veryliong/fine as varibles.. i have a folder stuctures in my unix machine as : /2011/hand_sl0312/best/HOD/file1.txt /2011/hand_sl0312/happy/HOD/file1.txt... (1 Reply)
Discussion started by: rajniman
1 Replies

9. Shell Programming and Scripting

Finding AIX user accounts expired or locked

// AIX 6.1 In need of finding which AIX user accounts will be expired and are locked. I have placed the following parameters under /etc/security/user... (1 Reply)
Discussion started by: Daniel Gate
1 Replies
SHADOW(5)                                                  File Formats and Conversions                                                  SHADOW(5)

NAME
shadow - shadowed password file DESCRIPTION
shadow is a file which contains the password information for the system's accounts and optional aging information. This file must not be readable by regular users if password security is to be maintained. Each line of this file contains 9 fields, separated by colons (":"), in the following order: login name It must be a valid account name, which exist on the system. encrypted password Refer to crypt(3) for details on how this string is interpreted. If the password field contains some string that is not a valid result of crypt(3), for instance ! or *, the user will not be able to use a unix password to log in (but the user may log in the system by other means). This field may be empty, in which case no passwords are required to authenticate as the specified login name. However, some applications which read the /etc/shadow file may decide not to permit any access at all if the password field is empty. A password field which starts with an exclamation mark means that the password is locked. The remaining characters on the line represent the password field before the password was locked. date of last password change The date of the last password change, expressed as the number of days since Jan 1, 1970. The value 0 has a special meaning, which is that the user should change her password the next time she will log in the system. An empty field means that password aging features are disabled. minimum password age The minimum password age is the number of days the user will have to wait before she will be allowed to change her password again. An empty field and value 0 mean that there are no minimum password age. maximum password age The maximum password age is the number of days after which the user will have to change her password. After this number of days is elapsed, the password may still be valid. The user should be asked to change her password the next time she will log in. An empty field means that there are no maximum password age, no password warning period, and no password inactivity period (see below). If the maximum password age is lower than the minimum password age, the user cannot change her password. password warning period The number of days before a password is going to expire (see the maximum password age above) during which the user should be warned. An empty field and value 0 mean that there are no password warning period. password inactivity period The number of days after a password has expired (see the maximum password age above) during which the password should still be accepted (and the user should update her password during the next login). After expiration of the password and this expiration period is elapsed, no login is possible using the current user's password. The user should contact her administrator. An empty field means that there are no enforcement of an inactivity period. account expiration date The date of expiration of the account, expressed as the number of days since Jan 1, 1970. Note that an account expiration differs from a password expiration. In case of an account expiration, the user shall not be allowed to login. In case of a password expiration, the user is not allowed to login using her password. An empty field means that the account will never expire. The value 0 should not be used as it is interpreted as either an account with no expiration, or as an expiration on Jan 1, 1970. reserved field This field is reserved for future use. FILES
/etc/passwd User account information. /etc/shadow Secure user account information. /etc/shadow- Backup file for /etc/shadow. Note that this file is used by the tools of the shadow toolsuite, but not by all user and password management tools. SEE ALSO
chage(1), login(1), passwd(1), passwd(5), pwck(8), pwconv(8), pwunconv(8), su(1), sulogin(8). shadow-utils 4.5 01/25/2018 SHADOW(5)
All times are GMT -4. The time now is 11:18 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy