CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Usage of IO::Socket::PortState

 



perl_dedicted
New User

Jun 20, 2015, 12:24 AM

Post #1 of 8 (2558 views)
Usage of IO::Socket::PortState Can't Post

Hi perl gurus,
I am a newbie in the forum. I was following the forum and today finally I became member of this fantastic forum.
I was trying out the module "IO::Socket::PortState qw(check_ports);" It is perfectly working without reading a file:


Code
#!/usr/bin/perl -w 
use strict;
use IO::Socket::PortState qw(check_ports);

my $proto = 'tcp';
my $port = '8307';
my $address = '127.0.0.1';
my %hash = ();

#########This works ########################################

my($section, $ping_timeout, %porthash);
$porthash{$proto}{$port}{'name'} = $section;
print "here is the $port";
check_ports($address, $ping_timeout, \%porthash);

my $open = $porthash{$proto}{$port}{'open'};
if ($open) {
print "alive\n";
}
else {
print "dead\n";
}
#################################################################


output is :
"alive"
and the text file contains:
port1 = 8307
port2 = 15677



But when I want to read my ports from a file, results are for each port "dead",
Here is the code:


Code
#!/usr/bin/perl -w 
use strict;
use IO::Socket::PortState qw(check_ports);

my $proto = 'tcp';
#my $port = '8307';
my $address = '127.0.0.1';
my %hash = ();
open( OUT, "<location of the file>" )
|| die "Problems during the writing $!";

while (<OUT>) {
%hash = ();
my ( $key, $port ) = split( '=', $_ );
$hash{$key} = $port;

my ( $section, $ping_timeout, %porthash );
$porthash{$proto}{$port}{'name'} = $section;
check_ports( $address, $ping_timeout, \%porthash );
my $open = $porthash{$proto}{$port}{'open'};
if ($open) {
print "port : $port alive\n";
}
else {
print "port : $port dead\n";

}

}



output is :
port : 8307
dead

I really don't get it. why the port 8307 (listening) is working with the previous script and not when I read all those ports from a file?


Could you help me out from this issue?

Thanks in advance


(This post was edited by FishMonger on Jun 22, 2015, 1:38 PM)


FishMonger
Veteran / Moderator

Jun 20, 2015, 7:36 AM

Post #2 of 8 (2551 views)
Re: [perl_dedicted] Usage of IO::Socket::PortState [In reply to] Can't Post

When reading from a file, each line will include the line terminator which needs to be removed. The chomp function used to do that removal.
http://perldoc.perl.org/functions/chomp.html


Code
while (<OUT>) { 
chomp;
%hash = ();
my ( $key, $port ) = split( /=/, $_ );
$hash{$key} = $port;



perl_dedicted
New User

Jun 22, 2015, 9:32 AM

Post #3 of 8 (2501 views)
Re: [FishMonger] Usage of IO::Socket::PortState [In reply to] Can't Post

Correct. I tried again but when I read all ports from a file, strange enough I don't get same result as when I do it just with one port. I still don't get where is the problem?
Thanks for the help.


Laurent_R
Veteran / Moderator

Jun 22, 2015, 9:55 AM

Post #4 of 8 (2498 views)
Re: [perl_dedicted] Usage of IO::Socket::PortState [In reply to] Can't Post

Can you show an example of different output?


perl_dedicted
New User

Jun 22, 2015, 9:59 AM

Post #5 of 8 (2495 views)
Re: [Laurent_R] Usage of IO::Socket::PortState [In reply to] Can't Post

I already indicated in my first thread.
without file for the port 8307:
output is :
"alive"

with file for 8307:
output is :
"dead"


FishMonger
Veteran / Moderator

Jun 22, 2015, 2:10 PM

Post #6 of 8 (2483 views)
Re: [perl_dedicted] Usage of IO::Socket::PortState [In reply to] Can't Post

Did you add the chomp function like I showed?


Code
    %hash = ();  
my ( $key, $port ) = split( '=', $_ );
$hash{$key} = $port;

That hash is never used, so why is it being declared and assigned?


Quote
and the text file contains:
port1 = 8307
port2 = 15677

Based on your split statement above, $port will have a leading space before the port number which might cause a problem if the module doesn't handle it correctly. Change the split statement to this:

Code
my ( $key, $port ) = split( /\s*=\s*/, $_ );



Code
    my ( $section, $ping_timeout, %porthash );  
$porthash{$proto}{$port}{'name'} = $section;

$section was never assigned any value (i.e., it's values is undef), so why are you using it to assign a hash value?

That module hasn't been maintained/updated for the last 10 years. I would recommend using a more up to date module such as Net::Ping
http://search.cpan.org/~smpeters/Net-Ping-2.41/lib/Net/Ping.pm


perl_dedicted
New User

Jun 22, 2015, 2:20 PM

Post #7 of 8 (2481 views)
Re: [FishMonger] Usage of IO::Socket::PortState [In reply to] Can't Post

thanks for your help. I will try your suggestions and I will give asap response. You told me that "That module hasn't been maintained/updated for the last 10 years" how can I know that module hasn't been maintained and it's not useful. I will also check about net::ping.

thank you for all your help. much appreciated.


FishMonger
Veteran / Moderator

Jun 22, 2015, 3:13 PM

Post #8 of 8 (2478 views)
Re: [perl_dedicted] Usage of IO::Socket::PortState [In reply to] Can't Post

Look up the module on CPAN, it will give you the date the last time it was update.
http://search.cpan.org/search?query=IO%3A%3ASocket%3A%3APortState&mode=all

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives