deelinux has asked for the wisdom of the Perl Monks concerning the following question:

Hi I want to do some port checks (http and tcp) on an array of servers

I'm still new to Perl/Programming, and found in the perldoc Net::Ping, from which I have created some ping checks, along with code found on the net, but would like some guidance on how I could ping check and then test if some specific ports are available

I have created an array to ping some servers, which is fine, but Id like to create a sub function to check an array of ports.

sub ping_servers { foreach $host (@my_servers) { my $p = Net::Ping ->new("icmp"); #create ping object my $res = $p -> ping ($host); #ping the hosts from array list $output .= "Unknown host $host\n" unless defined $resolve; if (!$res) { $output .= "$host doesn't respond to ping requests!\n" +; } else { $output .= "$host is alive.\n"; } } }

I's also like to understand what this "->" means in code?

Any know how, or pointers would be great.

Replies are listed 'Best First'.
Re: port check
by toolic (Bishop) on Nov 28, 2015 at 13:36 UTC
Re: port check
by stevieb (Canon) on Nov 28, 2015 at 15:58 UTC

    toolic's already answered the -> question, so I'll focus on the port scan here.

    You can use Nmap::Scanner for this task. I must tell you that while fudging the below example, I found errors in the docs throughout the distribution (I've made myself a reminder and I'll go through it more thoroughly later and patch them). This example is nearly copy and paste from the various SYNOPSIS from the modules.

    The most notable err in the docs is the call to get_host_list(). In the docs, they specify gethostlist(), which doesn't exist.

    Also, you *must* install nmap before installing Nmap::Scanner, and if you get any weird errors upon running your script, you may need to install XML::SAX::Expat as well (I found that solution in this post, in this thread here on PM).

    use strict; use warnings; use Nmap::Scanner; my @systems = qw(localhost other); my $scanner = new Nmap::Scanner; $scanner->tcp_syn_scan; $scanner->add_scan_port('22'); $scanner->add_scan_port('80-82'); for (@systems){ $scanner->add_target($_); } my $results = $scanner->scan; my $hosts = $results->get_host_list; while (my $host = $hosts->get_next) { print "\nOn " . $host->hostname . ": \n"; my $ports = $host->get_port_list; while (my $port = $ports->get_next) { print join(' ', 'Port', $port->portid, 'is in state', $port->state, "\n" ); } } __END__ On localhost: Port 22 is in state open Port 80 is in state closed Port 81 is in state closed Port 82 is in state closed On other: Port 22 is in state closed Port 80 is in state closed Port 81 is in state closed Port 82 is in state closed

      Many thanks!!! :-)