Shell "Last login" Date Format

General discussions and other topics.
7 posts Page 1 of 1
by Michael » Mon Apr 09, 2012 1:30 am
Hi,

Is there a way that I can change the Shell "Last login" date format? Currently the format reads:
Last login: Sun Apr 8 23:30:44 2012 from xxx.xxx.xxx.xxx
I'd like to change the format to read:
Last login: 2012-04-08 23:30:44 from xxx.xxx.xxx.xxx
To me, this makes more sense and is easier.


P.S. Why isn't the INDENT BBcode available?
by Michael » Wed Apr 11, 2012 1:58 am
Before most people were driven out of the Sonic groups, I suspect that a question like the one presented here would have already been answered. To me, this indicates that today's Web forums are not a complete replacement.
by kbenson » Wed Apr 11, 2012 10:07 am
From the man page of login(1) it appears you can't through any sort of flag or setting. There are other options though, such as this:

Code: Select all

$ perl -MSys::Lastlog -MPOSIX=strftime -e 'my $ll = Sys::Lastlog->new->getllnam($ENV{USER}); printf("Last login: %s from %s\n", strftime("%F %T",localtime($ll->ll_time)), $ll->ll_host);'
Which outputs this:

Code: Select all

Last login: 2012-04-11 10:01:44 from xxx.yyy.zzz.com
Just throw it in your .profile, or .bash_profile (or equivalent).
by Michael » Wed Apr 11, 2012 12:36 pm
First and foremost, thank you for taking the time to post a reply. I've found two issues using the command as presented. First, when copy and pasted as written into .login, when re-logging back into Shell I see the following error:

Code: Select all

$: Command not found.
I then removed the $ and space before perl. After re-logging back into Shell, the command worked.

Second, I removed the ".hushlogin" file to expose the original "Last login" and apparently the date from the command is the current date, not the "Last login" date and time. When re-logging back into Shell I see the following:

Code: Select all

Last login: Wed Apr 11 11:51:04 2012 from xxx.xxx.xxx.xxx
Last login: 2012-04-11 11:57:33 from xxx.xxx.xxx.xxx

  Sys Time: 2012-04-11 11:57:33 AM
The "Sys Time" (above) is a 'little something' I added afterwards, which is nothing more than:

Code: Select all

echo -n "  Sys Time: " && date "+%F %r"
The only purpose of the 'little something' is to display the current date and time for comparison. Both the command and the 'little something' show the exact same date and time, and do not match the actual "Last login" date and time.

If it matters, I'm using Tcsh shell.

Thank you.
by kbenson » Wed Apr 11, 2012 1:51 pm
Michael wrote:First and foremost, thank you for taking the time to post a reply. I've found two issues using the command as presented. First, when copy and pasted as written into .login, when re-logging back into Shell I see the following error:

Code: Select all

$: Command not found.
I then removed the $ and space before perl. After re-logging back into Shell, the command worked.
Yep, I left that in as a slight clue that it's a shell command, in case there was any ambiguity. I figured a shell user would probably figure it out. :)
Second, I removed the ".hushlogin" file to expose the original "Last login" and apparently the date from the command is the current date, not the "Last login" date and time. When re-logging back into Shell I see the following:
D'oh! On reflection, this makes perfect sense. Since this runs after the actual login program, the last login is the current login.

If you drop this into a file and make it executable, it should work (it's modified code (found online):

Code: Select all

#!/usr/bin/env perl

use strict;
use warnings;
use POSIX qw(strftime);

my @logins;
open(my $wtmp, '<', '/var/log/wtmp') or die($!);
my $recs = do { local $/; <$wtmp> };
close($wtmp);
for (split(/(.{384})/s,$recs)) {
    next unless length($_);
    my ($type,$pid,$line,$inittab,$user,$host,$t1,$t2,$t3,$t4,$t5) = $_ =~/(.{4})(.{4})(.{32})(.{4})(.{32})(.{256})(.{4})(.{4})(.{4})(.{4})(.{4})/s;
    if (defined $line && $line =~ /\w/) {
        $user =~ s/\x00+//g;
        next unless $user eq $ENV{USER};
        $line =~ s/\x00+//g;
        $host =~ s/\x00+//g;
        my $epoch = unpack("I4",$t3);
        push(@logins, sprintf("Last login: %s from %s\n",strftime("%F %T",localtime($epoch)),$host));
    }
}
print $logins[-2] if @logins > 1;
Feel free to tweak it to your needs, if you feel confident enough.
by Michael » Wed Apr 11, 2012 7:45 pm
Tried the new version you posted, but Shell choked on "use". Tried tweaking it here and there without any success. I had the confidence, but not the skill. :) So, I came up with something a little different. I created "lastlogon" in $HOME/bin, made it executable and added the following:

Code: Select all

######################################################
# Lastlogin By Michael - v0.5 - 2012-04-11 (Wednesday)
#           
#!/bin/sh -f
echo -n "Last Login: " && cat $HOME/.lastlogin
setenv CDT `date "+%F %r"`
setenv HIP `who -m | awk -F"[()]" '{print $2}'`
echo -n $CDT > $HOME/.lastlogin
echo -n " from " >> $HOME/.lastlogin
echo -n $HIP >> $HOME/.lastlogin
echo " "
After saving, I put the ".hushlogin" file back in $HOME and then added the following to my .login file:

Code: Select all

$HOME/bin/lastlogon
echo -n "  Sys Time: " && date "+%F %r"
echo " "
Logged in and out to create the context in the file and now when I log into Shell I receive the following:

Code: Select all

Last Login: 2012-04-11 06:22:20 PM from xxx.xxx.xxx.xxx
  Sys Time: 2012-04-11 06:22:36 PM

...logged out and back in...

Last Login: 2012-04-11 06:22:36 PM from xxx.xxx.xxx.xxx
  Sys Time: 2012-04-11 06:39:41 PM

...logged out and back in...

Last Login: 2012-04-11 06:39:41 PM from xxx.xxx.xxx.xxx 
  Sys Time: 2012-04-11 06:54:36 PM
Notice that the first "Sys Time" matches the second "Last Login", and so on. It's not the cleanest solution. It's accurate to within one second, which is the best that I could do. Even so, I think it's much better than the default.

Thanks for all of your help.
by kbenson » Thu Apr 12, 2012 9:12 am
Glad you found something that worked!
7 posts Page 1 of 1

Who is online

In total there are 41 users online :: 1 registered, 0 hidden and 40 guests (based on users active over the past 5 minutes)
Most users ever online was 999 on Mon May 10, 2021 1:02 am

Users browsing this forum: Bing [Bot] and 40 guests