App-Monport

 view release on metacpan or  search on metacpan

lib/App/Monport.pm  view on Meta::CPAN

    my $yaml = YAML::Tiny->read($conf_file);
    for my $hashref (@$yaml) {
        for my $host ( sort keys %$hashref ) {

            my $open = scan_ports($host, $verbose);
            my $expected_open = $hashref->{$host} // [];

            my @report_closed;
            for my $port ( sort @$open ) {
                push @report_closed, $port
                  unless grep $port == $_, @$expected_open;
            }

            my @report_open;
            for my $port ( sort @$expected_open ) {
                push @report_open, $port
                  unless grep $port == $_, @$open;
            }

            # print report
            print "$host\n" if @report_open or @report_closed;
            print "  $_ open\n" for @report_closed;
            print "  $_ closed\n" for @report_open;
        }
    }
}

=head2 scan_ports($host, $verbose)

Return an array reference containing list of ports open on $host.

=cut

sub scan_ports {
    my ($host, $verbose) = @_;

    print "--> scanning $host ...\n" if $verbose;
    my $np = new Nmap::Parser;

    #runs the nmap command with hosts and parses it automagically
    my $nmap = nmap_path();
    $np->parsescan( $nmap, '-Pn', $host );

    my ($h) = $np->all_hosts();
    my @ports = $h->tcp_ports(q(open));

    return \@ports;
}

#sub scan_ports {
#    my $host = shift;
#    my @open;
#    print "scanning $host: \n";
#    my $ports = default_ports();
#    for my $port (@$ports) {
#        my $socket = IO::Socket::INET->new(
#            PeerAddr => $host,
#            PeerPort => $port,
#            Proto    => 'tcp',
#            Type     => SOCK_STREAM,
#            Timeout  => 1,
#        );
#
#        if ($socket) {
#            push @open, $port;
#            shutdown( $socket, 2 );
#        }
#    }
#
#    return \@open;
#}

=head2 nmap_path()

Return absolute path to nmap executable or die.

=cut

sub nmap_path {
    my @paths = qw(
      /usr/bin/nmap
      /usr/local/bin/nmap
    );
    for my $p (@paths) {
        return $p if -x $p;
    }
    die "can't find exacutable nmap; searched @paths\n";
}

=head1 INSTALLATION

To install this module run:

 $ cpan App::Monport

or

 $ cpanm App::Monport

when using L<App::cpanminus>.

To install manually clone the L</"SOURCE REPOSITORY"> and then run (on Unix):

 perl Build.PL
 ./Build
 ./Build test
 ./Build install

For more see L<StackOverflow|http://stackoverflow.com/questions/65865/whats-the-easiest-way-to-install-a-missing-perl-module>
or L<CPAN|http://www.cpan.org/modules/INSTALL.html> instructions.

=head1 SOURCE REPOSITORY

L<http://github.com/jreisinger/App-Monport>

=head1 AUTHOR

Jozef Reisinger, E<lt>reisinge@cpan.orgE<gt>

=head1 COPYRIGHT AND LICENSE



( run in 0.551 second using v1.01-cache-2.11-cpan-c966e8aa7e8 )