4 Replies - 1461 Views - Last Post: 20 March 2012 - 03:00 AM

#1 Dface  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 18-March 12

How to use wordlist file in perl script

Posted 18 March 2012 - 10:38 AM

In script i want to use wordlist instead of this words (foo bar baz boo baa bum sim sala bim)
How to include external wordlist file?
Thanx for helping !


#!/usr/bin/perl -w
use strict;
use warnings;
use threads;
use Thread::Queue;
use threads::shared;

my @passes=qw(foo bar baz boo baa bum sim sala bim);

foreach my $pass(@passes){
do{} until threads->list(threads::running)<10;
my $thr = threads->create('doshit',($pass));
foreach(threads->list(threads::joinable)){
$_->join();
}
}
do{sleep 1}while(threads->list(threads::running)!=0);

sub doshit(){
my($pass)=@_;
print 'Trying: ' . $pass . "\n";
use LWP::Simple;
my $raw = get 'http://www.sample.com' . $pass;
print '--->' . $raw . "\n";
use JSON;
my $json = ();
eval{$json = JSON->new->utf8(1)->decode($raw)};
if(defined $json->{'message'}){
if($json->{'message'} eq 'Access Denied'){
print 'FAIL: ' . $pass . "\n";
}else{
print 'SUCCESS: ' . $pass . "\n";
die;
}
}
}

Is This A Good Question/Topic? 0
  • +

Replies To: How to use wordlist file in perl script

#2 dsherohman  Icon User is offline

  • Perl Parson
  • member icon

Reputation: 226
  • View blog
  • Posts: 654
  • Joined: 29-March 09

Re: How to use wordlist file in perl script

Posted 19 March 2012 - 06:59 AM

To read the words from an external file containing one word per line, you would use:
open my $file_handle, '<', $filename or die "file open failed: $!\n";
while (my $line = <$file_handle>) {
  # process the line
}



If you just want to pull all the lines into an array, you can instead use
open my $file_handle, '<', $filename or die "file open failed: $!\n";
my @passes = <$file_handle>;


but it looks like the only thing you're doing with @passes is running a for loop over it, so you may as well just skip the array and process each line as it's read.

Note that the lines read from the file will have newline characters at the end of them, so there's a good chance that the first thing you'll want to do with each line when processing it will be
chomp $line;

Was This Post Helpful? 1
  • +
  • -

#3 Dface  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 18-March 12

Re: How to use wordlist file in perl script

Posted 19 March 2012 - 12:11 PM

#2 dsherohman
Thanx for response !

Yes i want to read one word per line
What u think about this to input?
open(FILE, "/path/to/file") or die "open() failed: $!";
my @passes = <FILE>;
close(FILE);
foreach my $line (@passes) {
chomp($line);
}

As you suggest i use chomp
script working BUT in several seconds pops a message "Use of uninitialized value $raw in concatenation (.) or string at 2.pl line 29."
in that line is:
print '--->' . $raw . "\n";

after it continued working, but soon stopped with error "PANIC: underlying join failed at 2.pl line 19."
line 19 is:
$_->join();

I think there is a problem from server limiting threads or requests...
Is there some workaround?
Was This Post Helpful? 0
  • +
  • -

#4 dsherohman  Icon User is offline

  • Perl Parson
  • member icon

Reputation: 226
  • View blog
  • Posts: 654
  • Joined: 29-March 09

Re: How to use wordlist file in perl script

Posted 20 March 2012 - 02:09 AM

View PostDface, on 19 March 2012 - 08:11 PM, said:

What u think about this to input?
open(FILE, "/path/to/file") or die "open() failed: $!";


This is an older form of the open command. The currently-preferred style is to use the three-argument form of open (open $handle, 'mode', 'filename') and lexical filehandles ($handle rather than HANDLE), as in my earlier reply. The three-arg open improves security (in the general case; if the filename is hard-coded, there's no real difference, but it's still a good habit to develop) and allows for better handling of files containing something other than plain ASCII text (e.g., UTF-8 strings), while lexical filehandles improve maintainability (they're scoped variables rather than globals).

Other than that, it looks good.

View PostDface, on 19 March 2012 - 08:11 PM, said:

As you suggest i use chomp
script working BUT in several seconds pops a message "Use of uninitialized value $raw in concatenation (.) or string at 2.pl line 29."
in that line is:
print '--->' . $raw . "\n";


No value has been assigned to $raw before you print it. I can't say anything more specific because you haven't shown where $raw comes from.

View PostDface, on 19 March 2012 - 08:11 PM, said:

after it continued working, but soon stopped with error "PANIC: underlying join failed at 2.pl line 19."
line 19 is:
$_->join();

I think there is a problem from server limiting threads or requests...
Is there some workaround?


Congratulations! You've come up with a Perl error message that I've never seen before! :D

I don't seem to be able to find anything online about that error message other than archives of the patch submission from when it was added to the threads module and all I can get from the source of the patch itself is that it appears to come about from the operating system failing to join the thread. Your best bet for getting more help with this issue would be to ask on PerlMonks - be aware that, if you ask there, they'll likely want a small, runnable code sample which demonstrates the problem (i.e., produces the error message) with as little additional stuff around it (e.g., skip the file-reading code). Of course, one of the greatest benefits of producing minimal sample code like that is that, in the process of stripping your code down to the minimum failing case, there's a strong chance that you'll figure out the problem for yourself - I hardly ever post questions to PerlMonks any more because I usually find the answer myself while preparing the question.

In this particular case, though, I would start by seriously questioning whether you really need threads in the first place or not. I don't know enough about what you're doing with them to comment on your specific case, but I can tell you that I've been programming professionally in Perl in *nix environments since 1998 and it's been my primary language since 2004 and I've never run across a problem that called for threads as a solution.
Was This Post Helpful? 1
  • +
  • -

#5 Dface  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 18-March 12

Re: How to use wordlist file in perl script

Posted 20 March 2012 - 03:00 AM

Your answer is very informative ! I'll try to find a glitch...
I'm more familiar with C, but that not stops me to learn coding on perl.
Thank you dsherohman !
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1