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

Hi perl monks,
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:
#!/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:
#!/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

Replies are listed 'Best First'.
Re^3: Usage of IO::Socket::PortState
by Anonymous Monk on Sep 04, 2015 at 04:36 UTC
    I suspect your port from the file includes a newline character and thus the check is failing.