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
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!
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!';
?>
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
Related
I am trying learn to execute shell scripts from within PHP code. So, I made a test program to execute a bash script from within PHP. However, it has no effect. The relevant code is shown below.
<?php
.......
shell_exec('/bin/bash /var/www/html/just_touch.sh');
?>
The just_touch.sh script just creates a new file, like as shown below.
touch /home/user/some.txt
I was expecting to have file /home/user/some.txt after execution, but no, it isn't made. What mistake, am I doing?
P.S: The following code works though.
$output = shell_exec('ls /home/user');
echo $output;
Does this have anything to do with permissions?
Moreover, I notice that while this prints "Can you see me?".
$output = shell_exec('echo Can you see me?');
echo $output;
This doesn't!
shell_exec('echo Can you see me?')
What is going on here?
Stderr is lost when using shell_exec. You might wan't to use:
shell_exec('/bin/bash /var/www/html/just_touch.sh 2>&1');
I have a PHP script that I'd like to run both via the browser and from the command line. When I run the script from the command line, it executes without a problem. However, when I call it via a function such as exec or passthru, it doesn't work. I'm not getting any output from the call and I see no errors in the logs. I'm very confused...
echo exec('php /usr/share/nginx/www/function.php arg1');
Any ideas?
The output is captured in a parameter variable as an array and not returned back, when you use exec(). So do something like this:
exec('php /usr/share/nginx/www/function.php arg1', $output);
print_r($output);
#David,
A few things to check.
Create a PHP Info page and see if exec or passthru is enabled. Hosts disable it for security.
Try:
<?php
$exe = exec('php /usr/share/nginx/www/function.php arg1');
var_dump($exe);
?>
You could even do:
<?php
if(function_exists('exec')) {
echo "exec is enabled";
}
?>
I have a C program which has been compiled to an executable. I can run this program from my console. I am trying to get the output from this program through my web browser so I am using the exec command in PHP. My PHP script works fine when I execute it from the command line, but when I call it over a browser I get no input. Here is my PHP program
<?php
echo exec('/var/www/html/./readcard');
?>
The readcard program has 777 permissions. I am guess the issue is something to do with permissions?
You aren't capturing the output. The second argument to exec consists of an array to which the output lines will be placed.
<?php
$output=array();
$rv=0;
exec('/var/www/html/./readcard',$output,$rv);
if($rv!=0)
{
die("readcard failed, got a return value of $rv\n");
}
foreach($output as $line)
{
echo("<p>$line</p>\n");
}
?>
You probably just echo the return code of the script, which is zero. You can either redirect the output to a file and then serve that from php, or pipe the output stream directly back to php code.
Try
<?php
$output = array();
exec('/var/www/html/./readcard', &$output);
?>
I am trying to run a simple PHP that runs a powershell script,
if I use this code I get the results in a command window, but I get an empty array in the browser:
<?php
exec("powershell C:\\Inetpub\\wwwroot\\my_shell.ps1 < NUL", $output);
echo "<pre>";
print_r($output);
echo "</pre>";
?>
I believe the NUL will discard the output, however it works in the browser found it on [this Fourm][1]
If I use this code, without the NUL I will get the results on a command window but if I run the script in the browser will keep loading forever and it will never give me any results:
exec("powershell C:\\Inetpub\\wwwroot\\emsrDev\\manual_shell.ps1", $output);
Same results if I do it this way:
$output = shell_exec("powershell C:\\Inetpub\\wwwroot\\emsrDev\\manual_shell.ps1");
The powershell Script runs fine if I ran it independently:
$cmd = "cmd.exe";
&$cmd "/C echo update tasks set last='manual' where id='8'; | sqlplus vvv/www#xxx";
So I need to execute this in a browser and get the output.
The powershell process isn't terminating, which causes the browser to hang. Add the following to the end of your script
Stop-Process -processname powershell*
I'm trying to execute a linux command through a PHP command-line script, which is no problem using the exec command.
The problem is, the command I am executing (mysqldump) outputs an error message if something is wrong (for example user/password is incorrect). I can't seem to be able to capture this error in order to log it. It just prints this error to the screen.
How do I cause this error not to be printed to the screen, but instead to put it in a variable for use in my script?
Thanks!
Use popen to run the process. The example #2 on this page shows exactly what you're looking for:
<?php
error_reporting(E_ALL);
/* Add redirection so we can get stderr. */
$handle = popen('/path/to/spooge 2>&1', 'r');
echo "'$handle'; " . gettype($handle) . "\n";
$read = fread($handle, 2096);
echo $read;
pclose($handle);
?>
exec("mysqldump -u user -p passwod database > outputfile.sql 2> error.log");
You need to redirect stderr to stdout, so you can capture it. This example routes stdout to devnull (thus ignoreing it) and routes stderr to you:
exec('ls * 2>&1 1>/dev/null');
I'm not too hot in Unix (New Years Resolution...) but these functions look helpful:
shell_exec - returns result as a string.
passthru - It looks like you can execute this like: passthru('command', $result); and then use $result.
tried using backticks?
$var = `command`;
The following will route stderr messages to the same place as the normal output.
exec("mysql_dump blah 2>&1",$output,$return_val)
if($return_val !== 0)
echo "there was an error"
2>&1 means re-route stderr messages to the same place as stdout, and thus will be loaded into the output array.
Have you looked at the system() command? It's been a while since I did any PHP, but that rings a bell.