Solved

script to retrieve info from flat file needed.

Posted on 2008-10-13
16
166 Views
Last Modified: 2013-12-25
Greetings,

I am working on an old site that has a htaccess password protected area. It also has a pipe delimited flat file that stores the unencrypted login info. I need a script to read the file and match the email address posted to the script from a form and then email the Username and Password to the email address if matched. It needs to stop at the first match and exit. If it does not match after reading all lines it needs to exit and print the failure to the browser.
This is a file with 600+lines.


Flat file is formatted as below:

1/15/08|User|FreePass|email@email.com|67.132.243.203

As always your input and help is greatly appreciated.

Thanks
0
Comment
Question by:capper5
  • 7
  • 6
  • 3
16 Comments
 
LVL 28

Expert Comment

by:FishMonger
ID: 22708231
What have you tried?

What part is giving you problems?

These docs should help you to get started.
perldoc -f open
perldoc -f split
perldoc -f last

If you have a script that you're working on, please post it and tell us what part of it is giving you problems.
0
 
LVL 8

Expert Comment

by:nognew
ID: 22710020
Hello!
 I have attached a script you need.
It takes an email address as a parameter and search through datafile.txt for the same address , if foound sends username and password.

Regards,
t.
#!/usr/bin/perl
 
my $email=shift || die "Email not specified\n";
 
open FP,"< datafile.txt";
 
print "Content-type: text/html\n\n";
print "<html><body>\n";
 
 
while(<FP>) {
   my @data_arr = split /\|/;
   print "$data_arr[3]\t $email\n";
   if ( $data_arr[3] == $email ) {
      open (MAIL, "|/usr/lib/sendmail -t");
      print MAIL "To: $data_arr[3]\n";
      print MAIL "From: script\n";
      print MAIL "Subject: get pass\n\n";
      print MAIL "Username: $data_arr[1] \n";
      print MAIL "Password: $data_arr[2] \n";
      close(MAIL);
 
      print "<h1>Email is sent.\n";
      print "</body></html>\n";
      close FP;
      exit(0);
   }
 
}
 
print "<h1>Email you specified has not found.\n";
print "</body></html>\n";
close FP;
 
exit(0);

Open in new window

0
 

Author Comment

by:capper5
ID: 22711292
Greetings,

I am playing with the script you posted and cannot get it to recognize the email that is input into the form.
it always dies with "Email not specified".  The form has one field named email and when submitted is posted to the script.

What am I missing?
#!/usr/bin/perl
use strict;
use warnings;
 
print "Content-type: text/html\n\n";
 
my $email=shift || die "Email not specified\n";
 
open FP,"< /path/to/datafile.txt";
 
 
print "<html><body>\n";
 
while(<FP>) {
   my @data_arr = split /\|/;
   print "$data_arr[3]\t $email\n";
   if ( $data_arr[3] == $email ) {
      open (MAIL, "|/usr/lib/sendmail -t");
      print MAIL "To: $data_arr[3]\n";
      print MAIL "From: script\n";
      print MAIL "Subject: get pass\n\n";
      print MAIL "Username: $data_arr[1] \n";
      print MAIL "Password: $data_arr[2] \n";
      close(MAIL);
 
      print "<h1>Email is sent.\n";
      print "</body></html>\n";
      close FP;
      exit(0);
   }
 
}
 
print "<h1>Email you specified has not found.\n";
print "</body></html>\n";
close FP;
 
exit(0);

Open in new window

0
How our DevOps Team Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

 
LVL 8

Expert Comment

by:nognew
ID: 22711351
hi!
 you should start it like that:
./script_name.pl youremail@email.com
0
 
LVL 8

Accepted Solution

by:
nognew earned 250 total points
ID: 22711408
Oh, got your question now, You need to use perl script with CGI interface.

Please see amended example.  Instead of reading email address from command line, it takes it from a form, taking into considiration that the field name is email.

Regards,
t.
#!/usr/bin/perl
use strict;
use warnings;
use CGI;
my $q = new CGI;
my $email = $q->param( 'email' );
 
print "Content-type: text/html\n\n";
 
#my $email=shift || die "Email not specified\n";
 
open FP,"< /path/to/datafile.txt";
 
 
print "<html><body>\n";
 
while(<FP>) {
   my @data_arr = split /\|/;
   print "$data_arr[3]\t $email\n";
   if ( $data_arr[3] == $email ) {
      open (MAIL, "|/usr/lib/sendmail -t");
      print MAIL "To: $data_arr[3]\n";
      print MAIL "From: script\n";
      print MAIL "Subject: get pass\n\n";
      print MAIL "Username: $data_arr[1] \n";
      print MAIL "Password: $data_arr[2] \n";
      close(MAIL);
 
      print "<h1>Email is sent.\n";
      print "</body></html>\n";
      close FP;
      exit(0);
   }
 
}
 
