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

the multiple thread port scanner scripts issue

by jeremy.fang (Initiate)
on Jan 26, 2013 at 06:21 UTC ( [id://1015470]=perlquestion: print w/replies, xml ) Need Help??

jeremy.fang has asked for the wisdom of the Perl Monks concerning the following question:

There is a port scanner scripts and include thread ,but dont finished,so please someone can help me finish it.
I have many domain names in psinfo table(about 1000w) and want to scan those domain names with nmap command,then update result of scanner to table.

now has 2 question:
1、thread dont have Limit the largest number of process running,how to limit?
2、my scirpt dont update date to mssql,the nmap scan process has finshed, but the row "port" is a blank,not null。 use perl debug command ,the namp command had scaned the domain names and port had display by the script's print command ,but did not update to database.

there is thread scripts

#!/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;

there is no thread scripts


#!/usr/bin/perl use strict; use warnings; use DBI; 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 @list; while ($line = $sth->fetchrow_array()) { my $myport; my @list =`nmap $line`; foreach(@list){ if($_=~/open/g){ $_ =~ s/\/.*//g; if($myport){$myport=$myport.','.$_;chomp $mypo +rt;} else{$myport=$_; chomp $myport} } } 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; } $sth->finish;

Replies are listed 'Best First'.
Re: the multiple thread port scanner scripts issue
by roboticus (Chancellor) on Jan 26, 2013 at 14:34 UTC

    jeremy.fang:

    The reason that the date isn't being updated in your database is because you're not telling it to do so. I'd change your update statement from:

    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";

    to something like this:

    my $upd_sth = $dbh->prepare(q{ update psinfo set port=?, last_checked=getdate() where domainname=? }); $upd_sth->execute($myport, $line) or die "unable to update: name=$line! error\n$DBI::errstr\n";

    Note: I don't recall the method to get the current date/time in MSSQL, so I just guessed. I also converted your code to use placeholders, because:

    • they're safer,
    • they're easy to use
    • they make the SQL more readable (in my opinion)

    I'm sorry, but I can't quite make out all of your question, nor can I make much sense from your code, so that's about all I can offer for now.

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

      thanks your reply
      can you added the thread into the scripts
Re: the multiple thread port scanner scripts issue
by NetWallah (Canon) on Jan 26, 2013 at 17:07 UTC
    You are misusing/abusing threads by creating a new thread for each activity instance.

    Consider using BrowserUk's excellent threads::Q implementation from this node.

                 Most people believe that if it ain't broke, don't fix it.
            Engineers believe that if it ain't broke, it doesn't have enough features yet.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (7)
As of 2024-03-28 18:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found