http://qs321.pair.com?node_id=200166


in reply to Re: Module Net-Ping
in thread Module Net-Ping

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!

Replies are listed 'Best First'.
Nonroot ping with Net::Ping::External (was: Re: packet types & ports)
by blm (Hermit) on Sep 23, 2002 at 17:34 UTC

    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!

    --blm--
      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.
        Not to be too picky, but your ping command almost certainly _does_ require root permissions.

        Yes, ping needs root privileges and yes, blm's statement about the "binary ping command that does not need root level permissions" suggested otherwise. However, I think what he meant is that a user usually doesn't have to have root privs to execute ping. This is true because ping generally has its setuid bit on and is owned by root.

        -sauoq
        "My two cents aren't worth a dime.";
        
      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.

Re: packet types & ports
by Kanji (Parson) on Sep 23, 2002 at 17:48 UTC

    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.).

        --k.


Re: packet types & ports
by Preceptor (Deacon) on Sep 23, 2002 at 17:13 UTC
    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.