home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / comp / lang / perl / 4966 < prev    next >
Encoding:
Internet Message Format  |  1992-07-26  |  2.5 KB

  1. Path: sparky!uunet!zephyr.ens.tek.com!uw-beaver!micro-heart-of-gold.mit.edu!rutgers!usc!zaphod.mps.ohio-state.edu!mips!spool.mu.edu!news.cs.indiana.edu!nstn.ns.ca!morgan.ucs.mun.ca!csd.unb.ca!jaf
  2. From: jaf@jupiter.sun.csd.unb.ca (J Anthony Fitzgerald)
  3. Newsgroups: comp.lang.perl
  4. Subject: print and write display different results
  5. Message-ID: <1992Jul26.155523.12416@jupiter.sun.csd.unb.ca>
  6. Date: 26 Jul 92 15:55:23 GMT
  7. Distribution: na
  8. Organization: University of New Brunswick
  9. Lines: 58
  10.  
  11. Running perl 4.035 on SunOS 4.1.2,  I am trying to extract the name of
  12. the system from which a user last logged in and print it in a report.
  13. The script and a sample output are enclosed below.  The script seems to
  14. be able to extract the value that I want and will print it out when I
  15. use a print statement, however, when I use a write statement, the value
  16. is not printed.  Any suggestions?
  17.  
  18. ============================ start enclosure
  19. #!/usr/local/bin/perl
  20. #  Process list of compromised accounts and lock same
  21.  
  22. require "getopts.pl";
  23. &Getopts( "tld:P:" );
  24.     $test=defined $opt_t;
  25.  
  26. open( PWD, "ypcat passwd |" ) || die "can't ypcat passwd: $!\n";
  27. while( <PWD> ) { $pass{(split(/:/))[0]}= $_; }
  28. close( PWD );
  29.  
  30. open( LOG, "< /var/adm/lastlog" ) || die "can't open lastlog: $!\n";
  31. foreach $lid ( @ARGV ) {
  32.     if( $lid=~ /^\d+$/ ) {
  33.         $uid= $lid;
  34.     } else {
  35.         ( $uid,$gcos )= ( split( ':', $pass{$lid} ) )[ 2,4 ];
  36.     }
  37.     if( seek( LOG, $uid*28, 0 ) ){}
  38.     sysread( LOG, $lastlog, 28 );
  39.     ( $ltime, $lport, $llfrom )= unpack( "la5A*", $lastlog );
  40.     ( $ly, $lmo, $lda )= (localtime($ltime))[ 5,4,3 ];
  41.     if( $ltime ) {
  42.         ( $llda= sprintf( "%2d%2d%2d", $ly, 1+$lmo, $lda ) )=~ tr/ /0/;
  43.         $llog= $llda;
  44.         $llog.= "@${llfrom}"    if $llfrom;
  45.     } else { $llog= ""; }
  46.     write;
  47.         print "test: $llfrom  $lport $llog\n";
  48. }
  49. close( LOG );
  50.  
  51. format STDOUT_TOP =
  52. loginid   lastlog                       Name, etc.
  53. ========  ===========================   =============================
  54. .
  55. format STDOUT =
  56. @<<<<<<<  @<<<<<<<<<<<<<<<<<<<<<<<<<<   @<<<<<<<<<<<<<<<<<<<<<<<<<<<<
  57. $lid,     $llog,                        $gcos
  58. .
  59. ================================  sample output of script
  60. loginid   lastlog                       Name, etc.
  61. ========  ===========================   =============================
  62. mikes     920725@                       Mike Sprague Dept. Environmen
  63. test: milton.u.washing  ttyp0 920725@milton.u.washing
  64. ================================  end enclosures
  65. -- 
  66. J. Anthony Fitzgerald    jaf@UNB.ca    (506) 453-4573
  67. Computing Services  UofNB  Box 4400    (506) 453-3590  (FAX)
  68. Fredericton, NB,  CANADA,  E3B 5A3
  69.