simple Perl CGI scripts help

C

CAT

I wrote a simple perl scripts to run a scripts remotely in another
machine and copy the result back to local server and show it on the
web browser. It will only work on command line (Will show nothing on
browser ) if I defined "APP_ROOT=/opt/APP/server/app" in the remote
scripts.
If I remove this line, it just works fine. (show the result fast on my
IE or Firefox browser)
APP_ROOT is required for me. Then I unset it by put "UnsetEnv
APP_ROOT" in my httpd.conf, however, it still refuses to work.

Please help.

root@localbox# cat showremote.cgi
#!/usr/bin/perl
##
## printedi -- CGI program which just prints app check information
##

print "Content-type: text/plain\n\n";

#This line generates check file
system ("ssh 10.1.1.10 /export/home/admin/check.sh");

#This line copy the file over to localbox
system ("scp 10.1.1.10:/tmp/check.txt /tmp");

# Name the file
$file1 = '/tmp/check.txt';

print scalar(localtime);

open(INFO1, $file1); # Open the file
@lines1 = <INFO1>; # Read it into an array
close(INFO1); # Close the file
print @lines1;
system (`/usr/bin/rm /tmp/check.txt`);


root@remotebox# cat check.sh
#!/usr/bin/ksh
APP_ROOT=/opt/APP/server/app
export APP_ROOT
TODAY=`date`
echo $TODAY > /tmp/check.txt
echo ""
/opt/APP/server/app /bin/app status | grep NOT-RUNNING >> /tmp/
check.txt
 
G

Gunnar Hjalmarsson

CAT said:
open(INFO1, $file1); # Open the file

You should check the return value from open.

open INFO1, $file1 or die "Couldn't open $file1: $!";
 
A

A. Sinan Unur

I wrote a simple perl scripts to run a scripts remotely in another
machine and copy the result back to local server and show it on the
web browser. It will only work on command line (Will show nothing on
browser ) if I defined "APP_ROOT=/opt/APP/server/app" in the remote
scripts.
If I remove this line, it just works fine. (show the result fast on my
IE or Firefox browser)
APP_ROOT is required for me. Then I unset it by put "UnsetEnv
APP_ROOT" in my httpd.conf, however, it still refuses to work.

Your description is confusing and makes it sound like you have a web
server configuration issue. If that is the case, you should post your
question in an appropriate newsgroup. However, your whole description
might be a red herring.
Please help.

root@localbox# cat showremote.cgi

If I understand correctly, you are running this script as root when you
test it from the command line. The web server will be running under a
different user id with different privileges and different (or no) home
directory.

#!/usr/bin/perl
##
## printedi -- CGI program which just prints app check information
##

print "Content-type: text/plain\n\n";

I am not sure if it is a good idea to print headers when there is
nothing to show at the point they are printed.
#This line generates check file
system ("ssh 10.1.1.10 /export/home/admin/check.sh");

And, how is the ssh login to the remote machine authorized? If you are
using keys rather than password authentication, the web server is not
going to have access to the keys you have set up for the root user.

Incidentally, you should bypass the shell and call ssh and scp directly
by using the list form of the system call.
#This line copy the file over to localbox
system ("scp 10.1.1.10:/tmp/check.txt /tmp");
Similarly.


# Name the file
$file1 = '/tmp/check.txt';

A better program would have (untested):

my %config = (
remote_host => '10.1.1.10',
remote_program => '/export/home/admin/check.sh',
remote_file => '/tmp/check.txt',
local_file => '/tmp/check.txt',
);

system ssh => $config{remote_host}, $config{remote_program};

system scp => "$config{remote_host}:$config{remote_file}",
$config{local_file}

You should, of course, check if the system calls succeeded.
print scalar(localtime);

You probably want a newline after this.
open(INFO1, $file1); # Open the file

open my $info1, '<', $config{local_file}
or die "Cannot open '$config{local_file}': $!";
@lines1 = <INFO1>; # Read it into an array

Don't do that. There is no need to slurp the file. You can keep memory
usage independent of file size by doing:

print while said:
close(INFO1); # Close the file
print @lines1;
system (`/usr/bin/rm /tmp/check.txt`);

perldoc -f unlink

Sinan
 
G

Gunnar Hjalmarsson

CAT said:
I do not have any problem with the return value. I can run it with
command line and get my result. However, I follow your suggestion to
add this line in.
My problem is the Environment Variable problem, no doubt.

Don't take anything for granted. Do you know that /tmp/check.txt exists
and is readable by the user the CGI script runs as? If that's not the
case, Perl will tell you if you check the open() return value.
 
I