print "<h1>Email you specified has not found.\n";
print "</body></html>\n";
close FP;
 
exit(0);

Open in new window

0
 

Author Comment

by:capper5
ID: 22711407
--> ./script_name.pl youremail@email.com

I do not understand you refering to. Are you talking about the form action?

Sorry for the ignorance on this issue.
0
 
LVL 8

Expert Comment

by:nognew
ID: 22711440
I assume your form should look like:
<form action="script.pl" method="post">
<input type="text" name="email" />
<input type="submit" />
</form>

Open in new window

0
 

Author Comment

by:capper5
ID: 22711464
Yes that is correct on the look of the form.
0
 
LVL 8

Expert Comment

by:nognew
ID: 22711575
try to use that updated version of the script which has CGI library support. It must work for you.
0
 

Author Comment

by:capper5
ID: 22711718
It will now get the email using CGI to parse the form.
However, even if it does not match it will return the 1st record in the file.
I commented out the mail section and added print statements to troubleshoot.

This Line-->print "$data_arr[3]\t $email\n";
returns the email address of the 1st record in the file, and the posted email address.
the print statements also print the login info of the first record even though the posted email does not match the record in the file.

Attached is th code




#!/usr/bin/perl
use strict;
use warnings;
use CGI;
my $q = new CGI;
my $email = $q->param( 'email' );
 
print "Content-type: text/html\n\n";
 
#my $email=shift || die "Email not specified\n";
 
open FP,"< /path/to/datafile.txt";
 
 
print "<html><body>\n";
 
while(<FP>) {
   my @data_arr = split /\|/;
   print "$data_arr[3]\t $email\n";
   if ( $data_arr[3] == $email ) {
      #open (MAIL, "|/usr/lib/sendmail -t");
      #print MAIL "To: $data_arr[3]\n";
      #print MAIL "From: script\n";
      #print MAIL "Subject: get pass\n\n";
      #print MAIL "Username: $data_arr[1] \n";
      #print MAIL "Password: $data_arr[2] \n";
      #close(MAIL);
 
      print "Username: $data_arr[1] <br>\n";
      print "Password: $data_arr[2] <br> \n";
      print "Date Joined: $data_arr[0] <br> \n";
      print "<h1>Email is sent to<br>$data_arr[3]\n";
      #print "<h1>Email is sent.\n";
      print "</body></html>\n";
      close FP;
      exit(0);
   }
 
}
 
print "<h1>Email you specified has not found.\n";
print "</body></html>\n";
close FP;
 
exit(0);

Open in new window

0
 
LVL 8

Expert Comment

by:nognew
ID: 22712253
Thats weird, It must work, what operating system do you have?
0
 

Author Comment

by:capper5
ID: 22712378
Agreed, it does not make any sense to me either.

Below is the server info:

Perl Version: 5.008008
Apache/2.0.63 (Unix) mod_ssl/2.0.63 OpenSSL/0.9.8b
CENTOS Enterprise 5.2 i686 on virtuozzo
Linux 2.6.9-023stab048.4-enterprise
0
 
LVL 28

Assisted Solution

by:FishMonger
FishMonger earned 250 total points
ID: 22712424
Your if conditional is doing a numerical test when it should be doing string equality test.

$data_arr[3] == $email

should be
$data_arr[3] eq $email

You should also, ALWAYS check the return code of an open call and take action if it failes.

Why are you printing the email address for each and every line upto and including the desired email address?

If you're going to use the CGI module, as you should, you should use its methods for outputing the html.
0
 
LVL 28

Expert Comment

by:FishMonger
ID: 22712465
I probably should have explained that when you do a numerical test on a string, you're evaluating in boolean context.  So, if the var is defined it will return 1 in booleon context.
0
 
LVL 8

Expert Comment

by:nognew
ID: 22712599
i think I know there the bug is. replace that line
 if ( $data_arr[3] == $email ) {
with
 if ( $data_arr[3] eq $email ) {

Open in new window

0
 

Author Closing Comment

by:capper5
ID: 31505758
The print of the emails was for debugging only. The if conditional was the problem. I have tested using eq and it returns the values. Thanks for your assistance.
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It is a general practice to get rid of old user profiles on a computer  in a LAN environment. As I have been working with a company in a LAN environment where users move from one place to some other place at times. This will make many user profil…
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

825 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question