Script to find users not logged in for 90 days


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script to find users not logged in for 90 days
# 1  
Old 04-14-2015
Script to find users not logged in for 90 days

Dear All,

I need your help in finding out users not logged in to linux system for more than 90 days. I found a script from our forum i am getting error while using that.

from the code i have debugged line by line to see where i am getting the problem. i found out the below line i am getting error. I have provided the error details below.
Code:
llogin=$( date +%s -d"$umnt $uday, $year $uhour:$umin:01" )

Code:
Month: Apr  day:9 hour:07 min:45
Date:invalid date `Apr 9,2015 07:47:01`
User login.sh: line 37: 1428580770- : syntax error: operand expected (error token is "_ ")

Pleae help me and let me know If there is any other find the users notlogged in for more that 90days . FYI: I have around 250 users. for some users last command returns null.

I have attached script in which i did slight modification

thanks a lot in advance.

Last edited by Don Cragun; 04-14-2015 at 04:04 AM.. Reason: Change Bold & Quote tags to CODE tags.
# 2  
Old 04-14-2015
Does date +%s -d"Apr 9, 2015 07:47:01" work if you run it from the command line?
# 3  
Old 04-14-2015
Hi Carlo,


No its not working getting same error like invalid date..
# 4  
Old 04-14-2015
Works for me on Ubuntu linux3.16.0. What's your system and locale?

---------- Post updated at 22:36 ---------- Previous update was at 21:22 ----------

Given you are using awk anyhow, and several times, and hoping your date will eventually work, you might want to consider replacing your script with (an extended version of) this:
Code:
awk -F: '
BEGIN   {NOW=srand()}           

        {("last -1 " $1) | getline PW
         split (PW, USR, " ")
         if (USR[1]==$1)        {printf "%s %s, ",  $1, substr (PW, 40, 13)
                                 ("date +%s -d\""substr(PW, 40, 13)"\"") | getline LOG
                                 printf "%s\n", " logged in " int((NOW-LOG)/86400) " days ago."
                                }
         else print $1 " never logged in so far."
        }
        ' /etc/passwd


Last edited by RudiC; 04-16-2015 at 05:18 AM.. Reason: Typo correction
# 5  
Old 04-14-2015
try this Smilie

for example notlogins for 90 days
Code:
# ./loginusers.sh notlogins 90
                     notlogins
              --------------------------
Last Login information for 'sync' could [not found] for 90 days
Last Login information for 'shutdown' could [not found] for 90 days
Last Login information for 'halt' could [not found] for 90 days
Last Login information for 'news' could [not found] for 90 days
Last Login information for 'ksh' could [not found] for 90 days
Last Login information for 'csh' could [not found] for 90 days
Last Login information for 'sftpuser' could [not found] for 90 days
Last Login information for 'sil' could [not found] for 90 days
Last Login information for 'vct' could [not found] for 90 days
Last Login information for 'ftptest' could [not found] for 90 days
Last Login information for 'ksh2' could [not found] for 90 days
Last Login information for '2' could [not found] for 90 days
Last Login time is long from period ['90'] of days ago for [3746ygem] user
Last Login information for 't2' could [not found] for 90 days
Last Login information for 'mysql' could [not found] for 90 days
.......................
............

for example only "test" user for 30 days
Code:
# ./loginusers.sh test 30

                test
=======================================================
Last Login Date ->  Tue Apr 14 15:19:01 EEST 2015
Elapsed Times (Days): 0.398252
Elapsed Times (Hours): 9.55806
Elapsed Times (Minutes): 573.483
Elapsed Times (Seconds): 34409
=======================================================

for example "3746ygem" user has a last login but not for in the 30 days
Code:
# ./loginusers.sh 3746ygem 30

Last Login time is long from period ['30'] of days ago for [3746ygem] user

# ./loginusers.sh 3746ygem 300

            3746ygem
=======================================================
Last Login Date ->  Thu Sep  4 15:38:01 EEST 2014
Elapsed Times (Days): 222.386
Elapsed Times (Hours): 5337.25
Elapsed Times (Minutes): 320235
Elapsed Times (Seconds): 19214113
=======================================================

for example only successfull last logins user for one day
Code:
# ./loginusers.sh logins 1
                        logins
              --------------------------

                root
