LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-23-2011, 11:32 AM   #1
arashi256
Member
 
Registered: Jan 2008
Location: Brighton, UK
Distribution: Ubuntu 12.04 / CentOS 6.5
Posts: 394

Rep: Reputation: 61
Getting user's previous successful login before this one?


This should be an easy one, but I can't figure it out. I'm simply trying to display a user's last successful login before this one. I know that sshd_config has this, but you cannot dictate where in the login process this displays unless you turn it off and write it yourself and append your code to /etc/profile.
I thought I'd hit the jackpot with the lastlog command so I thought I'd use something like: -

Code:
lastlog | grep -w $USER
and then use awk to get the columns I need. Fine and dandy until I discovered that the lastlog command actually gets the most recent successful login...which is always going to be the current one. Not very useful.

If I login on Tuesday 23rd August 2011 at 15:30 then after the rest of /etc/profile has done it's stuff, lastlog will return...the same timestamp as now. I'm looking for the previous successful login timestamp

Can I use "last" instead? I read through the man pages but couldn't work out the switches.

Little help?
 
Old 08-23-2011, 11:38 AM   #2
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 3,797
Blog Entries: 1

Rep: Reputation: 1341Reputation: 1341Reputation: 1341Reputation: 1341Reputation: 1341Reputation: 1341Reputation: 1341Reputation: 1341Reputation: 1341Reputation: 1341
this will get you close:
Code:
last | sort | grep $USER | tail -n 2
this will get you closer:

Code:
last | sort | grep $USER | tail -n 2 | grep -v "still logged in"
Then do some awking on it and grab the date...

Last edited by szboardstretcher; 08-23-2011 at 11:40 AM.
 
1 members found this post helpful.
Old 08-23-2011, 01:40 PM   #3
arashi256
Member
 
Registered: Jan 2008
Location: Brighton, UK
Distribution: Ubuntu 12.04 / CentOS 6.5
Posts: 394

Original Poster
Rep: Reputation: 61
Thanks, man. Got it now
 
Old 08-23-2011, 02:29 PM   #4
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,066
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Or w/ just awk
Code:
last | awk '$1 ~ /user/ { if($0 !~ /still logged in/){print gensub( /^[^A-Z]+([A-Z].*)$/, "\\1", "1");exit}}'
 
Old 08-24-2011, 06:16 AM   #5
arashi256
Member
 
Registered: Jan 2008
Location: Brighton, UK
Distribution: Ubuntu 12.04 / CentOS 6.5
Posts: 394

Original Poster
Rep: Reputation: 61
Coolio - eventually came up with: -

Code:
last | grep $USER | grep -v "still logged in" | head -1
..and then piped this to awk to get the columns I needed.
 
Old 08-24-2011, 06:20 AM   #6
arashi256
Member
 
Registered: Jan 2008
Location: Brighton, UK
Distribution: Ubuntu 12.04 / CentOS 6.5
Posts: 394

Original Poster
Rep: Reputation: 61
Quote:
Originally Posted by Tinkster View Post
Or w/ just awk
Code:
last | awk '$1 ~ /user/ { if($0 !~ /still logged in/){print gensub( /^[^A-Z]+([A-Z].*)$/, "\\1", "1");exit}}'
That doesn't return anything for me - thanks though
 
Old 08-24-2011, 08:09 AM   #7
cheddarcheese
Member
 
Registered: Aug 2011
Location: Massachusetts, USA
Distribution: Fedora; Centos; Puppy
Posts: 82

Rep: Reputation: 5
Code:
last | awk '$1 ~ /user/ { if($0 !~ /still logged in/){print gensub( /^[^A-Z]+([A-Z].*)$/, "\\1", "1");exit}}'
... seems to work for me if I substitute "user" for my actual username, but it doesn't appear to expand $USER if I substitute that in its place.

Either way, I learned a couple of handy things before breakfast, which is always a good start to the day!
 
Old 08-24-2011, 02:49 PM   #8
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,066
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Quote:
Originally Posted by cheddarcheese View Post
Code:
last | awk '$1 ~ /user/ { if($0 !~ /still logged in/){print gensub( /^[^A-Z]+([A-Z].*)$/, "\\1", "1");exit}}'
... seems to work for me if I substitute "user" for my actual username, but it doesn't appear to expand $USER if I substitute that in its place.

Either way, I learned a couple of handy things before breakfast, which is always a good start to the day!

If you wanted to use $USER that can be done, too.
Code:
last | awk -v user=$USER '$1 ~ user { if($0 !~ /still logged in/){print gensub( /^[^A-Z]+([A-Z].*)$/, "\\1", "1");exit}}'


Cheers,
Tink
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
I want 100 last successful login linuxjamil Linux - Newbie 2 11-11-2008 01:51 AM
Configuring Previous Login Details ryanlum Linux - Server 1 08-06-2007 04:39 AM
samba reports authentication successful but can't login matt1982 Debian 1 08-14-2005 10:08 AM
FTP Fails on Successful Login machineghost Linux - Networking 5 02-27-2005 12:00 PM
Login freezes after successful install. neocookie Slackware - Installation 6 09-23-2004 10:43 AM


All times are GMT -5. The time now is 04:03 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration