Missing output when running system command in perl/cgi file

∥☆過路亽.° 提交于 2019-12-11 20:11:05

问题


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";


来源:https://stackoverflow.com/questions/2553880/missing-output-when-running-system-command-in-perl-cgi-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!