=======================================================
Last Login Date ->  Tue Apr 14 22:28:01 EEST 2015
Elapsed Times (Days): 0.101528
Elapsed Times (Hours): 2.43667
Elapsed Times (Minutes): 146.2
Elapsed Times (Seconds): 8772
=======================================================

Last Login time is long from period ['1'] of days ago for [3746ygem] user

                test
=======================================================
Last Login Date ->  Tue Apr 14 15:19:01 EEST 2015
Elapsed Times (Days): 0.399444
Elapsed Times (Hours): 9.58667
Elapsed Times (Minutes): 575.2
Elapsed Times (Seconds): 34512
=======================================================

Last Login time is long from period ['1'] of days ago for [ucmdb] user


for example all last logs (logins and notlogins) from all users for last 5 day
Code:
# ./loginusers.sh 5

                root
=======================================================
Last Login Date ->  Wed Apr 15 09:48:01 EEST 2015
Elapsed Times (Days): 0.000405093
Elapsed Times (Hours): 0.00972222
Elapsed Times (Minutes): 0.583333
Elapsed Times (Seconds): 35
=======================================================

Last Login information for 'sync' could [not found] for 5 days
Last Login information for 'shutdown' could [not found] for 5 days
Last Login information for 'halt' could [not found] for 5 days
Last Login information for 'news' could [not found] for 5 days
Last Login information for 'ksh' could [not found] for 5 days
Last Login information for 'csh' could [not found] for 5 days
Last Login information for 'sftpuser' could [not found] for 5 days
Last Login information for 'sil' could [not found] for 5 days
Last Login information for 'vct' could [not found] for 5 days
Last Login information for 'ftptest' could [not found] for 5 days
Last Login information for 'ksh2' could [not found] for 5 days
Last Login information for '2' could [not found] for 5 days
Last Login time is long from period ['5'] of days ago for [3746ygem] user

                test
=======================================================
Last Login Date ->  Tue Apr 14 15:19:01 EEST 2015
Elapsed Times (Days): 0.770544
Elapsed Times (Hours): 18.4931
Elapsed Times (Minutes): 1109.58
Elapsed Times (Seconds): 66575
=======================================================

Last Login information for 't2' could [not found] for 5 days





Code:
# cat loginusers.sh
#!/bin/bash
## @ygemici unix.com last logins check script

# # # # # # # # # # # # # # # # #
# ./loginusers.sh notlogins 90  #
# ./loginusers.sh logins 90     #
# ./loginusers.sh username 90   #
# ./loginusers.sh 90            #
# # # # # # # # # # # # # # # # #

calldates() {
daysago=$(date -d "-$day days" +'%Y%m%d%H%M%S')
#lastyearstart=$(last|awk 'END{print $NF}')
validyear=$(date -d "-$day days" +'%Y')
}

writeerror() {
echo "Last Login information for '$1' could [not found] for $day days"
}

writelogin() {
nowepochtime=$(date +%s)
diff=$((nowepochtime-lastlgepochtime))
awk -vuser=$user 'BEGIN{printf "\n%20s\n",user}' ; awk 'BEGIN{$55=OFS="=";printf "%s\n",$0}'
awk -vdatex="$(date -d @${lastlgepochtime})" 'BEGIN{printf "%s%30s\n","Last Login Date -> ", datex}'
awk -vdiff=$diff 'BEGIN{printf "%20s%s\n%20s%s\n%20s%s\n%20s%s\n","Elapsed Times (Days): ",diff/60/60/24,"Elapsed Times (Hours): ",diff/60/60,"Elapsed Times
(Minutes): ",diff/60,"Elapsed Times (Seconds): ",diff}'
awk 'BEGIN{$55=OFS="=";print $0"\n"}'
}

calloldwtmp() {
lastlogf=$(ls -lt /var/log/wtmp*|awk 'END{print $NF}')
lasttyear=$(last -f $lastlogf|awk 'END{print $NF}')
}

calllastlogin() {
#lmonth=$(awk -vmonth=$lastmonthstart 'BEGIN{split("Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec",month," ");for(i=1;i<=12;i++)if(month[i]~month)print i}'
)
calloldwtmp

for((i=$lasttyear;i<$validyear;i++)) ; do
lastlogl=$(last -t ${i}1231235959 -1 $user -f /var/log/wtmp*|awk 'NR==1')
j=$((i+1)) ; lastlogf=$(last -t ${j}1231235959 -1 $user -f /var/log/wtmp*|awk 'NR==1')

