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