Solved

script to retrieve info from flat file needed.

Posted on 2008-10-13
16
169 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

 
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

The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

Question has a verified solution.

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

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
This article will show, step by step, how to integrate R code into a R Sweave document
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

734 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