Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: System commands using CGI

by rjt (Deacon)
on Dec 03, 2012 at 06:38 UTC ( #1006804=note: print w/ replies, xml ) Need Help??


in reply to System commands using CGI

It's likely that the who program is not in the $PATH of the web server's environment. Try printing out $ENV{PATH} in your CGI program to verify this. Anyway, the more secure route is to specify the full path of the command you want to execute, usually /usr/bin/who or /bin/who (check by running which who from a shell prompt).

Second, I'm not sure what you intend to do with the text file, but if you intend to show the output inside your CGI program (or even if you don't), you have introduced a race condition by sending the output to the same text file every time. (If two visitors hit the CGI very close together, the first visitor might see the second visitor's who output.) A better way to handle this is to capture the output with qx// or backticks.

my $output = `/usr/bin/who`; print '<p>Something went horribly awry!</p>' if $?;


Comment on Re: System commands using CGI
Select or Download Code
Re^2: System commands using CGI
by nikhilB (Initiate) on Dec 03, 2012 at 06:48 UTC
    Hi rgt, Thanks for the reply. I tried using the following but it looks like the command doesnt get executed from the CGI :
    #!/usr/bin/perl print "content-type: text/html \n\n"; # SET UP THE TABLE print "<table border='1'>"; print "<th>Test</th><th>Value</th><th>Expected Value</th><th>Result</t +h>"; $b=`/usr/bin/who>>/root/abcd.txt`; print "$array[1]\n\n"; #print "VALUE IS $b\n\n"; print "<tr><td>".$b."</td>";
    When I run this from the command prompt, it the gives the correct output.

    Any idea where I am going wrong ?

    Thanks.

      A webserver with root access? Doubtful

      Your code here did not generate that output. It was hard to read without <code> tags, so I reformatted it for you:

      #!/usr/bin/perl print "content-type: text/html \n\n"; # SET UP THE TABLE print ""; print ""; $b=`/usr/bin/who&gt;&gt;/root/abcd.txt`; print "$array<a href="?node=1">1</a>\n\n"; # <== FIXME #print "VALUE IS $b\n\n"; print "";

      Your code here does not compile. You need to escape the double quotes in your print "$array... line.

      Beyond that, however, why in the world are you trying to append the output of /usr/bin/who to /root/abcd.txt? More to the point, please let us know exactly what your CGI program is supposed to do, and perhaps then we can get to the root of the problem (no pun intended... or maybe it was...).

      The lack of code tags that rjt pointed out also munges up the formatting of the replies. It looks like the empty print statements in what he posted contain table related tags, like:

      #!/usr/bin/perl print "content-type: text/html \n\n"; # SET UP THE TABLE print "<table border='1'>"; print "<th>Test</th><th>Value</th><th>Expected Value</th><th>Result</t +h>"; $b=`/usr/bin/who>>/root/abcd.txt`; print "$array<a href="?node=1">1</a>\n\n"; #print "VALUE IS $b\n\n"; print "<tr><td>".$b."</td>";
      It would make it easier to sort out the replies if you can go back and wrap your code in code tags.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1006804]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (3)
As of 2014-07-13 04:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (245 votes), past polls