sidhartha has asked for the wisdom of the Perl Monks concerning the following question:

Just wondering why my cgi script refuses to print it's output to a webpage. Works fine from the console. I've stripped it down to try and troubleshoot but now turn to the wise for a little help.
#!/usr/bin/perl -w print "Content-type:text/html\n\n"; @swalk = `snmpwalk -v1 -Ov -c syscomstr3 sys1.stolarski.org HOST-RESOU +RCES-MIB::hrSWInstalledName`; $blah=test; foreach $swalk (@swalk) { print $swalk; } print "blah";
Sure enough my webpage sticks it's tongue out at me and just says blah.

Replies are listed 'Best First'.
Re: Newbie cgi question
by Ovid (Cardinal) on Jan 29, 2003 at 18:21 UTC

    Gilimanjaro's point about using CGI is doubly made by the typo in your header. There should be a space after the colon and, as a result, you have no guarantee that the Web browser will parse things correctly. Further, if you're not using HTML, you may as well specify a plain text content type:

    use CGI qw(:standard); print header('text/plain');

    See my .sig for the location of my CGI course. You might find it useful.

    Cheers,
    Ovid

    New address of my CGI Course.
    Silence is Evil (feel free to copy and distribute widely - note copyright text)

      Cool, I will definitely give it a read. Cheers
Re: Newbie cgi question
by sidhartha (Acolyte) on Jan 29, 2003 at 20:10 UTC
    Thanks to all, it turns out putting the full path to snmpwalk fixed the problem. Thanks for all the help, I will definitley take all the advice to heart.
Re: Newbie cgi question
by JamesNC (Chaplain) on Jan 29, 2003 at 18:17 UTC
    Your script runs as a different user when it is executed via the Web Browser. That is why you may notice Net::Ping functionality work when you test it from the console, but not from the Web Browser. This is *part* of the problem... To fix this, you have to make your script executable by someone who has those rights... in Win32 IIS he is usually called IUSR+name of your web server. You do that in the IIS Admin tool. The other part of this problem may be that the path to your system command is unknown to the console. *This will work on Win32
    #!/perl/bin/perl use strict; #use CGI qw(:all); #Install this module! #print header; #Life will get much easier! print "Content-type: text/html\n\n"; # Or you can hand code like the above all you want my @output = `echo %path%`; # my @output = `set`; #see all the goodies print "<b>Your web browsers path:</b><br> @output";
    Name the file path.cgi or whatever... If your executable is not in your path.. viola` you have found why it doesn't work. Making the script run as a user who has that in his path, or physically moving the executable or a link to it in /perl/bin will also let you do it.... Hope this helps! :-) Revised: Added Win32 statement
Re: Newbie cgi question
by Gilimanjaro (Hermit) on Jan 29, 2003 at 17:59 UTC
    First of all, I'd suggest doing:

    use CGI qw(:all); print header;

    Instead of the manual header-print...

    The actual problem is probably that your webserver doesn't have the correct permissions to execute the snmpwalk command. Furthermore why the assignment to $blah?

    But the permission thing is probably it...

Re: Newbie cgi question
by OM_Zen (Scribe) on Jan 29, 2003 at 18:15 UTC
    Hi ,

    use CGI qw(:all); print "Content-type : text/html\n\n"; # print $header; # all your lines come here


    and run the cgi from commandline and check if the snmpwalk returns or is the permission granted for your group or users ,the permission to run the snmpwalk can be that which does not let you get in the foreach loop and print stuff , so look at these changes intiallay and you may have the $swalk printed to the STDOUT , I guess