在 perl/cgi 文件中运行系统命令时缺少输出

发布于 2024-08-27 23:45:54 字数 714 浏览 2 评论 0原文

我需要编写一个CGI程序,它将显示系统命令的输出:

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

在CGI文件中:

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

最后,当我在unix中运行CGI文件时,$VAR是一个很长的包含 \n 和一些分隔符的字符串。但是,当我在 Web 服务器上运行时,输出为

++++++

++++++

So $VAR is Missing while in the Web Interface/browser. 我知道问题可能是 $VAR 是很长的字符串。

但无论如何,除了将输出写入文件然后从浏览器检索它之外,还有什么办法可以解决这个问题吗?

如果您对我的问题感兴趣,谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

匿名。 2024-09-03 23:45:54

script.sh 使用多个环境变量:$USER$HOST$CMD$PASS。 CGI 环境将设置与登录 shell 不同的环境变量。在调用 script.sh 之前,您可能需要从 CGI 脚本中设置这些变量。

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.

无可置疑 2024-09-03 23:45:54

尝试查找您正在调用的expect和ssh等命令在系统上的位置,并将它们的目录路径添加到脚本使用的PATH中。

which expect

添加行:

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

返回 /usr/bin/expect,然后在 ksh 脚本的开头附近 。在调试期间,您可能还希望通过将 2>/tmp/errors.txt 附加到命令末尾来将 stderr 重定向到文件,因为浏览器中不显示 stderr。

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

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";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文