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

Hi, I'm very new to perl and am trying to use it to do some nifty stuff. I have a script that telnets to a given port on a machine, gets a number and then processes it into an array. After going through a host of machines it returns the whole thing with HTML. This works when run in command line but no values are shown if run from browser. I'm baffled, no errors in the logs to help me and diagnostics and -w don't help at all.. If anyone could take a look at this and tell me if there is some beginners error here I should slap myself for.
#!/usr/bin/perl BEGIN { $|=1; print "Content-type: text/html\n\n"; use CGI::Carp qw(fatalsToBrowser); } $kjarnavelar = "9"; $i = "0"; $rbports = "0"; $oraports = "0"; @host = ("x00802","x00803","x00804","x00902","x00903","x00904","xb0011 +2","xb00113","xb00114"); foreach $host (@host) { $rbport = `/usr/bin/telnet $host 1367 |egrep -v "Trying|Connec +|Escape"`; $rbarray[$i] = "$rbport"; $oracle = `/usr/bin/telnet $host 1371 |egrep -v "Trying|Connec +|Escape"`; $oraarray[$i] = "$oracle"; $oraports = $oraports+$oracle; $rbports = $rbports+$rbport; $i++; } print "<HTML>"; print "<TABLE BORDER=1>"; print "<TR><TD><B>Host</TD><TD><B>RB Ports Active</TD><TD><B>Oracle Po +rts Active</TD></TR>"; for($start=0;$start<=$kjarnavelar;$start++) { print "<TR><TD>$host[$start]</TD><TD>$rbarray[$start]</TD><TD> +$oraarray[$start]</TD></TR>"; } print "</TABLE>"; print "<TABLE BORDER=1>"; print "<TR><TD><B>Samtals RB Port</TD><TD><B>Samtals Oracle Port</TD>< +/TR>"; print "<TR><TD>$rbports</TD><TD>$oraports</TD></TR>"; print "</TABLE>"; print "</HTML>";
  • Comment on CGI script returns no values in browser but is ok on command line?
  • Download Code

Replies are listed 'Best First'.
Re: CGI script returns no values in browser but is ok on command line?
by wazzuteke (Hermit) on Jan 25, 2007 at 19:49 UTC
    This may or may not help fix the issue, but the fatalsToBrowser will be much more verbose with the use of strict and warnings. Usually something most beginners either forget or sometimes don't realize are there.

    You will have to define your variables (because of strict), yet the modules together will show you *everything* that's syntactically wrong.

    In your example:
    #!/usr/bin/perl # Use the modules here! use strict; use warnings; BEGIN { $|=1; print "Content-type: text/html\n\n"; use CGI::Carp qw(fatalsToBrowser); } $kjarnavelar = "9"; $i = "0"; $rbports = "0"; $oraports = "0"; @host = ("x00802","x00803","x00804","x00902","x00903","x00904","xb0011 +2","xb00113","xb00114"); foreach $host (@host) { $rbport = `/usr/bin/telnet $host 1367 |egrep -v "Trying|Connec +|Escape"`; $rbarray[$i] = "$rbport"; $oracle = `/usr/bin/telnet $host 1371 |egrep -v "Trying|Connec +|Escape"`; $oraarray[$i] = "$oracle"; $oraports = $oraports+$oracle; $rbports = $rbports+$rbport; $i++; } print "<HTML>"; print "<TABLE BORDER=1>"; print "<TR><TD><B>Host</TD><TD><B>RB Ports Active</TD><TD><B>Oracle Po +rts Active</TD></TR>"; for($start=0;$start<=$kjarnavelar;$start++) { print "<TR><TD>$host[$start]</TD><TD>$rbarray[$start]</TD><TD> +$oraarray[$start]</TD></TR>"; } print "</TABLE>"; print "<TABLE BORDER=1>"; print "<TR><TD><B>Samtals RB Port</TD><TD><B>Samtals Oracle Port</TD>< +/TR>"; print "<TR><TD>$rbports</TD><TD>$oraports</TD></TR>"; print "</TABLE>"; print "</HTML>";
    Hope that helps!

    ---------
    perl -le '$.=[qw(104 97 124 124 116 97)];*p=sub{[@{$_[0]},(45)x 2]};*d=sub{[(45)x 2,@{$_[0]}]};print map{chr}@{p(d($.))}'
      additionally you should tell the browser of what content-type the response is. try sending a 'text/html' first before you print the document.
Re: CGI script returns no values in browser but is ok on command line?
by dragonchild (Archbishop) on Jan 25, 2007 at 19:52 UTC
    If it works on the commandline, then the problem is in the webserver's setup. Some things to look for:
    • Is your webserver in a chroot jail? If so, it may not be able to find /usr/bin/telnet or egrep.
    • Is your CGI script setup correctly in a directly that allows CGI scripts?
    • Do you have a "Hello, World" test CGI that works?

    My criteria for good software:
    1. Does it work?
    2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?
      scrict is no in with all variables defined under the my command as thus
      use strict; use CGI qw(:standard); my ($oracle,@oraarray,@rbarray,$rbport,$host,@host,$kjarnavelar, $i, $ +start, $rbports, $oraports, $rbarray, $oraarray);
      I also tried defining a $world variable, assigning Hello to it and printing it in the table and that works fine. The webserver is not chrooted and I'm running the script in commandline as the same user as the webserver runs on and it still only works in command line.
        In contrast is probably best to explain The HTML code is presented but without the values I insert into the arrays.. The problem seems to be that the foreach loop doesn't run or the telnet doesn't run and no values are generated.
Re: CGI script returns no values in browser but is ok on command line?
by leocharre (Priest) on Jan 26, 2007 at 16:32 UTC

    Check permissions. for group and others
    # ls -l ./script.cgi

    Check who the cgi runs as.
    add a line somewhere that says
    print STDERR 'i am running as: '. `whoami`;
    go to your terminal and do :
    # tail -f /var/log/httpd/error_log then call the script from the browser- you'll see the messages as they come out.
    Keep in mind your log may be elsewhere, but should be easy to find. If you are using ssl, it is in ssl_error or so.

    Sometimes the script *must* be named whatever.cgi, .pl extennsion will not run. Depends on server setup.

    When you use the cli, the script is running as the invokee (unless with sudo, but that's kinda outta scope). That means if you log in as joe, and you run sue.pl, sue.pl runs with joe's permissions.

    But when you call this via a browser... aha.. it may not be running as 'joe', it may be running as 'apache' or "sue". And apache and "sue" may not have the permissions to do what your script is trying to do- maybe even run!

    Where is the script? It may have to be in cgi-bin, some servers are configured to demand that. If so, make sure this is there.
    You can make a cgi-bin dir alongside public_html(like) and then make a soft link in public_html/cgi-bin to that one.. that may work it in such case.

Re: CGI script returns no values in browser but is ok on command line?
by Anonymous Monk on Jan 26, 2007 at 09:55 UTC
      I managed to resolve the issue using the Net::Telnet module.. Thank to those that helped :D