http://www.perlmonks.org?node_id=1015152


in reply to how to write thread which was multithreaded port scan scripts for nmap

I am not sure, I understand your requirement clearly!

What I guess is,you have many domain names in psinfo table and want to scan those domain names with nmap command and you are currently scanning through each domain one by one with while loop. Now you want to start thread process for each domain name to scan with nmap and update back to the psinfo table ?

Please confirm is this your requirement? So that we can help you better!

  • Comment on Re: how to write thread which was multithreaded port scan scripts for nmap
  • Download Code

Replies are listed 'Best First'.
Re^2: how to write thread which was multithreaded port scan scripts for nmap
by Anonymous Monk on Jan 24, 2013 at 12:28 UTC

    thanks your reply yes,you are right

      Ok,fine. Then include the threads module into your code as

      use threads;

      within the while loop,create new thread for each domain name.

      while ($line = $sth->fetchrow_array()) { #Your code goes here my $new_thread = threads->new(\&scan_domain, $line);</b> } sub scan_domain { my $domain_name = shift; print "started thread for $domain_name\n"; my @list =`nmap $line`; foreach(@list){ if($_=~/open/g){ $_ =~ s/\/.*//g; if($myport) {$myport=$myport.','.$_;chomp $myport;} else{$myport=$_; chomp $myport} }

      Even you can return the $myport from the subroutine and update the table outside the subroutine or even you can have that table updating script inside the subroutine, its your wish.

      If you want these threads to run as daemon process do not return from the subroutine use infinite loop to process in a frequent time.

      Update:

      Included threads cpan link

        thanks

        In order to improve the progress of the scan,i dont have enough ability to finish ,so Are trying to learn perl.

        i hope you will help me finish it,Write a complete script.

        thank you very much.

        now has 2 question
        1、thread dont have Limit the number of running,how to limit?
        2、my scirpt dont to update date to mssql
        the nmap scan process has finshed, but the row "port" is a blank,not null。
        #!/usr/bin/perl use strict; use warnings; use DBI; use threads; my $dsn = 'DBI:Sybase:server=sql1'; my $dbh = DBI->connect($dsn, "test", 'oray.com'); die "unable to connect to server $DBI::errstr" unless $dbh; $dbh->do("use portscan"); my $query = "SELECT domainname FROM psinfo"; my $sth = $dbh->prepare ($query) or die "prepare failed\n"; $sth->execute() or die "unable to execute query $query error $DBI::e +rrstr"; my $line; my $myport; while ($line = $sth->fetchrow_array()) { my $new_thread = threads->new(\&scan_domain, $line); print "$myport\n"; #for test my $upd_sth = $dbh->prepare("update psinfo set port=\'$myport\' where +domainname=\'$line\'"); $upd_sth->execute() or die "unable to execute update line where name i +s $line! error $DBI::errstr"; $upd_sth->finish; } sub scan_domain { my $s_myport; my $domain_name = shift; print "started thread for $domain_name\n"; my @list =`nmap $line`; foreach(@list){ if($_=~/open/g){ $_ =~ s/\/.*//g; if($s_myport) {$s_myport=$s_myport.','.$_;chomp $s_myport;} else{$s_myport=$_; chomp $s_myport} } } $myport=$s_myport; } $sth->finish;