LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   using perl with PNGs, Net::Ping, and CGI (https://www.linuxquestions.org/questions/programming-9/using-perl-with-pngs-net-ping-and-cgi-578618/)

cselph 08-21-2007 02:29 AM

using perl with PNGs, Net::Ping, and CGI
 
I have a web page that pulls a list of computers from a DHCP database and I would like to add a column to see if the computers were online. My idea was simple enough, a perl script that pings the host and returns either a red PNG or a green PNG...as you can see...
Code:

#!/usr/local/bin/perl -w

use GD;
use Net::Ping;
   
print ("Content-type: image/png\n\n");
$im = new GD::Image(100,100);
$white = $im->colorAllocate(255,255,255);
$im->transparent($white);
$p = Net::Ping->new("icmp", 1, 64);
my $host = $ENV{'QUERY_STRING'};
if( $p->ping($host,0.09))
{
    $colour=$im->colorAllocate(0,255,0);     
}
else
{
    $colour = $im->colorAllocate(255,0,0);
}

$im->filledEllipse(5,5,10,10,$colour);
# make sure we are writing to a binary stream
binmode STDOUT;

# Convert the image to PNG and print it on standard output
print $im->png;


However, the script does not run in a apache, as apache's user is not root and root access is required for ICMP stuff, apparently. I saw something about setuid, but I was too scared to look into it, as I am taking direct input from a user (potentially, I guess I could lock it down by IP) and don't want someone breaking out and gaining root access. But other than locking it down by IP, does anyone have a hint for me how to do this effectively? I've been using perl for about a month now, so i'm probably not familiar with a lot of the basics.

thanks

bigearsbilly 08-21-2007 05:02 AM

why not use a less restrictive ping like tcp or udp?

bigearsbilly 08-21-2007 05:06 AM

or...
write a separate ping server, run as root which you query from your CGI script.

so, you have a separate ping-ing server, you send it a hostname, it does the ping for you and replies
yes or no.

cselph 08-21-2007 11:36 AM

thanks for the replies.

I thought about writing it in 2 parts, but I couldnt figure out if that would actually be more secure. I suppose I could always just valid my input with a regular expression right? I've heard that's one of the main reasons to using perl.

I had some problems with the upd thing, probably my own fault. (It always fails) Does there need to be a port listening to reply to a UDP packet? I don't know much about networking yet...


All times are GMT -5. The time now is 07:23 AM.