shell script help (374 Views)
Reply
Super Advisor
rajesh73
Posts: 448
Registered: ‎03-17-2009
Message 1 of 2 (374 Views)

shell script help

i want to write a shell script

 

below is the output file

 

output

PS 030812 0 182 7

 

request is the password reset date 030812 is the 8th march 2012

182  days is password age

 

we need to write a script for all user, when ever user reach 170 days we send one auto reply mail to user, regarding your password expire.

 

Trusted Contributor
Steve Post
Posts: 1,058
Registered: ‎09-13-1998
Message 2 of 2 (349 Views)

Re: shell script help

I have half an answer for you.   I put in a test at the bottom of my .profile for when I log in.  It is not perfect.  It is far from secure.  The program does not walk on water, or turn water into wine.   It uses basic perl. 

 

Best of all.  It works for me.  It warns me if my password is close to expiring.

The missing part of your request?  The email you want.

 

The main key to this is the /usr/bin/passwd  -r files -s unix_login_name_here

 

So if my unix login is Billy_bobby_billy_bo_bob, I run

/usr/bin/passwd -r file -s Billy_bobby_billy_bo_bob.

 

The 86400 is 60 seconds times 60 minutes times 24 hours.  I.e. number of seconds per day.

 

 

#!/usr/bin/perl
use Time::Local;
$WARN=14;
#___________________________________________________
# todays date
#________________________________________________________
( $T_sec, $T_min, $T_hour, $T_day, $T_mon, $T_year, @junk ) = localtime();
$T_epoch = timelocal( 0, $T_min, $T_hour, $T_day, $T_mon, $T_year);
($T_sec,$T_min,$T_hour,$T_day,$T_mon,$T_year,@junk ) = localtime($T_epoch);
$T_mon = $T_mon + 1;
$T_year = $T_year + 1900;
#print "Today is day $T_day month $T_mon yr $T_year\n";

#__________________________________________
# reset password date  R_xxxx
#__________________________________________
$CMD="\/usr\/bin\/passwd -r files -s $ENV{LOGNAME} |";
open(COMMAND, $CMD) || die "passwd cmd error";

while ( <COMMAND> ) {
  ( @line ) = split;
   if ( $#line == 4 ){
     if ( $line[3] < 10 and
          $line[4] > 50 and
          $line[4] < 80 and
          $line[1] eq "PS"  ) {
       # foreach $x (  @line ) { print "x is $x\n"; }
       # $line[0] name
       # $line[1] PS
       # $line[2] mm/dd/yy  $dline[0], $dline[1], $dline[2]
       # $line[3] < 10
       # $line[4] < 80 and > 50

       @dline = split(/\//, $line[2]);
       $R_epoch=timelocal( 0, 0, 0, $dline[1], $dline[0]-1, $dline[2]);
       $R_epoch=$R_epoch + ( $line[4] * 86400 );
       ($R_sec,$R_min,$R_hour,$R_day,$R_mon,$R_year ) = localtime($R_epoch);
       $R_mon++;
       $R_year = $R_year + 1900;
       #print "Password day $R_day month $R_mon yr $R_year\n";

       #------------------------------
       # Dday is the reset required date - warning days
       #------------------------------
       $D_epoch=$R_epoch - ( $WARN * 86400 );
       ($D_sec,$D_min,$D_hour,$D_day,$D_mon,$D_year ) = localtime($D_epoch);
       $D_mon++;
       $D_year = $D_year + 1900;
       #print "D-day        $D_day month $D_mon yr $D_year\n";

       if ( $T_epoch >= $D_epoch ) {
           print "User $ENV{LOGNAME}, You need to change our password soon.\n";
           printf("Today is day %02d/%02d/%04d\n", $T_mon, $T_day, $T_year);
           printf("Warn day is  %02d/%02d/%04d\n", $D_mon, $D_day, $D_year);
           printf("Password day %02d/%02d/%04d\n", $R_mon, $R_day, $R_year);
       } else {
           print "User $ENV{LOGNAME}, ";
           printf("the day your password dies is %02d/%02d/%04d\n",
                $R_mon, $R_day, $R_year);
       }
     }
   }
}
close(COMMAND);

 

The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation.