php 如何隐藏 system() 输出

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6708145/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 01:02:47  来源:igfitidea点击:

how to hide system() output

phpsystem

提问by user806168

i am working on windows XP . i can successfully run a system() command through my browser by calling a TCL script that automates a ssh session. I also return a value from the script. however my problem is that the script dumps the entire ssh session in the browser.

我正在使用 Windows XP。通过调用自动执行 ssh 会话的 TCL 脚本,我可以通过浏览器成功运行 system() 命令。我还从脚本中返回一个值。但是我的问题是脚本在浏览器中转储了整个 ssh 会话。

my php script looks like :

我的 php 脚本如下所示:

$lastline=system('"C:\tcl\bin\tclsh.exe" \path to file\filename.tcl '.$username.' '.$pass,$val);

$lastline=system('"C:\tcl\bin\tclsh.exe" \path to file\filename.tcl '.$username.' '.$pass,$val);

filename.tcl:

文件名.tcl:

spawn plink -ssh $user@$host
expect "assword:"
send "$pass\r"
expect "\prompt:/->"
set $return_value [string compare /..string../ $expect_out(buffer)]
/...some code...this runs fine/
exit $return_value

spawn plink -ssh $user@$host
expect "assword:"
send "$pass\r"
expect "\prompt:/->"
set $return_value [string compare /..string../ $expect_out(buffer)]
/ ...一些代码...这运行良好/
退出 $return_value

everything runs fine and i get $return_value back correctly but the php file prints the result of the execution of the entire ssh session in my browser which looks like:

一切运行正常,我正确地返回了 $return_value 但 php 文件在我的浏览器中打印了整个 ssh 会话的执行结果,如下所示:

Using username "admin". [email protected]'s password: === /*some text*/ === \prompt:/->.../some text/

使用用户名“admin”。[email protected] 的密码: === / *some text*/ === \prompt:/->.../some text/

i want to prevent the system() function from printing this in my browser
i have used the shell_exec() function but it returns the entire ssh session result (which i have parsed in the tcl script and got a precise value to return to the php script) is there a way i can do this without using shell_exec() but using system() instead

我想阻止 system() 函数在我的浏览器中打印这个
我已经使用了 shell_exec() 函数但它返回了整个 ssh 会话结果(我已经在 tcl 脚本中解析了它并得到了一个精确的值来返回到 php脚本)有没有一种方法可以在不使用 shell_exec() 而是使用 system() 的情况下做到这一点

thanks in advance

提前致谢

回答by csl

The documentation for system()specifically says:

system()文档特别说明:

Execute an external program and display the output

执行外部程序并显示输出

On that page are listed alternatives. If you use the exec functioninstead, it will only execute the commands without displaying any output.

在该页面上列出了替代方案。如果您改用exec 函数,它将只执行命令而不显示任何输出。

Example:

例子:

<?php
echo "Hello, ";
system("ls -l");
echo "world!\n";
?>

will display the output of system:

将显示输出system

$ php -q foo.php
Hello, total 1
-rw-r--r-- 1 bar domain users 59 Jul 15 16:10 foo.php
world!

while using execwill not display any output:

使用exec时不会显示任何输出:

<?php
echo "Hello, ";
exec("ls -l");
echo "world!\n";
?>

$ php -q foo.php
Hello, world!

回答by genesis

use ob_start(); before and ob_clean(); after calling it

使用 ob_start(); 之前和 ob_clean(); 调用后

http://sandbox.phpcode.eu/g/850a3.php

http://sandbox.phpcode.eu/g/850a3.php

<?php 
ob_start(); 
echo '<pre>'; 
$last_line = system('ls'); 
ob_clean(); 
echo 'nothing returned!'; 
?>

回答by poisson

In general if you want to prevent anything to output to the browser you can use ob_start() before your system() call and then ob_end_clean(). See http://php.net/manual/en/function.ob-start.php

通常,如果您想阻止任何内容输出到浏览器,您可以在 system() 调用之前使用 ob_start(),然后使用 ob_end_clean()。见http://php.net/manual/en/function.ob-start.php