Calling a PHP script from within PHP - php

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";
}
?>

Related

PHP shell_exec command doesn't work

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');

PHP exec() TRACERT.exe how to pass desired arguments to Windows command

I want to remotely execute tracert in a Windows machine with PHP exec(). I have:
<?php
echo exec("C:\\Windows\\System32\\TRACERT.exe");
echo "<br/>Success!";
?>
This does not give me errors and it prints "Success!."
But how do I pass an argument (such as an IP address to tracert.exe and print the result in a variable or array?
I do not know the syntax to pass an argument that looks like: tracert , etc.
I prefer passthru() as the traceroute output can be watched in browser on the fly, not having to wait until completed.
$IP=$_REQUEST['IP'];
set_time_limit(120);
echo "<h1>Traceroute $IP</h1><pre>";
passthru("tracert.exe -h 8 $IP");
By default exec will return only the last line of the executed command.
You should use shell_exec as follows:
<?php
$result = shell_exec("C:\\Windows\\System32\\TRACERT.exe www.google.com");
print $result;
echo "<br/>Success!";
?>

Trying to execute a C program from PHP and output to a web browser

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);
?>

how to hide system() output

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

Executing linux commands with PHP

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.

Categories