Sponsored Content
Full Discussion: Last time logged in
Top Forums Shell Programming and Scripting Last time logged in Post 302115662 by ughosting on Thursday 26th of April 2007 08:33:48 AM
Old 04-26-2007
Many thanks so far!!

Thanks for the suggestions!

Meanwhile I have come up with this!

It's not fast, but it does the job!

#!/usr/bin/ksh

#####
#
# Set up Global Variables
#
#####

MIN_DAYS=2
MAX_DAYS=90
DEF_GROUP="cpx"
DATECALC="<patch_to_datecalc>"

#####
#
# Preliminary Tests
#
#####

#
# Test for the correct parameters are passed or report Usage
#

case $# in

1)

NUM_DAYS=$1
SEARCH_GROUP=$DEF_GROUP
;;

2)

NUM_DAYS=$1
SEARCH_GROUP=$2
;;

*)

echo "Usage: $(basename $0) <num_days> [ <group> ]"
exit 1
;;

esac

#
# Test that the number of days passed is in range (and therefore a number)
#

if [ ${NUM_DAYS} -lt ${MIN_DAYS} ] || [ ${NUM_DAYS} -gt ${MAX_DAYS} ]
then

echo "<num_days> (${NUM_DAYS}) should be > ${MIN_DAYS} && < ${MAX_DAYS}"
exit 1

fi

#
# Test that the group name passed exists
#

grep -q "^${SEARCH_GROUP}" /etc/group

if [ $? = 1 ]
then

echo "<group> (${SEARCH_GROUP}) not found!"
exit 1

fi

#####
#
# Declare Local Functions and populate arrays
#
#####

dateformat () {

DATE_MONTH=$(echo "Jan\nFeb\nMar\nApr\nMay\nJun\nJul\nAug\nSep\nOct\nNov\nDec" | grep -n $2 | awk -F':' '{print$1}')

print "$5 ${DATE_MONTH} $3"
return 0
}

#####
#
# Start the program!!!
#
#####

SEARCH_NUM=$(grep "^${SEARCH_GROUP}" /etc/group | awk -F':' '{print$3}')

TODAY=$(dateformat $(date | awk '{print $1" "$2" "$3" "$4" "$6}'))

for i in $(grep "${SEARCH_NUM}" /etc/passwd | awk -F':' '{print$1}')
do

#
# Make sure that this user has logged in before trying to process them
#

grep -p "^${i}" /etc/security/lastlog | grep -q time_last_login

if [ $? = 0 ]
then

LOGGED_DATE=$(perl -le 'print scalar localtime shift' $(grep -p "^${i}" /etc/security/lastlog | grep time_last_login | awk '{print$3}'))
LOGGED_IN=$(dateformat ${LOGGED_DATE})

DIFF_DAYS=$(${DATECALC} -a ${TODAY} - ${LOGGED_IN})

if [ ${DIFF_DAYS} -ge ${NUM_DAYS} ]
then

typeset -L9 USER="${i}:"
typeset -R4 NUMBER=${DIFF_DAYS}
echo "${USER} ${NUMBER} days (${LOGGED_DATE})"

fi

fi

done


Of course, my AIX coding is a bit rubbish, so if anyone can see how to speed this up I would be interested.

I have used the datecalc script from this forum to perform the one date take away the other!!
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Finding last time users logged in

I would like to find out the last time all users have logged in or out. I tried the last command, but it could not find the wtmp file in /var/adm (I searched in othe directories also). This is an AIX rs6000 4.2.1 system. We are moving our applications from this system to an AIX 5.2 system and I... (11 Replies)
Discussion started by: jyoung
11 Replies

2. Shell Programming and Scripting

How can one know how much time user logged?

Hello, i know who command gives you the time when particular user logged in. And subtracting today's date and time from the one found in who we can get how much time user logged in. But this can get very much clumsy as we can't subtract date directly in unix . Is there any other way or command... (4 Replies)
Discussion started by: salman4u
4 Replies

3. UNIX for Dummies Questions & Answers

How many time have user logged last X days?

Hi all.. I was trying to do a little shell script, that would list users and their login times, lets say like last 5 days. But I couldnt figure out how to count users login times from previous days. Any tips? Funny that nobody has do this kinda script before, or atleast I couldnt find on... (2 Replies)
Discussion started by: Kimmo_
2 Replies

4. 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

5. UNIX for Dummies Questions & Answers

Finding users logged on time and space useage

I need to find what users are currently logged onto the system that is easy just a simple who | awk '{ print $1 }' (thats all I need for the part), but I also need to find how long they have been logged on and the total amount of file space they are using. Thanks in advance, I have been looking... (3 Replies)
Discussion started by: mauler123
3 Replies

6. UNIX Desktop Questions & Answers

Shell script to display user logged in within given time

how can i know which users have logged in at specified given start and end time in 24 hour format? (3 Replies)
Discussion started by: meherzad4u
3 Replies

7. Shell Programming and Scripting

Shell script to display user logged last week and time connected

Hello all, I want to display all the user logged last week and know the time tnat they are connected. I have been looking for a solution in the Forum, but I didn't find. Thanks in advance (4 Replies)
Discussion started by: ahernandez
4 Replies

8. UNIX for Dummies Questions & Answers

Script to determine logged in time

Hi all So I am thinking my inability to cope with math is bogging me down here so Im asking for help. I want to determine how long a user has been logged on for by using the date and who commands to determine the time they have been logge don. My problem is that I keep getting the wrong... (2 Replies)
Discussion started by: losingit
2 Replies

9. Homework & Coursework Questions

Sort current logged in users by log in time (supposedly to be very easy but I'm missing something)

1. The problem statement, all variables and given/known data: Show all users who are currently logged in, sorted from earliest to latest log in time. The log in time includes the month, day, and time. 2. Relevant commands, code, scripts, algorithms: finger, who, sort, pipe, head, tail, ... (8 Replies)
Discussion started by: vtmd
8 Replies

10. Shell Programming and Scripting

• Write a shell script that upon invocation shows the time and date and lists all the logged-in user

help me (1 Reply)
Discussion started by: sonu pandey
1 Replies
ZGREP(1)                                                      General Commands Manual                                                     ZGREP(1)

NAME
zgrep - search possibly compressed files for a regular expression SYNOPSIS
zgrep [ grep_options ] [ -e ] pattern filename... DESCRIPTION
Zgrep invokes grep on compressed or gzipped files. These grep options will cause zgrep to terminate with an error code: (-[drRzZ]|--di*|--exc*|--inc*|--rec*|--nu*). All other options specified are passed directly to grep. If no file is specified, then the standard input is decompressed if necessary and fed to grep. Otherwise the given files are uncompressed if necessary and fed to grep. If the GREP environment variable is set, zgrep uses it as the grep program to be invoked. EXIT CODE
2 - An option that is not supported was specified. AUTHOR
Charles Levert (charles@comm.polymtl.ca) SEE ALSO
grep(1), gzexe(1), gzip(1), zdiff(1), zforce(1), zmore(1), znew(1) ZGREP(1)
All times are GMT -4. The time now is 03:27 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy