home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #18 / NN_1992_18.iso / spool / comp / lang / perl / 5316 < prev    next >
Encoding:
Text File  |  1992-08-14  |  1.6 KB  |  51 lines

  1. Newsgroups: comp.lang.perl
  2. Path: sparky!uunet!caen!sol.ctr.columbia.edu!eff!ckd
  3. From: ckd@eff.org (Christopher Davis)
  4. Subject: another semi-useful 10-minute hack
  5. Message-ID: <CKD.92Aug14120200@loiosh.eff.org>
  6. Sender: usenet@eff.org (NNTP News Poster)
  7. Nntp-Posting-Host: loiosh.eff.org
  8. Organization: Electronic Frontier Foundation Tech Central
  9. Date: Fri, 14 Aug 1992 16:02:02 GMT
  10. Lines: 39
  11.  
  12. This is just another perlvangelistic posting showing how you can easily
  13. toss together a bit of perl code to do something useful.
  14.  
  15. this will print the lastlog entry for a user, or you if you don't specify.
  16.  
  17. took about 10 minutes; the main "problem" was a typo ($llty for $lltty)
  18. which -w caught nicely.
  19.  
  20. #!/usr/bin/perl -- # -*- Perl -*-
  21.  
  22. # based on lastlog.h, so change it if you must
  23. # assumes Sun format, time_t is a 4byte long, line is 8 chars, host is 16 chars
  24. # should use c2ph or something to do this right, it's a hack
  25. require 'ctime.pl';
  26.  
  27. $lastlogfile = "/usr/adm/lastlog";
  28. $lastlogfmt = "La8a16";
  29. $lastlogsize = 28;
  30.  
  31. $username = shift(@ARGV) || getlogin;
  32. $userid = (getpwnam($username))[2];
  33.  
  34. open(LASTLOG,"<$lastlogfile");
  35.  
  36. seek(LASTLOG,$lastlogsize*$userid,0);
  37.  
  38. read(LASTLOG,$lastlogentry,$lastlogsize);
  39.  
  40. ($lltime,$lltty,$llhost) = unpack($lastlogfmt,$lastlogentry);
  41.  
  42. chop($lldate = &ctime($lltime));
  43.  
  44. print "last login for $username: $lldate on line $lltty";
  45. print " from host $llhost" if $llhost;
  46. print "\n";
  47. --
  48. Christopher Davis * ckd@eff.org * System Administrator, EFF * +1 617 864 0665
  49.             ``Ed Gruberman, you fail to grasp Ti Kwan Leep.
  50.             Approach me that you might see.'' -- The Master
  51.