Perl Solaris/Linux LASTLOG

Discussion in 'Perl Misc' started by drew, Aug 11, 2005.

  1. drew

    drew Guest

    i have to process a large amount of data and i cannot perform this on
    the invidual servers for either security sake or the way we have been setup.


    here is the script from the PERL COOKBOOK to show the closest approach

    #!/usr/bin/perl
    # laston - find out when given user last logged on
    use User::pwent;
    use IO::Seekable qw(SEEK_SET);

    open (LASTLOG, "/var/log/lastlog") or die "can't open /usr/adm/lastlog: $!";

    $typedef = 'L A12 A16'; # linux fmt; sunos is "L A8 A16"
    $sizeof = length(pack($typedef, ()));

    for $user (@ARGV) {
    $U = ($user =~ /^\d+$/) ? getpwuid($user) : getpwnam($user);
    unless ($U) { warn "no such uid $user\n"; next; }
    seek(LASTLOG, $U->uid * $sizeof, SEEK_SET) or die "seek failed: $!";
    read(LASTLOG, $buffer, $sizeof) == $sizeof or next;
    ($time, $line, $host) = unpack($typedef, $buffer);
    printf "%-8s UID %5d %s%s%s\n", $U->name, $U->uid,
    $time ? ("at " . localtime($time)) : "never logged in",
    $line && " on $line",
    $host && " from $host";
    }






    i have geathered all the passwd files/lastlog file for each server

    how do i get getpwuid / getpwnam to work on the the password file that
    i gathered and then work on the lastlog of the associated server?

    all pointers will be appreciated and thanks.
    prasanth
     
    drew, Aug 11, 2005
    #1
    1. Advertising

  2. drew wrote:
    > i have to process a large amount of data and i cannot perform this on
    > the invidual servers for either security sake or the way we have been
    > setup.
    >
    >
    > here is the script from the PERL COOKBOOK to show the closest approach
    >
    > #!/usr/bin/perl
    > # laston - find out when given user last logged on
    > use User::pwent;
    > use IO::Seekable qw(SEEK_SET);
    >
    > open (LASTLOG, "/var/log/lastlog") or die "can't open /usr/adm/lastlog:
    > $!";
    >
    > $typedef = 'L A12 A16'; # linux fmt; sunos is "L A8 A16"
    > $sizeof = length(pack($typedef, ()));
    >
    > for $user (@ARGV) {
    > $U = ($user =~ /^\d+$/) ? getpwuid($user) : getpwnam($user);
    > unless ($U) { warn "no such uid $user\n"; next; }
    > seek(LASTLOG, $U->uid * $sizeof, SEEK_SET) or die "seek failed: $!";
    > read(LASTLOG, $buffer, $sizeof) == $sizeof or next;
    > ($time, $line, $host) = unpack($typedef, $buffer);
    > printf "%-8s UID %5d %s%s%s\n", $U->name, $U->uid,
    > $time ? ("at " . localtime($time)) : "never logged in",
    > $line && " on $line",
    > $host && " from $host";
    > }
    >
    >
    >
    >
    >
    >
    > i have geathered all the passwd files/lastlog file for each server
    >
    > how do i get getpwuid / getpwnam to work on the the password file that i
    > gathered and then work on the lastlog of the associated server?
    >


    I'd not use getpwuid and getpwnam. I'd read the passwd files, split the
    records and store the UID and Name in a hash keyed by Login-ID. Then I'd
    use '$name{$user}' in place of 'getpwnam($user)'

    I'd either keep each passwd and lastlog pair in its own directory named
    by server, or, I'd maybe prefix the filenames with servername and
    iterate over a predefined list of server names.

    YMMV.
     
    RedGrittyBrick, Aug 11, 2005
    #2
    1. Advertising

  3. drew

    drew Guest

    thank you... works like a charm.

    RedGrittyBrick wrote:
    > drew wrote:
    >
    >> i have to process a large amount of data and i cannot perform this on
    >> the invidual servers for either security sake or the way we have been
    >> setup.
    >>
    >>
    >> here is the script from the PERL COOKBOOK to show the closest approach
    >>
    >> #!/usr/bin/perl
    >> # laston - find out when given user last logged on
    >> use User::pwent;
    >> use IO::Seekable qw(SEEK_SET);
    >>
    >> open (LASTLOG, "/var/log/lastlog") or die "can't open
    >> /usr/adm/lastlog: $!";
    >>
    >> $typedef = 'L A12 A16'; # linux fmt; sunos is "L A8 A16"
    >> $sizeof = length(pack($typedef, ()));
    >>
    >> for $user (@ARGV) {
    >> $U = ($user =~ /^\d+$/) ? getpwuid($user) : getpwnam($user);
    >> unless ($U) { warn "no such uid $user\n"; next; }
    >> seek(LASTLOG, $U->uid * $sizeof, SEEK_SET) or die "seek failed: $!";
    >> read(LASTLOG, $buffer, $sizeof) == $sizeof or next;
    >> ($time, $line, $host) = unpack($typedef, $buffer);
    >> printf "%-8s UID %5d %s%s%s\n", $U->name, $U->uid,
    >> $time ? ("at " . localtime($time)) : "never logged in",
    >> $line && " on $line",
    >> $host && " from $host";
    >> }
    >>
    >>
    >>
    >>
    >>
    >>
    >> i have geathered all the passwd files/lastlog file for each server
    >>
    >> how do i get getpwuid / getpwnam to work on the the password file that
    >> i gathered and then work on the lastlog of the associated server?
    >>

    >
    > I'd not use getpwuid and getpwnam. I'd read the passwd files, split the
    > records and store the UID and Name in a hash keyed by Login-ID. Then I'd
    > use '$name{$user}' in place of 'getpwnam($user)'
    >
    > I'd either keep each passwd and lastlog pair in its own directory named
    > by server, or, I'd maybe prefix the filenames with servername and
    > iterate over a predefined list of server names.
    >
    > YMMV.
     
    drew, Aug 17, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. elastic
    Replies:
    1
    Views:
    652
    elastic
    Feb 3, 2004
  2. thomhashi
    Replies:
    2
    Views:
    841
    Nils O. =?iso-8859-1?Q?Sel=E5sdal?=
    Oct 31, 2003
  3. Garry
    Replies:
    0
    Views:
    362
    Garry
    Jan 18, 2005
  4. nitin
    Replies:
    2
    Views:
    444
    Ron Natalie
    Jul 14, 2003
  5. Frank
    Replies:
    0
    Views:
    396
    Frank
    Jul 18, 2003
Loading...

Share This Page