Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

Net::Ping troubles

by Ovid (Cardinal)
on Nov 21, 2000 at 04:24 UTC ( #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.perlmo ); my $p = Net::Ping->new(); foreach my $host ( @hosts ) { print "$host\t" . $p->ping( $host ) . "\n"; }
The output is as follows: 0 0 0 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 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.


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 line 295. Bad arg length for Socket::unpack_sockaddr_in, length is 0, should be +16 at /usr/lib/perl5/5.00503/i386-linux/ line 295. +

    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 so that it reports this or use the Perl debugger to step into 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.


    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
    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?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://42634]
Approved by root
[marto]: Hey there
[Marshall]: hope we get a good answer to Script stopped working... this OP is making serious efforts to solve problem on his own.
[Marshall]: I've become frustrated lately with some OP's.

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (4)
As of 2018-03-18 12:53 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (230 votes). Check out past polls.