Ian Wilson

CAT said:
I am using this showme.cgi scripts to run the remote scripts check.sh
on another unix server, the public key already copied over to that
machine so that the password is not required.
My problem is, if I comment this line in the remote scripts, the CGI
will work and will print files to my IE browser
#APP_ROOT=/opt/APP/server/app
#export APP_ROOT

If I leave them uncommented, only the command line will work and the
IE and firefox browser will wait forever even I set up the timeout
value as big enough.

You are not using the "-l" option of ssh when you run the remote script
so the effective userid for the remote script differs when you invoke it
from a CGI script. This might affect the current working directory for
the remote script as well as its permissions there.

You seem to be discarding STDERR output of your remote script.

I'd change check.sh from

#!/usr/bin/ksh
APP_ROOT=/opt/APP/server/app
export APP_ROOT
TODAY=`date`
echo $TODAY > /tmp/check.txt
echo ""
/opt/APP/server/app /bin/app status \
| grep NOT-RUNNING >> /tmp/check.txt

to

#!/usr/bin/ksh
function checkstuff {
APP_ROOT=/opt/APP/server/app
export APP_ROOT
TODAY=`date`
echo $TODAY > /tmp/check.txt
echo ""
/opt/APP/server/app /bin/app status \
| grep NOT-RUNNING >> /tmp/check.txt
}
checkstuff 2>/tmp/check.err >check.out

Then see what turns up in /tmp/check.err on the remote system


P.S. I'm no Korn shell wizard but I suspect you could use some help from
a shell related newsgroup like comp.unix.shell.

P.P.S. I'd do both sides in Perl or both sides in Ksh. Mixing both
together seems an unecessary complication. YMMV.
 
C

CAT

You are not using the "-l" option of ssh when you run the remote script
so the effective userid for the remote script differs when you invoke it
from a CGI script. This might affect the current working directory for
the remote script as well as its permissions there.

You seem to be discarding STDERR output of your remote script.

I'd change check.sh from

#!/usr/bin/ksh
APP_ROOT=/opt/APP/server/app
export APP_ROOT
TODAY=`date`
echo $TODAY > /tmp/check.txt
echo ""
/opt/APP/server/app /bin/app status \
| grep NOT-RUNNING >> /tmp/check.txt

to

#!/usr/bin/ksh
function checkstuff {
APP_ROOT=/opt/APP/server/app
export APP_ROOT
TODAY=`date`
echo $TODAY > /tmp/check.txt
echo ""
/opt/APP/server/app /bin/app status \
| grep NOT-RUNNING >> /tmp/check.txt
}
checkstuff 2>/tmp/check.err >check.out

Then see what turns up in /tmp/check.err on the remote system

P.S. I'm no Korn shell wizard but I suspect you could use some help from
a shell related newsgroup like comp.unix.shell.

P.P.S. I'd do both sides in Perl or both sides in Ksh. Mixing both
together seems an unecessary complication. YMMV.

Thanks all, the above replies are very helpful to improve my perl
programming skill, I already revised my codes according all of your
suggestions.

My problem is still on the local side, I could run the perl scripts on
command line without any problem, however, whenever I tried to use web
interface, I will get the problem. ( it is wired that if I click the
button after 20 minutes or longer, it works, then, if I click again,
it will hang there, I cleared the cache file each time).

Best
 
C

CAT

Thanks all, the above replies are very helpful to improve my perl
programming skill, I already revised my codes according all of your
suggestions.

My problem is still on the local side, I could run the perl scripts on
command line without any problem, however, whenever I tried to use web
interface, I will get the problem. ( it is wired that if I click the
button after 20 minutes or longer, it works, then, if I click again,
it will hang there, I cleared the cache file each time).

Best- Hide quoted text -

- Show quoted text -

Thanks, many thanks to all of your great helpers.
I learned a lot and would like to follow in the future programming.
And, you know what, I migrated all these scripts to another better
performance machine, faster CPU, larger memory, it works now.

Thanks again for your time.
 
J

J. Gleixner

CAT said:
[...]
Thanks all, the above replies are very helpful to improve my perl
programming skill, I already revised my codes according all of your
suggestions.

That's amazing, since you haven't posted any perl code.
My problem is still on the local side, I could run the perl scripts on
command line without any problem, however, whenever I tried to use web
interface, I will get the problem. ( it is wired that if I click the
button after 20 minutes or longer, it works, then, if I click again,
it will hang there, I cleared the cache file each time).

Well then, it must be wired wrong. Make sure red connects to red
and black connects to black.

Read the "Posting Guidelines for comp.lang.perl.misc", next time.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top