Tuesday, February 22, 2011

who logged in last since last two hours

when we have to check who have logged in last few minutes, here say two hours, or 120minutes.

who | awk -v d=`date +"%Y-%m-%d"` 
                 -v h=`date +"%H"` 
                 -v m=`date +"%M"` 
                ' { split($4,wa,":"); 
                     wc=wa[1]*60+wa[2]; 
                     dc=h*60+m; 
                     if ($3==d && dc-wc<=120 )  printf ("%s\n",$0) 
                  }'

you can change that dc-wc<=120 -- this 120 to any minutes to check

dc - is the current system date time (Hours and minutes converted into minutes), 
wc - is the date time from who command for user logged in 



explained with example

$date
Tue Feb 22 09:01:05 UTC 2011

$ who
sanjeev  pts/0        2011-02-22 07:54 (what.is.the.host)
jro      pts/1        2011-02-21 16:43 (what.is.the.host)
beak     pts/2        2011-02-22 00:49 (beak.has.the.host)
madrose  pts/3        2011-02-22 07:59 (what.is.the.host)
onthepk  pts/4        2011-02-22 08:00 (is.this.the.host)
anita    pts/6        2011-02-22 02:35 (what.is.the.host)

$ who|awk -v d=`date +"%Y-%m-%d"` -v h=`date +"%H"` -v m=`date +"%M"` '{ split($4,wa,":"); wc=wa[1]*60+wa[2]; dc=h*60+m; if ($3==d && dc-wc<=120 )  printf ("%s\n",$0) }'

sanjeev  pts/0        2011-02-22 07:54 (what.is.the.host)
madrose  pts/3        2011-02-22 07:59 (what.is.the.host)
onthepk  pts/4        2011-02-22 08:00 (is.this.the.host)

No comments:

Post a Comment