Hello Perl Monks. I'm trying to make a portscanner that will take values from a MySQL database. I was able to make it without SQL support with this code:

%h; $h{'Echo [Returns what is sent to server]'} = {service => ['7']}; $h{'Discard [A sink, like /dev/null]'} = {service => ['9']}; foreach $key(keys %h) {foreach $service (@{$h{$key}{'service'}}) {if($khost=~/^$service/) {chop($service) if ($service =~/\.$/); print "\n[!] Port: [$start_port] Service: $key"; print O "[!] Port: [$start_port] Service: $key\n"; $start_port++}}}}}

Now I'm trying to do the same thing but with DBI. I have a table called "vulnerabilities" and I want to create a hash in perl, populated by the 'port', 'name', 'comment' that can be found on the vulnerabilities table. Untill now my code is something like this:

$sql = "select * from scanner.vulnerabilities"; $sth = $dbh->prepare($sql); $sth->execute or die "SQL Error: $DBI::errstr\n"; my %ref; while (my $row = $sth ->fetchrow_hashref() ){ $ref{ $row ->{'port'}} = $row ->{'Name'} } foreach $key(keys %ref) {foreach $port (@{$ref{$key}{'port'}}) {if($khost=~/^$port/) {chop($port) if ($port =~/\.$/); print "\n[!] Port: [$start_port] Service: $key"; print O "[!] Port: [$start_port] Service: $key\n"; $start_port++}}}}}

The way it is does not working as it does not log the results. In small, I want to create a hash from MySQL that can do what the original program does. Although I have read a hell lot of resources it seems that I miss something. This program is for a college project and I cannot seem to be able to find the answer anywhere. Thank you in advance.


In reply to Hash from mySQL by CreationP

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.