Solved

script to retrieve info from flat file needed.

Posted on 2008-10-13
16
167 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
Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

 
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

Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Perl Untar File 1 70
Powershell Exchange mailboxsizes 3 49
Review of the nodejs code 1 38
powershell automatic variable for results 21 28
I hope you'll find this tutorial useful and interesting. So let's try to extend Tcl with a new package.  For anyone more deeply interested please check out the book "Practical Programming in Tcl and Tk". It's really one of the best written books abo…
The Windows functions GetTickCount and timeGetTime retrieve the number of milliseconds since the system was started. However, the value is stored in a DWORD, which means that it wraps around to zero every 49.7 days. This article shows how to solve t…
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…

730 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