Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Net::Ping troubles

by Ovid (Cardinal)
on Nov 21, 2000 at 04:24 UTC ( [id://42634]=perlquestion: print w/replies, xml ) Need Help??

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

I can't seem to get the following to work on my machine:
use warnings; use strict; use Net::Ping; my @hosts = qw( www.ovidinexile.com www.yahoo.com 127.0.0.1 www.perlmo +nks.org ); my $p = Net::Ping->new(); foreach my $host ( @hosts ) { print "$host\t" . $p->ping( $host ) . "\n"; }
The output is as follows:
www.ovidinexile.com 0 www.yahoo.com 0 127.0.0.1 0 www.perlmonks.org 0
I can successfully ping all of those from the command line. My reading the POD, the holy Cookbook, and several PM posts has failed to enlighten me. Chatterbox commentary has been interesting, but also unenlighting (though I had to run to a meeting after a bit -- maybe I missed something).

Using the icmp ping gives me the following error message:

icmp socket error - Unknown error at C:\WINNT\profiles\cp\desktop\ping +.pl line 6
The tcp ping is unavailable because alarm is not supported on Windows NT (naturally, it doesn't have a decent ping program, either).

About the only thing I can think of is to use the system command and parse the response. Yuck! Anyone know what's wrong with my code?

Using WinNT 4.0 with ActiveState Perl 5.6.0.

Cheers,
Ovid

Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

Replies are listed 'Best First'.
(jcwren) Re: Net::Ping troubles
by jcwren (Prior) on Nov 21, 2000 at 04:35 UTC
    The 3 possible values for the new() are 'udp', 'tcp', and 'icmp'. If I use 'icmp' or 'tcp', it works for me, although the pings are a little squirrley because of the firewall ('icmp' works, 'tcp' does not). However, using new() with no arguments defaults to 'udp'. When explicity specifying 'udp', I get the following errors:
    Use of uninitialized value at /usr/lib/perl5/5.00503/i386-linux/Socket +.pm line 295. Bad arg length for Socket::unpack_sockaddr_in, length is 0, should be +16 at /usr/lib/perl5/5.00503/i386-linux/Socket.pm line 295.
    --Chris

    e-mail jcwren
(tye)Re: Net::Ping troubles
by tye (Sage) on Nov 21, 2000 at 04:38 UTC

    Doing TCP or UDP pings is often useless since most machines (at least these days) don't service the TCP/UDP echo services. This type of "ping" is like trying to telnet to port 7 of a machine. Most of the time this will just get you "connection refused" because nothing listens to that port on so many machines. But since UDP is not connection oriented, you can't get "connection refused" so the ping just fails.

    So you want to do Net::Ping->new("icmp"), which works for me. You need to get at 0+$! in order to understand what "unknown error" really is so we can figure out what is going wrong. Either modify Ping.pm so that it reports this or use the Perl debugger to step into Ping.pm and query what that value is. Perhaps you have to be "administrator" in order to use ICMP under WinNT?

            - tye (but my friends call me "Tye")
(Ovid) Re: Net::Ping troubles
by Ovid (Cardinal) on Nov 21, 2000 at 04:55 UTC
    Apparently, I do need to run this as administrator and do an icmp ping. Worked fine once I did that. I made the mistake of assuming that since I was told that UDP pings on our boxes were okay, that they were okay :(

    Thanks for the help! I'll break out a can of ++ when my new shipment comes in tomorrow.

    Ovid makes a note to always test assumptions.

    Cheers,
    Ovid

    Update: Anyone know why the ping request works from a command line on NT and not from a Perl script? The NT documentation states that NT's ping uses icmp.

    Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

Re: Net::Ping troubles
by little (Curate) on Nov 21, 2000 at 04:37 UTC
    out of the docs:
    Net::Ping->new([$proto [, $def_timeout [, $bytes]]]); Create a new ping object. All of the parameters are optional. $proto specifies the protocol to use when doing a ping. The current choices are ``tcp'', ``udp'' or ``icmp''. The default is ``udp''.
    Do u use the right proto ?

    Have a nice day
    All decision is left to your taste
    Update
    ack, I get old and slower
      He says he's tried them all.. With UDP he gets zeros, with ICMP he gets an error message, and with TCP he gets an error about alarm not being supported on NT.
        For me it worked fine under so are all the services available? TCP didn't want to work on Win98, so I've chosen icmp, which succeeded. ??

        Have a nice day
        All decision is left to your taste
Re: Net::Ping troubles
by Fastolfe (Vicar) on Nov 21, 2000 at 04:35 UTC
    Do you need to be running as an administrator to utilize ICMP in this fashion under NT?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://42634]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2024-04-25 06:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found