mojo-jojo has asked for the wisdom of the Perl Monks concerning the following question:

I am writing a perl CGI script which will ssh to different unix servers and get their space details using df -h command. The script works fine when I run it on the server but does not display anything when executed from a html page. The code is given below, request assistance.

#!/usr/bin/perl -w use strict; use warnings; use CGI; my $q = CGI->new(); print $q->header(); my @var = `ssh root\@lindpi001 \"df -hP | tr -s ' '\"`; print "<h1>Head</h1>"; my $varlength = @var; print "<h2>Length $varlength</h2>"; for (my $i = 0; $i < $varlength; $i++) { print "\n<b>$var[$i]</b>"; }

The lenghth @var array is zero when executed from an html page.

Replies are listed 'Best First'.
Re: cgi script to retrieve disk space of unix servers
by moritz (Cardinal) on Aug 04, 2011 at 08:39 UTC

    Is there anything in the error log file of the web server? What about syslog?

    Does the script work fine if you call it from the same user ID as the webserver uses?

    Is selinux or another security enhancement enabled on the webserver?

    Is the response from the CGI script rather slow?

    Update: You might also consider collecting statistics such as disc usage with tools like collectd or nagios, and then query the collected statistics in your CGI script.

Re: cgi script to retrieve disk space of unix servers
by duyet (Friar) on Aug 04, 2011 at 10:08 UTC
    There are 2 things you might need to check:
    • You need change the permission of your script to executable, ie. chmod +x scriptname.pl
    • Check if the webserver has permission to execute "ssh root@lindpi001". It might be running under another account and not root.
    You also might want to add an "<br>" to the string to be printed.
      Hey thx a lot .. The webserver was running with the user id apache which did not have keys to ssh to the machine. I have put the .ssh folder in the home of user apache and it is working now. Thanks again.
        You really should not be letting the apache user have root keys to machines. That's a huge security risk. You should be doing it as a non root user that has limited access to the machine. Also, SNMP works very well for this and you could easily hack something together that does an SNMP walk on the host via cgi. TMTOWTDI.
Re: cgi script to retrieve disk space of unix servers
by locked_user sundialsvc4 (Abbot) on Aug 04, 2011 at 23:09 UTC

    I rather idly wonder if, in the bigger picture of things, you might be wandering into the realm of what Nagios (http://www.nagios.org) is already doing (very well)...

    I’m all for doing cool things in Perl ... but not things that have already been done?

      We are evaluating nagios and use it for our project, as there is no point reinventing the wheel. Thanks.