Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: processing a module result

by GrandFather (Saint)
on May 18, 2022 at 05:56 UTC ( [id://11143957]=note: print w/replies, xml ) Need Help??


in reply to processing a module result

Reading the documentation for Masscan::Scanner may help. If it doesn't you might try showing the code you have tried using to process the results and tell us how it has failed.

You say you are not a "good programmer in perl", but I suspect that means you are not an experienced Perl programmer. If you show us some code that will help us asses what you do know and the level of help you need. Read I know what I mean. Why don't you? for some hints about how you should go about writing something to show us. Most of us will not easily be able to use the Masscan::Scanner module!

Optimising for fewest key strokes only makes sense transmitting to Pluto or beyond

Replies are listed 'Best First'.
Re^2: processing a module result
by averlon (Sexton) on May 18, 2022 at 06:12 UTC

    Hi GrandFather,

    in this case I have tried to understand the documentation but failed so far.

    $av_obj_MAS = Masscan::Scanner->new(); $av_obj_MAS->add_host($av_loc_IP); $av_obj_MAS->add_port(@av_arr_PORTS); $av_obj_MAS->sudo(1); $av_obj_MAS->verbose(1); $av_tmp_SCAN = $av_obj_MAS->scan; if ($av_tmp_SCAN) { $av_obj_SCANRESULT = $av_obj_MAS->scan_results; $av_obj_RESULT = %$av_obj_SCANRESULT{scan_results}; @av_arr_SCANRESULT = @{%$av_obj_SCANRESULT{scan_results}}; # print "Test1: " . $%av_obj_RESULT{ip}; }

    The first line to get the result from the object obviously works. It is according to the documentation.

    It looks like the second line also brings one part of the result to a variable.

    But to access the IP (and print it) and the array of port is still not working.

    The documentation of the module shows some example data structure provided as result.

    { 'scan_results' => [ { 'timestamp' => '1584816181', 'ip' => '10.0.0.1', 'ports' => [ { 'status' => 'open', 'reason' => 'syn-ack', 'port' => 443, 'proto' => 'tcp', 'ttl' => 60 } ] }, { 'timestamp' => '1584816181', 'ip' => '10.0.0.2', 'ports' => [ { 'reason' => 'syn-ack', 'status' => 'open', 'port' => 443, 'ttl' => 60, 'proto' => 'tcp' } ] }, { 'ports' => [ { 'port' => 80, 'ttl' => 60, 'proto' => 'tcp', 'reason' => 'syn-ack', 'status' => 'open' } ], 'ip' => '10.0.0.1', 'timestamp' => '1584816181' }, { 'ip' => '10.0.0.2', 'timestamp' => '1584816181', 'ports' => [ { 'port' => 80, 'ttl' => 60, 'proto' => 'tcp', 'status' => 'open', 'reason' => 'syn-ack' } ] }, { 'timestamp' => '1584816181', 'ip' => '10.0.0.3', 'ports' => [ { 'reason' => 'syn-ack', 'status' => 'open', 'proto' => 'tcp', 'ttl' => 111, 'port' => 80 } ] }, { 'ports' => [ { 'ttl' => 111, 'proto' => 'tcp', 'port' => 443, 'reason' => 'syn-ack', 'status' => 'open' } ], 'timestamp' => '1584816181', 'ip' => '10.0.0.3' } ], 'masscan' => { 'scan_stats' => { 'total_hosts' => 4, 'up_hosts' => 3 }, 'command_line' => '/usr/bin/masscan --rate 100000 + --banners -p 22,80,443,61222,25 10.0.0.2,10.0.0.1,10.0.0.3,10.0.0.4' } };

    with:

    $av_obj_SCANRESULT = $av_obj_MAS->scan_results;

    I geht the data structure into a scalar - so far so good

    now I would like e.g. to print the IP-Address as part of the data structure or move the whole result into an array or just run a foreach ... to process the results list.

    but I don't know how

    Regards Kallewirsch
      Hello averlon,

      mainly our goal is to help you in learning, not providing solutions, so I'd start with:

      • use strict; use warnings; always in your program
      this will save a lot of trouble and has not drawbacks, so let perl help you: it is amazing in this

      • Then, regarding your code:
      # doc my $mas = Masscan::Scanner->new(hosts => \@hosts, ports => \@ports, ar +guments => \@arguments); #your code $av_obj_MAS->add_port(@av_arr_PORTS);

      They are sligltly different... where? \@ports is a reference to an array while @av_arr_PORTS is an array. Look carefully at the documentation: arguments are always passed as a single scalar like in $mas->add_port(25) or as array reference like in $mas->ports(['22', '80', '443']); but never as list or array.

      • about accessing the results
      The doc says it: SCAN-RESULTS. I invite you to inspect yourself this result using the core module Data::Dumper or the CPAN one Data::Dump (I prefer the latter, but in some edge case the former is more accurate: get used to both).

      Consider the following oneliner (pay attention to windows double quotes around the code, single quote needed for Linux):

      perl -MData::Dumper -e "$hash{lvl1}[3]{lvl3}[0] = 42; print Dumper \%h +ash" $VAR1 = { 'lvl1' => [ undef, undef, undef, { 'lvl3' => [ 42 ] } ] };

      You are in a similar situation: perldsc is definitevely a good read.

      Modify your program and share your achievements: they will probably bring up new questions

      L*

      There are no rules, there are no thumbs..
      Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

      Hi

      I guess, I found a solution myself

      I will post it once it is completely working

      Regards Kallewirsch

        Crafting a good question often provides the answer you are looking for without needing to actually ask it. This is a Good Thing!

        You already have a little feedback on your solution: Always use strictures (use strict; use warnings; - see The strictures, according to Seuss). If I get time tomorrow night I'll run through your code and provide what I hope is helpful feedback.

        Optimising for fewest key strokes only makes sense transmitting to Pluto or beyond

        Hi

        here is my code:

        $av_obj_MAS = Masscan::Scanner->new(); # host given by the command line option $av_obj_MAS->add_host($av_loc_IP); # set the ports given by the command line options $av_obj_MAS->ports(\@av_arr_PORTS); $av_obj_MAS->sudo(1); $av_obj_MAS->verbose(1); # now scan $av_tmp_SCAN = $av_obj_MAS->scan; # if result is available if ($av_tmp_SCAN) { # get result from module - this then seems to be an object $av_obj_SCANRESULT = $av_obj_MAS->scan_results; # # # now I need to fetch the part of scan_results from this object # $av_obj_RESULT = %$av_obj_SCANRESULT{scan_results}; # since the scan_result is an array of values, I need to put it in +to an array @av_arr_SCANRESULT = @{%$av_obj_SCANRESULT{scan_results}}; } # now to process this array of results # this should contain the e.g. the ip-address of the computer scanned +and an array of ports found in what status ever foreach $av_tmp_STRING ( @av_arr_SCANRESULT ) { # print simply the ip-address print %$av_tmp_STRING{ip} . "\n"; # now I need to put the array of ports scanned and reported as an +result into an array @av_arr_RESULTPORTS = @{%$av_tmp_STRING{ports}}; # this array may have several entries for each port scanned # now to process the array and print the results foreach $av_tmp_STRING ( @av_arr_RESULTPORTS ) { print "Port: " . %$av_tmp_STRING{port} . "\n"; print "Status: " . %$av_tmp_STRING{status} . "\n"; } }

        I am sure there is a lot of optimization and shortening of commands possible - but one step after another

        probably it will help also others struggling with this issue - if some

        Regards Kallewirsch

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (2)
As of 2024-04-25 04:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found