if [ "$lastlogl" == "$lastlogf" ] ; then
validyearnew=$i
else
validyearnew=$validyear
fi
done
lastlogin=$(last -1 $user -f /var/log/wtmp*|awk -vy=$validyearnew 'NR==1&&NF{print $5,$6,",",y,$7":01"}')
if [ -z "$lastlogin" ] ; then
 if [ "$1" = "notlogins" ] || [ "$1" = "$user" ] || [ -z "$1" ] ; then
 writeerror $user
 fi
else
 lastlgepochtime=$(date +%s -d"$lastlogin")
 wantedepochtime=$(date +%s -d"-$day days")
 if [ $lastlgepochtime -lt $wantedepochtime ] ; then
 echo "Last Login time is long from period ['$day'] of days ago for [$user] user"
 else
 if [ "$1" = "logins" ] || [ "$1" = "$user" ] || [ -z "$1" ] ; then
 writelogin $user
 fi
 fi
fi
}

calllastloginall() {
awk -F':' '$NF!~/nologin/{print $1}' $passwd | while read -r user ; do
if [ -z "$1" ] ; then
calllastlogin
else
calllastlogin $1
fi
done
}

checkuser() {
if [ "$1" != "notlogins" ] && [ "$1" != "logins" ] ; then
grep $1 $passwd 2>&1 >/dev/null
if [ $? -ne 0 ] ; then
echo "Username is seem invalid!!" ; exit 1
fi
else
awk -vn=$1 'BEGIN{printf "%30s\n",n}'
echo|awk 'BEGIN{printf "%15c","-";for(i=1;i<=25;i++)printf "%c","-"}END{printf "%s","\n"}'
fi
}

callfunc() {
case $1 in
 notlogins) calllastloginall notlogins ;;
 logins)  calllastloginall logins ;;
 *) user=$1 ; calllastlogin $user ;;
esac
}

numcheckandset() {
numcheck=$(echo|awk -va=$1 '{print a+=0}')
if [ $numcheck -eq 0 ] ; then echo "where is the period of day ? " ; exit 1 ; fi
passwd='/etc/passwd'
calldates
}

case $# in
 2) day=$2 ; numcheckandset $day ; checkuser $1 ; callfunc $1 ;;
 1) day=$1 ; numcheckandset $day ; calllastloginall ;;
 *) exit 1 ;;
esac

regards,
ygemici

Last edited by ygemici; 04-15-2015 at 03:53 AM.. Reason: add some examples
# 6  
Old 04-16-2015
thanks lot every one..
@ygemici thank you very much. I will try the code.

mean time that script is working when i changed the date format..like below.
Code:
llogin=$( date +%s -d"$umnt $uday $uhour:$umin:01 , $year" )

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Users not logged in for last 90 days

Hi, How to find the users who did not login into a UNIX box (thru ssh/ftp or any other way) for last 90 days? I think of using "finger" or "last" command to findout each user's last login and then find number of days between today and that day. Is there any other better way or anyone prepared... (1 Reply)
Discussion started by: reddyr
1 Replies

2. UNIX for Dummies Questions & Answers

how to find top 3 users currently logged on

For the first 3 users only that are currently logged in output their effective user id. thank you. (6 Replies)
Discussion started by: whyatepies
6 Replies

3. Shell Programming and Scripting

find top 4 users currently logged on can i use grep

For the first 4 users only that are currently logged in output their effective user id. It's not important the order in which each logged in i just want to have the top 4. Same question as here...... (0 Replies)
Discussion started by: whyatepies
0 Replies

4. Shell Programming and Scripting

number of users logged in script

My admin needs a shell script in Korn that will show conditions based on users logged in. I have never used the Korn shell and have no clue what I am doing, can anyone help. here are the conditions that need to be returned. if users are below 5 displays should be: performance is high if... (1 Reply)
Discussion started by: vthokiefan
1 Replies

5. Shell Programming and Scripting

Script to identify logged users & commands executed

Hi All, I am trying to write a script to get the user information & the command executed. I tried something like this : w | sort | awk '{print$5$6$7}' My requirement is to identify the users who execute the same command at same time. I need the user name & the... (2 Replies)
Discussion started by: vijayarajvp
2 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. Shell Programming and Scripting

to find the number of users logged in

Hi, can u say to display the number of users that logged before me. thanks (10 Replies)
Discussion started by: shanshine
10 Replies
Login or Register to Ask a Question