Missing output when running system command in perl

2019-08-18 03:46发布

问题:

I need to write a CGI program and it will display the output of a system command:

script.sh

echo "++++++"  
VAR=$(expect -c " spawn ssh -o StrictHostKeyChecking=no $USER@$HOST $CMD match_max 
100000 expect \"*?assword:*\" send -- \"$PASS\r\" send -- \"\r\" expect eof ") 
echo $VAR 
echo "++++++"

In CGI file:

my $command= "ksh ../cgi-bin/script.sh";
my @output= `$command`; 
print @output;

Finally, when I run the CGI file in unix, the $VAR is a very long string including \n and some delimiters. However, when I run on web server, the output is

++++++

++++++

So $VAR is missing when passing in the web interface/browser. I know maybe the problem is $VAR is very long string.

But anyway, is there anyway to solve this problem except writing the output to a file then retrieve it from browser?

Thanks if you are interested in my question.

回答1:

script.sh uses several environment variables: $USER, $HOST, $CMD and $PASS. The CGI environment will have different environment variables set than a login shell. You may need to set these variables from your CGI script before calling script.sh.



回答2:

Try finding where commands like expect and ssh that you are calling are on your system and adding their directory paths to the PATH used by your script. I.e.

which expect

returns /usr/bin/expect then add the line:

PATH=$PATH:/usr/bin && export PATH

near the beginning of the ksh script. During debug you may also want to redirect stderr to a file by appending 2>/tmp/errors.txt to the end of your command since stderr is not shown in the browser.

my $command= "ksh ../cgi-bin/script.sh 2>/tmp/errors.txt";