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

Hi monks,

I've found Net::Ping can't recognize IP but DBI can:

$IP = "192.168.0.1 192.168.0.2 192.168.0.3"; for my $ip_addr (split /\s/, $IP){ my $p = Net::Ping->new(); if ($p->ping( $ip_addr )) { $my_state->{IP_stat} = 'UP'} #can't ping else{ $my_state->{IP_stat} = 'unreachable'; next; } $p->close(); my $dbh = DBI->connect("dbi:" . $db_type . ":" . $ip_addr . "/" . $ser +vice_name, $sysUser, $sysPass ); #can connect
I guess Net::Ping treat varaible as number not string , but DBI don't. is there a way to resolve it ? Thanks

s

Replies are listed 'Best First'.
Re: IP address can't recognize by Net::Ping
by Corion (Patriarch) on Nov 24, 2023 at 07:10 UTC

    According to the documentation, Net::Ping tries a tcp connection to the echo port on the target machine. Do the target machines have an echo daemon running?

Re: IP address can't recognize by Net::Ping
by Marshall (Canon) on Nov 24, 2023 at 07:34 UTC
    I would try command line ping and see if that works. Also perhaps, ping 8.8.8.8 that address is one of the primary V4 addresses for goggle.com. My current firewall will not respond to pings by my intent. Even on a local 192 network, you may be trying to ping and unpingable machine. see comment by Corion.
Re: IP address can't recognize by Net::Ping
by hippo (Archbishop) on Nov 24, 2023 at 10:14 UTC

    Well done for providing some code to illustrate the problem. Unfortunately what your code demonstrates is merely that the target addresses are not responding to the Net::Ping tests, it passes no judgement on whether or not the addresses are "recognized".

    use strict; use warnings; use Test::More tests => 7; use Net::Ping; my $p = Net::Ping->new; for my $host (qw/www.perlmonks.org tcpbin.com/) { ok $p->ping ($host), "$host pinged"; } for my $host (qw/www.ibm.com www.whitehouse.gov/) { ok !$p->ping ($host), "$host blocks pings"; } for my $host (qw/lsdfskf wocka.wocka 192.168.1/) { ok !$p->ping ($host), "$host doesn't respond because it won't reso +lve"; }

    Note how the second and third sets behave the same in terms of the false return value from the ping method despite failing in different ways.


    🦛

Re: IP address can't recognize by Net::Ping
by Anonymous Monk on Nov 27, 2023 at 07:08 UTC
    By default, Net::Ping uses tcp, so you need to set $p->port_number , presumably to whatever port the database listens on.
Re: IP address can't recognize by Net::Ping
by Anonymous Monk on Nov 24, 2023 at 10:54 UTC