Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

Re: Module Net-Ping

by Preceptor (Deacon)
on Sep 23, 2002 at 16:54 UTC ( #200162=note: print w/replies, xml ) Need Help??

in reply to Module Net-Ping

At a guess, it's because you are using UDP for your ping.
If you aim a UDP packet at a 'closed' port, then you get an ICMP 'destination port unreachable' (OTOH).
It means, that it could reach the host, but the port in question wasn't open. Suggest you either try a 'known good' udp port (hard on a mailserver) or use TCP or ICMP instead.
It's not pessimism if there is a worse option, it's not paranoia when they are and it's not cynicism when you're right.

Replies are listed 'Best First'.
packet types & ports
by Kozz (Friar) on Sep 23, 2002 at 17:04 UTC
    Preceptor: Thank you for the reply. I chose "UDP" because it is the Net::Ping's default. "TCP" likewise returns unreachable.

    If I change it to 'icmp', it DOES work, but only if effective uid is root (per the module's documentation). I wanted to run this w/o root privs, just as a plain user.

    There are indeed other services available on this server, but I don't see in the Net::Ping documentation any way to specify a different port number from the default ('echo'). Perhaps the only thing I can do is run it as root or setuid the script. But any other advice would be helpful.

    Thank you!

      On CPan there is a Net::Ping::External that relies on the binary ping command that does not need root level permissions (I presume so long as your ping command doesn't)

      It also works on a wide variety of systems

      Check it out!

        Not to be too picky, but your ping command almost certainly _does_ require root permissions.
        It's because for ICMP 'echo replies' to get back to the program, it effectively has to snoop the interface - there's absolutely no state in ICMP, so it just has to listen to see if it gets something that matches.
        Of course, since it's a compiled binary that's _generally_ trusted, that's not a huge problem. Course, you could probably just do a system("ping -c 1 $host");(syntax dependant on OS) and grep for 'bytes from'.
        It's not pessimism if there is a worse option, it's not paranoia when they are and it's not cynicism when you're right.
        blm: Thanks for the suggestion, btw!

        Originally I tried parsing the output directly from /bin/ping but the script spewed an error about execute permissions of ping from within the script. THAT's why I had pursued Net::Ping, though only the ICMP worked as root.

        I tried Net::Ping::External and it worked like a charm, no setuid bit or anything needed. Worked sweet. Thanks again.

      It's seems weird to be advocating another non-Perl solution in so short a timespan, but this is something done obscenely easy with a shell script if your ping binary has appropriate permissions (they usually do)...

      #!/bin/sh PING='/bin/ping -c 5' # FreeBSD/Linux #PING='/usr/sbin/ping' # Solaris FETCHMAIL='/usr/local/bin/fetchmail -s -t 60' $PING $hostname 1>/dev/null 2>&1 \ && $FETCHMAIL

      If you absolutely must write something in Perl, then I second blm's suggestion of Net::Ping::External (alt.).


      I'd suggest trying using netcat to test whether echo/tcp and echo/udp are actually available.
      Otherwise, you are probably looking at implementing a similar functionality using Net::Telnet (using the POP port)
      I posted a CUFP here. From which you could probably quite easily nab the 'OpenConn' sub to do what you want.
      It's not pessimism if there is a worse option, it's not paranoia when they are and it's not cynicism when you're right.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://200162]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (8)
As of 2018-06-18 19:34 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (110 votes). Check out past polls.