Advertisement
Guest User

Wtmp Parse

a guest
Feb 11th, 2011
1,111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 1.72 KB | None | 0 0
  1. ($utmp_file) = @ARGV;
  2.  
  3. print "Parsing file:$utmp_file\n";
  4.  
  5. $struct_utmp = "A8A4A12NnnnnNA16N";
  6.  
  7. # Todos los numéricos están dados en Network Format (es decir, big endian)
  8.  
  9. # Tipos de u_type
  10.  
  11. @u_type = ( 'EMPTY','RUN_LVL','BOOT_TIME','OLD_TIME','NEW_TIME',        
  12.             'INIT_PROCESS',    #      /* Process spawned by "init" */
  13.             'LOGIN_PROCESS',   #      /* getty process awaiting login */
  14.             'USER_PROCESS',    #      /* A user process */
  15.             'DEAD_PROCESS',    
  16.             'ACCOUNTING',      
  17.           );
  18.  
  19.  
  20.  
  21. $length = length(pack($struct_utmp));
  22.  
  23. open(UTMP, $utmp_file) || die "open: $!\n";
  24. binmode UTMP;
  25.  
  26.  
  27. print "user,id,line,pid,type,exit_status_termination,exit_status_exit,reserved,time,host,addr\n";  
  28.  
  29. while (read(UTMP, $_, $length)) {
  30.    ($user, $id, $line, $pid, $type, $exit_status_termination,$exit_status_exit, $reserved, $time, $host, $addr) =  unpack($struct_utmp, $_);
  31.    print "$user,$id,$line,$pid,$u_type[$type],$exit_status_termination,$exit_status_exit,$reserved,", scalar localtime($time),",$host,",Integer2Ip($addr),"\n";    
  32.  
  33. }
  34.  
  35. close(UTMP);
  36.  
  37.  
  38.  
  39. ############################################################################
  40. #
  41. #   Name:    Integer2Ip
  42. #
  43. #   Purpose: Convert an integer value into an dotted quad
  44. #
  45. #   Inputs:  $integer - Integer value
  46. #
  47. #   Returns: Dotted quad string, dies in case of problems
  48. #
  49. ############################################################################
  50.  
  51. sub Integer2Ip ($) {
  52.     my $integer = shift;
  53.     my $four = $integer & 0xff;
  54.     $integer >>= 8;
  55.     my $three = $integer & 0xff;
  56.     $integer >>= 8;
  57.     my $two = $integer & 0xff;
  58.     $integer >>= 8;
  59.     my $one = $integer;
  60.     "$one.$two.$three.$four";
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement