Link to home
Start Free TrialLog in
Avatar of denmarkw
denmarkw

asked on

compare two dates in script

I need to write a script that will notify users of their password expiration after logging in to to
a Win95 client. The text file with the user's password expiration date will reside in their
$HOME directory. The script will reside in the Windows startup folder. The script should
compare the date in the file with the system date, and print a "Password will expire in x days"
message if the date in the file is less than 5 days than the current (system) date.
The script will also need to update the file in the user's $HOME directory with the new
password expiration date after the user changes his or her password.
Can I accomplish this with perl?  I would appreciate some source code to help me write
this solution. I installed Perl for Win95 and I want to start using its functionality, but I think I
need some help to get off the ground. I am digesting all the documentation now, but I have to
write this solution now!

Thanks in advance for your help!
Avatar of ozo
ozo
Flag of United States of America image

What's the format of the date in the text file?
ASKER CERTIFIED SOLUTION
Avatar of tpryor
tpryor

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of tpryor
tpryor

OK , there are a couple parts to your question lets take them one at a time.

1) Read the file line by line looking for a match...

open(FILE, "pwd/file/path") || print couldnt open the file, $!;
while (<FILE>) {
      if ($_ =~ /$user/) { # be as specific as neeeded
            #### check dates todays vs pwd

2) Logic to check dates...

# I recomend storing the date as yymmdd
# makes for easy comparison
# use the date.pl library to help you format the dates
# i will give you the url for it

($user, $pwd, $user_date) = split(/\/, $_);
# this will split the matched record into fields

$today_julian_day = &today();
# this will give me the julian date for today

$days_ago = 30;
# freshness date

($month,$day,$year,$weekday) = &jdate($julian_day - $days_ago)
# this will format it into something a human can understand

$bench_mark_date = sprintf("%02d%02d%02d", $year, $month, $day);
# this will make it easy to compare

if($user_date <= $bench_mark_date){

#### Tell user to change pwd
#### close file
#### exit program

}else{

#### continue with program
}

3) if the user must change pwd

### present login screen with new pwd option

same as no.1, open pwd file read line by line when you find him split up the line change the password field write back to file.
make sure you look the file when writing to it.

I didnt go into to many details about reading the file and writeing because that is pretty basic. you can go to www.perl.com
and get a ton of info about that kind of stuff. The text file is simple just a file where each line is a record with each ellement in the record seperated by something ie
tom|password|19980629
Im not sure of your programming level so Ill leave it at that, i dont wont to bore you with the basics if you already know them.

The tricky part is dealing with dates, as long as your system deals with julian dates (most do) this code will do the trick.
I will give you the url to date.pl to use it make sure you type
use date.pl
in your cgi before you call the time/date functions. And that you save the date.pl library in the same dir as your cgi.
 should be all set.

the date.pl url is...
http://www.extropia.com/Scripts/Source/Library/date.pl.txt

GL
t

># I recomend storing the date as yymmdd
># makes for easy comparison

And people are still amazed that software produced within the past few years, let alone today, has a Y2K problem... (Although in this case it won't fail catastrophically, it's so easy to do it right that you should use a 4 digit year).

I am not criticizing you, tpryor, just pointing out how embedded 2 digit years are in most people's thinking, leading to a $trillion problem.
cool!


BTW the code example actually produces a 4 digit year.
<gr>
t

Avatar of denmarkw

ASKER

Thanks for your comments!
tpryor:
Please lets clarify some of the code. I am just a beginner when it comes to Perl!
So I would appreciate any details for clarity.
First, the compare file on the client PC only has
the password expiration date.  This file has to be created and populated with the
system date after the first reboot which will force the user to change his or her Solaris
password. How do I create this file and put the current Win95 system date in it? How can
I control the date format?
Second, is the split necessary if only a date is in the compare file?
Third, why do we subrtract 30 days? After the first system reboot the script should force
a password change and update the expiration date by adding 60 days to the current date
and update the compare file on the client.  Any subsequent reboot should only compare the difference between the current system date and the date in the compare file.
If the compare date is less than the system date by 5 days or less, a window should pop
up on the Win95 client notifying the user of the number of days until expiration and should give the user the option to change his or her password from the pop-up window.
How can I create a GUI with Perl for win32?
Fourth, I need to call the Win95 "telnet" executable after the first reboot and whenever the
compare condition is met. How do I make system calls in Perl?
Thanks in advance for your assistance experts!

Knowledge is power, but it is only useful if it is shared!