23

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 "password:"  
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

Mime
  • 1,142
  • 1
  • 9
  • 20
user806168
  • 457
  • 2
  • 5
  • 11

3 Answers3

32

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!
csl
  • 10,937
  • 5
  • 57
  • 89
  • 1
    thanks csl ... it works ... earlier i was using ob_start() and ob_end_clean() functions before and after the system() command to get the desired results ... thanks for the input .... – user806168 Jul 19 '11 at 11:21
  • Neither ob_start nor exec helped me prevent the stderr from showing. If someone wants this, see https://stackoverflow.com/questions/33171386/capture-supress-all-output-from-php-exec-including-stderr/33173998 – nl-x Aug 19 '21 at 19:12
4

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!'; 
?>
genesis
  • 50,477
  • 20
  • 96
  • 125
  • sry for missing that out in my ques, but even when is do that $result stores the value of $return_value which i am passing back from the tcl script. nevertheless, output still dumps the ssh session in my browser – user806168 Jul 15 '11 at 14:07
1

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

poisson
  • 1,324
  • 11
  • 20