details on "last" logins

the last command keeps track of the last logins into a system (user, from where, what time etc).  The info (as specified in the man page) is stored in /var/adm/wtmpx.  From what people have told me, that file is a sparse file.  My problem is that the info obtained from typing last is formatted to fit the screen.  In doing so, the location from which the user connected is truncated to ~16 characters.

Is there a way to get last to display that to the full length?  if not, is there somewhere where I can get a program to analyze the wtmpx file with?
cokeman_Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

seedyCommented:
you can write a 'C' program to access these details directly as
explained in the following code.
(Note: this code is for SunOS 5.x, but should be able to use on
other platforms too.)  
This code uses 'getutxent' call to get the utmp entries.  On
other machines this fucntion could be 'getutent'.


#include <stdio.h>
#include <utmpx.h>
 
main()
{
        struct utmpx *wt;
        int i = 0;
 
 
        utmpxname(WTMPX_FILE);  /* Change database to /var/adm/wtmpx */
 
        while ( (wt = getutxent()) != NULL ) {
                printf("%s\t",  wt->ut_user);   /*User Name */
                printf("%s\n", wt->ut_host);
        }    

}

Please note these issues:
1. There are lot more fields in the 'utmpx' structure that are
   not processed here.
2. getutxent function gets a entry from the begining of the
   'utmpx/wtmpx' file.  Where as 'last' command displays info
   from the bottom of the file to top.
3. Your platform may support only 16 characters to store the
   remote host name.  In that case even writing a 'C' as above
   will not help.  Verify this by man utmp and check how the
   field ut_host is defined in the utmp structure.

Hope this helps.  BTW, what is your platform?  
Cheers,
-Seedy-

0
seedyCommented:
One more issue:  You would want to skip a lot of entries
based on the 'ut_type' field - just as 'last' does.
Cheers,
-Seedy-
0
cokeman_Author Commented:
I'm using SunOS 5.5.1, and I've included some of the output from the man page of utmp below:

     #define  UTMP_FILE                  "/var/adm/utmp"
     #define  WTMP_FILE                  "/var/adm/wtmp"
     #define  ut_name                    ut_user

     struct utmp {
              char  ut_user[8];          /* user login name */
              char  ut_id[4];            /* /sbin/inittab id (created by */
                                         /* process that puts entry in utmp) */
              char  ut_line[12];         /* device name (console, lnxx) */
              short ut_pid;              /* process id */
              short ut_type;             /* type of entry */
              struct exit_status {
                   short e_termination;  /* process termination status */
                   short e_exit;         /* process exit status */
              } ut_exit;                 /* exit status of a process
                                         /* marked as DEAD_PROCESS */
              time_t ut_time;            /* time entry was made */
     };
     /*  Definitions for ut_type  */
     #define  EMPTY          0
     #define  RUN_LVL        1
     #define  BOOT_TIME      2
     #define  OLD_TIME       3
     #define  NEW_TIME       4
     #define  INIT_PROCESS   5         /* process spawned by "init" */
     #define  LOGIN_PROCESS  6         /* a "getty" process waiting for login */
     #define  USER_PROCESS   7         /* a user process */
     #define  DEAD_PROCESS   8
     #define  ACCOUNTING     9
     #define  UTMAXTYPE      ACCOUNTING  /* max legal value of ut_type */
     /*  Below are special strings or formats used in the "ut_line"  */
     /*  field when  accounting for something other than a process.  */
     /*  No string for the ut_line field can be more than 11 chars + */
     /*  a null character in length.  */
     #define  RUNLVL_MSG     "run-level %c"
     #define  BOOT_MSG       "system boot"
     #define  OTIME_MSG      "old time"
     #define  NTIME_MSG      "new time"

0
OWASP: Forgery and Phishing

Learn the techniques to avoid forgery and phishing attacks and the types of attacks an application or network may face.

cokeman_Author Commented:
ooops, sorry.. I guess I should have looked up the man page for utmpx instead of utmp.. I've added that below:

     utmpx is an extended version of utmp(4).

     utmpx and wtmpx hold user  and  accounting  information  for
     commands  such  as  who, write, and login.  These files have
     the following structure as defined by <utmpx.h>:

     #define  UTMPX_FILE                    "/var/adm/utmpx"
     #define  WTMPX_FILE                    "/var/adm/wtmpx"
     #define  ut_name                       ut_user
     #define  ut_xtime                      ut_tv.tv_sec
     struct utmpx  {
        char    ut_user[32];                /* user login name */
        char    ut_id[4];                   /* inittab id */
        char    ut_line[32];                /* device name */
                                            /* (console, lnxx) */
        pid_t   ut_pid;                     /* process id */
        short   ut_type;                    /* type of entry */
        struct  exit_status ut_exit;        /* process termination/exit */
                                            /* status */
        struct  timeval ut_tv;              /* time entry was made */
        long    ut_session;                 /* session ID, used for */
                                            /* windowing */
        long    pad[5];                     /* reserved for future use */
        short   ut_syslen;                  /* significant length of */
                                            /* ut_host */
                                            /* including terminating null */
        char    ut_host[257];               /* remote host name */
        };
     /* Definitions for ut_type */
     #define  EMPTY             0
     #define  RUN_LVL           1
     #define  BOOT_TIME         2
     #define  OLD_TIME          3
     #define  NEW_TIME          4
     #define  INIT_PROCESS      5           /* Process spawned by "init" */
     #define  LOGIN_PROCESS     6           /* A "getty" process waiting */
                                            /* for login */
     #define  USER_PROCESS      7           /* A user process */
     #define  DEAD_PROCESS      8
     #define  ACCOUNTING        9

     #define  UTMAXTYPE  ACCOUNTING         /* Largest legal value */
                                            /* of ut_type */

     /* Below are special strings or formats used in the "ut_line" */
     /* field when accounting for something other than a process. */
     /* No string for the ut_line field can be more than 11 chars + */
     /* a null character in length. */

     #define  RUNLVL_MSG                    "run-level %c"
     #define  BOOT_MSG                      "system boot"
     #define  OTIME_MSG                     "old time"
     #define  NTIME_MSG                     "new time"
     #define  MOD_WIN                       10

0
cokeman_Author Commented:
       char    ut_host[257];               /* remote host name */

it appears that this string is definitely long enough.. hmm.. out of curiousity, do you know why they used 257 instead of something like 255 or 256?  I've never seen 257 being used =)

I'll try this out when I get a chance tomorrow.  Thanks for your help.. it seems like it should give me what I need.
0
seedyCommented:
257?  No, neither have I.  But could it be 256 + 1 for the null?
Please note the ut_syslen field.  This must be used to find
the number of significant characters in 'ut_host'.  The maximum
for ut_syslen in my machine(Sun OS 5.5.1) was only 16.  But it
could be different in your machine - depending on the entries.
Cheers,
-Seedy-
0
seedyCommented:
Did it work for you?
0
cokeman_Author Commented:
it did.. it gave two of everything.. but looking at wt->ut_type, it was coded 7 and 8, i guess indicating login and logout.

hey, do you know how to deal with the timeval struct? ie, to get it into a date/time?

thanks
0
seedyCommented:
The field ut_tv is of type struct timeval.  This structure
is same as that is used in 'gettimeofday' function.
Read man pages for more info.  You can use ctime to convert
this to the character string.  So,
        ctime(&(wt->ut_tv.tv_sec))
will  do the trick for you.
Ps:  Last actually does a lot more.  It looks at both the login
and logout records and prints the duration of the session.
Cheers
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Unix OS

From novice to tech pro — start learning today.