Perl Solaris/Linux LASTLOG

D

drew

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
 
R

RedGrittyBrick

drew said:
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.
 
D

drew

thank you... works like a charm.
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top