Discussion:
Using Net::Ping in a CGI script
Nathan Gibbs
2006-12-22 18:17:33 UTC
Permalink
The Program:
I have a Perl script that reads a list of hosts from a file, uses
Net::Ping to check them with the icmp protocol, then writes an HTML
status report into a file on my apache server. This script is run every
minute by cron.

The Original Problem:
I usually use the status report after a split second disturbance in
electrical power, to get a quick overview of the damage to my network.
If the script ran right before the disturbance, I have to wait for it to
run again to get an accurate picture of what is going on.

The Original Solution:
The plan was to turn this script into a cgi, so that I wouldn't
need cron to drive it, and so that the results would always be current.
Basically it would only run when I loaded in my browser, as opposed to
all the time.

The current Problem:
Net::Ping won't do icmp as a non root user. I RTM, which said that
if I suid'ed ( Hmmm is that a word ?? )the script it would work, however
it still refuses to work.

I must be missing some detail here.
What am I missing?


Thanks
zentara
2006-12-23 15:32:01 UTC
Permalink
Post by Nathan Gibbs
Net::Ping won't do icmp as a non root user. I RTM, which said that
if I suid'ed ( Hmmm is that a word ?? )the script it would work, however
it still refuses to work.
I must be missing some detail here.
What am I missing?
You could ask this on http://perlmonks.org

Just guessing, I would guess that your suid isn't setup right,
remember, a web client comes in as something like "nobody:nogroup",
so have you looked into that? It sounds insecure to suid anything
from cgi to root. Another possible problem is that there is another
program which may be called by Net::Ping, and that would need to be
suid'd too. Or the system disallows suid scripts.
I think most people now recommend using sudo, instead of suid.
Google for sudo for examples of setting it up.

Why not just use a non-priviledged ping?

#!/usr/bin/perl
use strict;
use warnings;
use Net::Ping;

#tcp dosn't require root privileges
my $p = Net::Ping->new('tcp');
$p->tcp_service_check(1);

my $host = "192.168.0.1";
my $port = 80;

$p->{'port_num'} = $port;
print "The service on $host port $port is ",
($p->ping($host) ? "up" : "down"), ".\n";
__END__
--
I'm not really a human, but I play one on earth.
http://zentara.net/japh.html
--
To unsubscribe, e-mail: beginners-cgi-***@perl.org
For additional commands, e-mail: beginners-cgi-***@perl.org
<http://learn.perl.org/> <http://learn.perl.org/first-response>
Nathan Gibbs
2006-12-23 18:30:31 UTC
Permalink
Post by zentara
Post by Nathan Gibbs
Net::Ping won't do icmp as a non root user. I RTM, which said that
if I suid'ed ( Hmmm is that a word ?? )the script it would work, however
it still refuses to work.
I must be missing some detail here.
What am I missing?
You could ask this on http://perlmonks.org
Just guessing, I would guess that your suid isn't setup right,
remember, a web client comes in as something like "nobody:nogroup",
so have you looked into that? It sounds insecure to suid anything
from cgi to root. Another possible problem is that there is another
program which may be called by Net::Ping, and that would need to be
suid'd too. Or the system disallows suid scripts.
I think most people now recommend using sudo, instead of suid.
Google for sudo for examples of setting it up.
Why not just use a non-priviledged ping?
SNIP
__END__
Duuh, Why didn't I think of that, Doh!,Doh!;Doh!
The village idiot has been spotted.

You are absolutely right.
I wasn't too excited about the suid option either. However, for
some reason I incorrectly thought that the other ping methods would
return an unreachable status if their was nothing running on the
host:port I pinged. Tcp works just fine. It would be nice if the icmp
ping method worked as a non root user, just from a network utilization
point of view, but it really doesn't make much of a difference.

Thanks for the advice.
--
To unsubscribe, e-mail: beginners-cgi-***@perl.org
For additional commands, e-mail: beginners-cgi-***@perl.org
<http://learn.perl.org/> <http://learn.perl.org/first-response>
Loading...