A good start to help us help you better would be to show us the code that you tried but which did not work for you.

The general approach I would use would be to use a worker model like the following:

  1. Create a Thread::Queue, "$hosts". This is where the worker threads will get their units of work from.
  2. Read all the hosts into that queue, so the worker threads have something to do.
  3. Create another Thread::Queue, "$portstatus". This is where the worker threads will store their results.
  4. Create some worker threads, which will do mostly what your single-threaded version did:
    sub scan_host { while( my( $ip )= $hosts->dequeue() ) { warn "Scanning '$ip'"; ... do real work $portstatus->enqueue({ host => $ip, status => 'too lazy', }); }; } for( 1..$NUM_THREADS ) { threads->create( \&scan_host ); };
  5. Now, just read out the results as they come in:
    $hosts->end; while(my $result= $portstatus->dequeue()) { print Dumper $result; };

An easier way would be to run the nmap binary and use Nmap::Parser for the results.


In reply to Re: Making a multi-threaded port scanner in perl? by Corion
in thread Making a multi-threaded port scanner in perl? by jessica1231

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.