Beefy Boxes and Bandwidth Generously Provided by pair Networks Frank
Perl-Sensitive Sunglasses
 
PerlMonks  

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 ( #1015161=note: print w/ replies, xml ) Need Help??

Comment on Re^2: how to write thread which was multithreaded port scan scripts for nmap
Re^3: how to write thread which was multithreaded port scan scripts for nmap
by vinoth.ree (Vicar) on Jan 24, 2013 at 12:58 UTC

    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;

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1015161]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (3)
As of 2014-04-20 19:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (487 votes), past polls