Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 174
  • Last Modified:

script to retrieve info from flat file needed.

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
capper5
Asked:
capper5
  • 7
  • 6
  • 3
2 Solutions
 
FishMongerCommented:
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
 
nognewCommented:
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
 
capper5Author Commented:
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
nognewCommented:
hi!
 you should start it like that:
./script_name.pl youremail@email.com
0
 
nognewCommented:
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
 
capper5Author Commented:
--> ./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
 
nognewCommented:
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
 
capper5Author Commented:
Yes that is correct on the look of the form.
0
 
nognewCommented:
try to use that updated version of the script which has CGI library support. It must work for you.
0
 
capper5Author Commented:
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
 
nognewCommented:
Thats weird, It must work, what operating system do you have?
0
 
capper5Author Commented:
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
 
FishMongerCommented:
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
 
FishMongerCommented:
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
 
nognewCommented:
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
 
capper5Author Commented:
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 7
  • 6
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now