My Computer Programs

(Perl) lastpwchg.pl - prints users' last password change times

steloflute 2013. 9. 23. 23:30

#!/usr/bin/perl
use Term::ANSIColor;
print "lastpwchg.pl - Prints users' last password change times.\n";
print "(C) 2013 KIM Taegyoon\n\n";

chomp(my $os=`uname`);
print "OS: $os\n";
if ($os eq "AIX") {
  open (my $in, "<", "/etc/security/passwd") or die "Can't open the file.";
  my $id;
  while (<$in>) {
    if (/(^.+):$/) {$id=$1}
    elsif (/lastupdate = (\d+)/) {
      my $now=time;
      my $time=scalar localtime $1;
      my $day=sprintf('%.1f',($now - $1)/(24*60*60));
      printf "%-16s", $id;
      print "$time\tPassword changed ";
      if ($day > 90) { print color('bold red'), $day, color('reset'); }
      else { print $day }
      print " days ago\n";
    }
  }
} elsif ($os eq "Linux" or $os eq "SunOS") {
  open (my $in, "<", "/etc/shadow") or die "Can't open the file.";
  while (<$in>) {
    if (/^(.+?):.*?:(\d+):/) {
      my $id=$1;
      my $now=time;
      my $timesec = $2*24*60*60;
      my $time=scalar localtime $timesec;
      my $day=sprintf('%.1f',($now - $timesec)/(24*60*60));
      printf "%-16s", $id;
      print "$time\tPassword changed ";
      if ($day > 90) { print color('bold red'), $day, color('reset'); }
      else { print $day }
      print " days ago\n";
    }
  }
}

 

 

newLISP:

lastpw.lsp

 

 

#!/usr/bin/newlisp
(print "lastpw.lsp - Prints users' last password change times.\n")
(print "(C) 2013 KIM Taegyoon\n\n")

(define (die s)
  (println s)
  (exit 1))

(setq os ostype)
(println "OS: " os)
(cond
  ((= os "AIX")
    (unless (setq in (open "/etc/security/passwd" "r"))
      (die "Can't open the file."))
    (while (read-line in)
      (cond
        ((regex {(^.+):$} (current-line)) (setq id $1))
        ((regex {lastupdate = (\d+)} (current-line))
          (setq nowsec (date-value))
          (setq timestr (date (int $1)))
          (setq daystr (format "%.1f" (/ (- nowsec (int $1)) (* 24 60 60))))
          (println (format "%-16s" id) timestr "\tPassword changed " daystr " days ago")))))
  ((or (= os "Linux") (= os "SunOS"))
    (unless (setq in (open "/etc/shadow" "r"))
      (die "Can't open the file."))   
    (while (read-line in)
      (when (regex {^(.+?):.*?:(\d+):} (current-line))
        (setq id $1)
        (setq nowsec (date-value))
        (setq timesec (* (int $2) 24 60 60))
        (setq timestr (date timesec))
        (setq daystr (format "%.1f" (/ (- nowsec timesec) (* 24 60 60))))
        (println (format "%-16s%s\tPassword changed %s days ago" id timestr daystr))))))
(exit)

 

 

 

'My Computer Programs' 카테고리의 다른 글

convert unix time to local time  (0) 2013.10.01
(Perl) psh - Perl REPL  (0) 2013.09.24
(Perl) lastupdate-aix.pl  (0) 2013.09.16
(Paren) SweepSecondParen  (0) 2013.09.08
(Racket) SweepSecond  (0) 2013.08.22