how to hide system() output

2019-04-18 10:13发布

问题:

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.

my php script looks like :

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

filename.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

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:

Using username "admin". admin@10.135.25.150's password: === /*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

thanks in advance

回答1:

The documentation for system() specifically says:

Execute an external program and display the output

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

Example:

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

will display the output of 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 exec will not display any output:

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

$ php -q foo.php
Hello, world!


回答2:

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

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

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


回答3:

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



标签: php system