Link to home
Start Free TrialLog in
Avatar of R4JK
R4JK

asked on

solaris "last" command does not give year information in output

Hi EE,

I need to get last login dates of each user on a system in the format dd/mm/yy

however when I run "last" to extract this data I get  for eg...

[server:root]#last USERA | head -1
USERA    pts/6        server_name Wed Oct 28 11:24 - 12:06  (00:42)

have two problems to consider here..
1. the format is not what I want here (dd/mm/yy)
2. the year informatino seems to be missing from the output

could anyone advise.

thanks
R4JK
Avatar of woolmilkporc
woolmilkporc
Flag of Germany image

Hi,
there is a perl script (a one-liner) which could help you formatting the lastlog file closer to your needs.
http://www.hcidata.info/lastlog.htm
Some modifications may be necessary, but it's rather clearly explained on the page I pointed you to.
Good luck!
wmp
#!/usr/bin/perl
#this finds the most recent year for which the day matches the date
use POSIX;
@d{qw(Sun Mon Tue Wed Thu Fri Sat)}=(0..6);
@m{qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec)}=(0..11);
$r=qr/\b(@{[join'|',keys %d]})\s+(@{[join'|',keys %m]})\s+(\d+)/;
while( <> ){
    s#$r#
      my $w=$d{$1};
      my $m=$m{$2};
      my $d=$3;
      $y=(localtime)[5];
      --$y while (localtime mktime(0,0,12,$d,$m,$y))[6] != $w;
      strftime("%d/%m/%y",0,0,12,$d,$m,$y)
    #e;
    print;
}
Avatar of turnbulld
turnbulld

If it is the current year, Solaris doesn't show it in dates by default.  Same is true for day typically.  If the date it is displaying is the same as the current date, typically only the time is shown.
ASKER CERTIFIED SOLUTION
Avatar of yuzh
yuzh

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
#!/usr/bin/perl
#this finds the most recent year for which the day matches the date
use POSIX;
@d{qw(Sun Mon Tue Wed Thu Fri Sat)}=(0..6);
@m{qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec)}=(0..11);
$r=qr/\b(@{[join'|',keys %d]})\s+(@{[join'|',keys %m]})\s+(\d+)/;
while( <> ){
    s#$r#
      my $w=$d{$1};
      my $m=$m{$2};
      my $d=$3;
      $y=(localtime)[5];
      --$y while (localtime mktime(0,0,12,$d,$m,$y))[6] != $w;
      strftime("%d/%m/%y",0,0,12,$d,$m,$y)
    #e;
    print;
}
first perform the #uptime if ur server started above 1yr you will get the year info otherwise it will give you the month and date information only.