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

All,
I am trying to create a perl ping utility which can be used in both unix and windows boxes.

I am seeing an issue where if I use
Net::Ping->new();
I am not able to check windows hosts

If I use Net::Ping->new("icmp");
it works fine in my windows box but not in unix as I cannot run as root.

Net::Ping->new("tcp", 2); does not work on my windows machines.

Is there any way I can have a common script which runs on both windows and unix and checks successfully for hosts on both unix and windows.


Thanks,
Sunil.

Title edit by tye

  • Comment on net::ping not working for windows hosts

Replies are listed 'Best First'.
Re: net::ping ont working for windows hosts
by rob_au (Abbot) on Jan 09, 2003 at 10:51 UTC
    Have a look at Net::Ping::External on CPAN (or an older version here on PerlMonks) which employs the system binaries for performing the task of pinging hosts that do not usually require priviledged access - This module has been tested to work on Windows, Linux, OpenBSD, Solaris and IRIX.

     

    perl -le 'print+unpack("N",pack("B32","00000000000000000000001000010011"))'

Re: net::ping not working for windows hosts (reruns)
by tye (Sage) on Jan 09, 2003 at 17:35 UTC

    The somewhat old Net::Ping, the mini series will probably help you understand the problem and several of the options for working around it.

                    - tye
Re: net::ping not working for windows hosts
by theorbtwo (Prior) on Jan 09, 2003 at 13:45 UTC

    I'm not quite sure what you mean here, but the if the windows boxes you're looking at have firewalls, they're likely to not show up on ICMP pings -- most windows firewalls seem to block them by default. Then again, so does Linksys' firewall. As to port 2, I don't happen to recall what it is, but doubt windows has a daemon answering on it by default, which could explain it.

    Update: I'm informed (by gjb, thanks) that the 2 isn't a port number, it's a timeout.


    Warning: Unless otherwise stated, code is untested. Do not use without understanding. Code is posted in the hopes it is useful, but without warranty. All copyrights are relinquished into the public domain unless otherwise stated. I am not an angel. I am capable of error, and err on a fairly regular basis. If I made a mistake, please let me know (such as by replying to this node).

Sloppy, but simple
by logan (Curate) on Jan 09, 2003 at 18:09 UTC
    if ($^O =~ /WIN32/) { Net::Ping->new("icmp"); etcetera } elsif ($^O =~ /(irix | linux | SunOS | yougettheidea) /) { Net::Ping->new("tcp", 2); etcetera }
    I realize the syntax may not be correct as I rarely code perl for Windows, but the logic should be sound. Also, I'm guessing you have a limited number of unix os to deal with. A cleaner regex for the unix part is up to you.

    -Logan
    "What do I want? I'm an American. I want more."

      Why not something like

      unless ($^O =~ /WIN32/) { Net::Ping->new("tcp", 2); etcetera } else { Net::Ping->new("icmp"); etcetera }
      Untested..

      -----
      Of all the things I've lost in my life, its my mind I miss the most.