Question : how to read /var/adm/wtmp file of HP-UX

Hi,
  I have a Perl script which works fine on Solaris 10 but it doesnot show any output when i run it on HP-UX 11 servers... I need to know unpack command to get values from HP wtmp file?

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
#!/opt/OV/nonOV/perl/a/bin/perl
use strict;
my $OS= (`uname`);
my $DATE=(`date +%Y-%m-%d`);
my $HOST=(`hostname`);
my $OUTDATE=(`date +%Y%m%d`);
my $FILE="USER-$HOST";
chomp $OS;
chomp $DATE;
chomp $HOST;
chomp $FILE;

my $template = "A32 A4 A32 l s s2 x2 l2 l x20 s A257 x";
my $recordsize = length(pack($template,()));


open OUT, '>>',"$FILE" or die "could not write to file: $!";
open WTMP,"/var/adm/wtmp" or die "Unable to open wtmp:$!\n";
my ($record, %prev);
   printf(OUT "%7s   %8s         %-5s %-4s %-5s %s %s  %s %s %s %s    %s   %-s\n", 'HOST', 'DATE', 'OS', 'USER', 'PORT' , 'WEEK', 'MONTH', 'DAY', 'START', "-", 'END' ,'DURATIO
N','IP');
MAIN:
while (read(WTMP,$record,$recordsize)) {
    # 0 $ut_user
    # 1 $ut_id
    # 2 $ut_line
    # 3 $ut_pid
    # 4 $ut_type
    # 5 $ut_e_termination
    # 6 $ut_e_exit
    # 7 $tv_sec
    # 8 $tv_usec
    # 9 $ut_session
    # 10 $ut_syslen
    # 11 $ut_host
    my @vals = unpack($template,$record);
    # put all the conditionals on this line to make changing them easier
    next MAIN unless ($vals[0] and $vals[7] >= time-86400 and $vals[0] =~ /bsp/);
#    next MAIN unless ($vals[0] and $vals[0] =~ /bsp/);
    if ($vals[11]) { # login
        if ($vals[2] =~ m{^ftp} and exists $prev{$vals[0]}{$vals[2]}) {
            # it's ftp and already exists so it's really a logout
            my @stm = (localtime $prev{$vals[0]}{$vals[2]}[7])[6,4,3,2,1];
#            $stm[0] = (qw(Sun Mon Tue Wed The Fri Sat))[$stm[0]];
#            $stm[1] = (qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec))[$stm[1]];
            my @etm = (localtime $vals[7])[2,1];
            my $dmin = int(($vals[7] - $prev{$vals[0]}{$vals[2]}[7])/60); # get min
            my $dhr = int($dmin/60); # get hours
            $dmin -= $dhr*60;

         printf(OUT "%7s,  %8s,  %5s, %-4s,%-5s,  %s,  %s,    %02d, %02d:%02d,-,%02d:%02d, (%02d:%02d), %-s\n", $HOST , $DATE, $OS , $vals[0], 'ftp' , @stm, @etm, $dhr, $dmin,
 $prev{$vals[0]}{$vals[2]}[11]);
            delete $prev{$vals[0]}{$vals[2]};
        } else {
            # error-checking
            if (exists $prev{$vals[0]}{$vals[2]}) {
            #    warn "there's already a login entry for $vals[0] on $vals[2] - over-writing";
            }
            $prev{$vals[0]}{$vals[2]} = [@vals];
        }
    } else { # assuming logout/dead proc
        # error-checking
        if (not exists $prev{$vals[0]}{$vals[2]}) {
#    warn "could not find a login entry for $vals[0] on $vals[2] - skipping";
            next MAIN;
        }
        my @stm = (localtime $prev{$vals[0]}{$vals[2]}[7])[6,4,3,2,1];
#        $stm[0] = (qw(Sun Mon Tue Wed Thu Fri Sat))[$stm[0]];
#        $stm[1] = (qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec))[$stm[1]];
        my @etm = (localtime $vals[7])[2,1];
        my $dmin = int(($vals[7] - $prev{$vals[0]}{$vals[2]}[7])/60); # get min
        my $dhr = int($dmin/60); # get hours
        $dmin -= $dhr*60;
       printf(OUT "%7s,  %8s,  %5s, %-4s,%-5s,  %s,  %s,    %02d, %02d:%02d,-,%02d:%02d, (%02d:%02d), %-s\n", $HOST , $DATE, $OS, $vals[0], $vals[2], @stm, @etm, $dhr, $dmin,
$prev{$vals[0]}{$vals[2]}[11]);
        delete $prev{$vals[0]}{$vals[2]};
    }
}
close WTMP;

Answer : how to read /var/adm/wtmp file of HP-UX

Ah.  I see the problem.  Change line 8 to be:

my $script = basename $0;
my $TMPFILE = ".$script.last";

and add this after line 2:

use warnings;  # not strictly necessary but always a good idea
use File::Basename;
Random Solutions  
 
programming4us programming4us