Solved

How do I get the year to display using the lat command?

Posted on 2006-06-18
9
551 Views
Last Modified: 2013-12-27
Using the followng command I can see when all the system reboots occurred but the date field does not include the year.

last | grep reboot

reboot    system boot                   Sun Jun 18 08:23
reboot    system boot                   Tue May 23 19:55
reboot    system boot                   Tue Apr 18 11:15
reboot    system boot                   Wed Jul 13 09:29
reboot    system boot                   Tue Jul 12 10:07
reboot    system boot                   Tue Jun 28 09:46
reboot    system boot                   Tue Jun 14 09:08

Does anyone know how I can get the year to display. If not the date and times show the most recent at the top. Does anyone have a script that will look at the month and time and put in the year based on seeing something like Jun occurring again after Jul when viewing the output  or is there a file or a command that will provide me with information on reboots that include year.

Thanks for your help
0
Comment
Question by:bradd138
  • 4
  • 3
9 Comments
 

Author Comment

by:bradd138
ID: 16929498
Sorry for the typo in the title.  Using the last command not lat.
0
 
LVL 38

Expert Comment

by:yuzh
ID: 16931339
last command on Solaris systems doesn't report the year included in its records,

please have a look at the following doc for workarounds:
http://www.itworld.com/nl/unix_insider/03312005/pf_index.html

other tools:
http://www.boran.com/security/sp/solaris/
0
 

Author Comment

by:bradd138
ID: 16943417
Thanks for the help yuzh but I couldn't seen to get the unpack commands to work correctly. Had had trouble with the stucture part recommend in the
http://www.oreilly.com/catalog/perlsysadm/chapter/ch09.html . I brought back information the field size weren't correct so it was hard to work with.

I put together the below pearl script that although it is not very elegant since I rarely code in pearl it appears to work.

#!/usr/bin/perl
# Bradd Baldwin
# Program to add year to last reboot

system("last | grep reboot | awk '{print \$1,\$2,\$3,\$4,\$5,\$6,\$7}' > last.txt");
open(MYFILE, "last.txt") || die "opening last.txt: $!";
@LAST_REBOOT=<MYFILE>;
close(MYFILE);
$month=0;
$year="2006";

$current_month=`date +"%m"`;#+%m-%d-%y`;
chomp($current_month);
print "$current_month test\n\n";

foreach $LAST_REBOOT (@LAST_REBOOT) {
        @fields=split(/ /, $LAST_REBOOT);
                $check_month=join(' ',@fields[4]);

        if ($check_month eq "Jan") {
                $month=1;
                if ($current_month < $month) {
                        $year=$year-1 ;
                        }
        }
        $bradd=test;
        if ($check_month eq "Feb") {
                $month=2;
                if ($current_month < $month) {
                        $year=$year-1 ;
                        }
        }
        if ($check_month eq "Mar") {
                $month=3;
                if ($current_month < $month) {
                        $year=$year-1 ;
                        }
        }
        if ($check_month eq "Apr") {
                $month=4;
                if ($current_month < $month) {
                        $year=$year-1 ;
                        }
        }
        if ($check_month eq "May") {
                $month=5;
                if ($current_month < $month) {
                        $year=$year-1 ;
                        }
        }
        if ($check_month eq "Jun") {
                $month=6;
                if ($current_month < $month) {
                        $year=$year-1 ;
                        }
        }
        if ($check_month eq "Jul") {
                $month=7;
                if ($current_month < $month) {
                        $year=$year-1 ;
                        }
        }
        if ($check_month eq "Aug") {
                $month=8;
                if ($current_month < $month) {
                        $year=$year-1 ;
                        }
        }
        if ($check_month eq "Sep") {
                $month=9;
                if ($current_month < $month) {
                        $year=$year-1 ;
                        }
        }
        if ($check_month eq "Oct") {
                $month=10;
                if ($current_month < $month) {
                        $year=$year-1 ;
                        }
        }
        if ($check_month eq "Nov") {
                $month=11;
                if ($current_month < $month) {
                        $year=$year-1 ;
                                }
        }
        if ($check_month eq "Dec") {
                $month=12;
                if ($current_month < $month) {
                        $year=$year-1 ;
                                }
        }
        if ($month eq $month) {
                $DISPLAY_INFO=join(' ',@fields[0,1,2,3,4,5,6]);
        chomp($DISPLAY_INFO);
        print "$DISPLAY_INFO $year\n";
        }
        $current_month=$month;
}
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 38

Expert Comment

by:yuzh
ID: 16948704
>but I couldn't seen to get the unpack commands to work correctly.
I'm not a perl guru, but I think the script in:
http://www.oreilly.com/catalog/perlsysadm/chapter/ch09.html 

and you can modify it as in:
http://www.itworld.com/nl/unix_insider/03312005/pf_index.html

Anyway, you can use your own version of the script if you are happy with it.
you can modify:
   grep reboot
to:
   grep $1
   so that your can get the infor for a login name or reboot as well.


0
 

Author Comment

by:bradd138
ID: 16954207
The perl script in Oreilly bring back the info directly form last which does not incule the year. My perl script has a problem if the first reboot comes in a year prior to the one we are currently in. This is also a problem for finding out years for when a person last logged in.  I would prefer to use the unpack script but it didn't work because the format of my wtmpx file does not match the struct utmp they provide. The code works it just brings back parts of the fields and not the ones I am looking for.  

From Oreilly page
"As we've shown, the format of the wtmp file differs from Unix variant to Unix variant. On top of this, a vendor can change the format of wtmp between OS releases, rendering your perfectly good unpack( ) template invalid. "

does anyone know the format of the wtmpx file on Solaris starting with version 5.6 and going to 5.9
 
0
 
LVL 38

Accepted Solution

by:
yuzh earned 350 total points
ID: 16956519
>>does anyone know the format of the wtmpx file on Solaris starting with version 5.6 and going to 5.9

Please have a look at   /usr/include/utmpx.h for wtmp format , also have
a look at this doc:
http://www.sdsc.edu/~victor/acct.html
0
 

Author Comment

by:bradd138
ID: 16969273
Thanks for all your help yuzh I finally found the right unpack format and this works.

#!/usr/bin/perl
$template = "A32 A4 A32 l s s2 x2 l2 l x20 s A257 x";
# determine the size of a record
$recordsize = length(pack($template,(  )));
$recordsize = length(pack($template,(  )));
# open the file
open(WTMP,"/var/adm/wtmpx") or die "Unable to open wtmpx:$!\n";

# read through it one record at a time
while (read(WTMP,$record,$recordsize)) {
    ($ut_user,$ut_id,$ut_line,$ut_pid,$ut_type,$ut_e_termination,
     $ut_e_exit,$tv_sec,$tv_usec,$ut_session,$ut_syslen,$ut_host)=
       unpack($template,$record);

    if ($ut_line eq "system boot"){
        print "rebooted ".scalar localtime($tv_sec)."\n";
        $reboots++;
    }
}

close(WTMP);
print "Total reboots: $reboots\n";

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
AIX  NFS  question 1 122
Ubuntu install gnome-session-fallback is failing 4 101
need to remove or skip  grub protection password in ubuntu on boot time 2 64
UNIX SCP 5 70
In tuning file systems on the Solaris Operating System, changing some parameters of a file system usually destroys the data on it. For instance, changing the cache segment block size in the volume of a T3 requires that you delete the existing volu…
Why Shell Scripting? Shell scripting is a powerful method of accessing UNIX systems and it is very flexible. Shell scripts are required when we want to execute a sequence of commands in Unix flavored operating systems. “Shell” is the command line i…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now