Windows allow user to run commands - php

On windows xp I can run many commands form PHP with shell_exec() function.
I need to use OfficeToPDF on windows 10 with PHP.
My php script is:
<?php
$command = 'OfficeToPDF input.docx output.pdf';
$exec = shell_exec($command);
echo $exec;
?>
This script works on windows XP Professional, but not on windows 7, 8, 10.
OfficeToPdf simply opens Microsoft Word and saves file as PDF.
What changed on win7, win8, win10? Why I couldn't execute this command from PHP on new OS's?
I have apache service automaticly starts from SYSTEM user.
UPDATE: But I can run OfficeToPDF input.docx output.pdf command when I open cmd window manually.

I suspect that some environment variables are missing that are required. You should try running from within a command window instance, that will setup the default env vars:
$command = 'cmd /c OfficeToPDF input.docx output.pdf';
$exec = shell_exec($command);
echo $exec;
You might want to use the full path to OfficeToPDF, input.docx and output.pdf in case your default directory is incorrect, or call chdir() first.
The meaning of /C is:
/C Carries out the command specified by string and then terminates

Related

PHP + winexe -> loads forever

I'm trying to list running services on a windows server via php. Therefore I'm using shell_exec with winexe.
My script:
$cmd = "winexe --interactive=0 --user='***' --password='***' //192.168.***.** \"net start\"";
$output = shell_exec($cmd);
echo $output;
Unfortunately on execution the page loads forever with no result. The command works on the command-line (Debian).
Anyone an idea?
Thanks in advance.
Save $cmd with correct format into a new bash file. Set cmd value for call this file. Remember set execution perms to this file.
Check if your apache user has perms for exec winexe
===
Try to launch
cat </dev/null | winexe --interactive=0 --ostype=1 --user=...

Executing .exe file using PHP on Linux server

I'm new in using Linux, I'm trying to write a PHP code which can run .exe linux compatible file, I've made a short shell script
hello bash script:
#!/bin/bash
./program.exe file.mp4 // file.mp4 is an an input for .exe
echo "Hello World!"
shell.php:
<?php
$output = exec ("./hello ");
echo "<pre>$output</pre>";
?>
Now when I run shell.php using web browser it shows Hello World! but the .exe doesn't run, however when I run php using terminal command php shell.php, It works fine.
I think I'm having problems with permissions but I'm new with Linux and I don't know how to solve this.
Update:
I ignored the shell script and I used
<?php
$output = shell_exec ("cd /var/www/ && ./program.exe file.mp4 2>& " );
?>
also I granted access to program.exe
chmod 777 program.exe
the error I receive in the browser :could not open debug.bin!
use the absolute path to hello executable exec("sh path/to/the/file")
I'm using something similar to call an app compiled with mono on a remote ubuntu webserver and return it's output to the calling script.
For any of this to work properly wine needs to be already installed.
On Ubuntu systems try:
sudo apt-get -y install wine
You then need to know the owner of the web server process. If you are running the apache web server try the following:
cat /etc/apache2/envvars | grep "RUN"
The output will look something like this:
export APACHE_RUN_USER=www-data
export APACHE_RUN_GROUP=www-data
export APACHE_RUN_DIR=/var/run/apache2$SUFFIX
Now that you have the name of the process owner, which in this case is www-data you should ensure the file is owned the user and its group:
sudo chown www-data /var/www/program.exe
sudo chgrp www-data /var/www/program.exe
Finally, we can invoke the application from inside our PHP script by passsing it as a parameter to 'wine' and using its full file path.
<?php
$output = shell_exec("wine /var/www/program.exe file.mp4" );
?>
Any output from the above shell command sent to the command line will be saved in the PHP script variable $output.
It looks like you are trying to do some output redirection with your use of program.exe file.mp4 2>& so I've left that off of the example for clairity.
Try using the absolute path, such as exec("sh /path/to/file")
Generally, php is run as www or apache, so make sure that the execute access permission is granted to all user.

PHP exec in background using & is not working

I am using this code on Ubuntu 13.04,
$cmd = "sleep 20 &> /dev/null &";
exec($cmd, $output);
Although it actually sits there for 20 seconds and waits :/ usually it works fine when using & to send a process to the background, but on this machine php just won't do it :/
What could be causing this??
Try
<?PHP
$cmd = '/bin/sleep';
$args = array('20');
$pid=pcntl_fork();
if($pid==0)
{
posix_setsid();
pcntl_exec($cmd,$args,$_ENV);
// child becomes the standalone detached process
}
echo "DONE\n";
I tested it for it works.
Here you first fork the php process and then exceute your task.
Or if the pcntl module is not availabil use:
<?PHP
$cmd = "sleep 20 &> /dev/null &";
exec('/bin/bash -c "' . addslashes($cmd) . '"');
The REASON this doesn't work is that exec() executes the string you're passing into it. Since & is interpreted by the shell as "execute in the background", but you don't execute a shell in your exec call, the & is just passed along with 20 to the /bin/sleep executable - which probably just ignores that.
The same applies to the redirection of output, since that is also parsed by the shell, not in exec.
So, you either need to find a way to fork your process (as described above), or a way to run the subprocess as a shell.
My workaround to do this on ubuntu 13.04 with Apache2 and any version of PHP:
libssh2-php, I just used nohup $cmd & inside a local SSH session using PHP and it ran it just fine the background, of course this requires putting certain security protocols in place, such as enabling SSH access for the webserver user, so it would have exec-like permissions then only allowing localhost to login to the webserver ssh account.

How to start a program from web browers using php

I'm trying to make a open source kiosk like system. When the web browser starts all programs, it will run in the browser using PHP. I've found this link: Program execution Functions. It's using:
<?php
$WshShell = new COM("WScript.Shell");
$oExec = $WshShell->Run("WINWORD.exe", 7, false);
?>
I was able to start Microsoft Word on Windows, but I need to be able to do this on Linux.
This is what I've tried on my Linux server:
<?php
exec("/var/www/test.sh");
?>
But nothing happens. I know that test.sh works because I ran if from the terminal. I use test.sh to start a Python script. The Python script starts a text editor. I've tested the Python script and it works. All I need know is how to start the script from PHP on Linux.
Try out shell_exec.
shell_exec("/var/www/test.sh");
Also, make sure that the executable "permission" is set.
Additionally, you have to run the text editor as the logged-in user (propably you will do this with sudo or so) and to set the DISPLAY environment variable to :0.0 (or whatever is right for you). Example:
add this to /etc/sudoers:
ALL<tab>ALL=(kioskuser) NOPASSWD: ALL
(<tab> means that a real tab belongs there, edit the file by executing visudo as root)
content of the script:
#!/bin/bash
export DISPLAY=:0.0
sudo -u kioskuser /path/to/the/editor/command
I had to change the test.sh owner permission to www-data
then add
Cmnd_Alias RUN = /var/www/test.sh
www-data ALL = (root) NOPASSWD: RUN
To /etc/sudoers then the php code
<?php
shell_exec("sudo /var/www/test.sh");
?>

What is linux equivalent of running a .bat file on windows from PHP using system()

I have a PHP script that runs a .bat file on my windows machine using
$result = system("cmd /C nameOfBatchFile.bat");
This sets some environmental variables and is used to call Amazon EC2 API from the command line.
How do I do the same from a Linux server? I have renamed my .bat file to a shell (.sh) and changed the script to use 'export' when setting env vars. I have tested by running the code from a putty terminal and it does what it should. So I know the commands in the script are good. How do I run this from PHP? I have tried running the same command as above with the new filename and I don't get any errors, or file not found etc but it doesn't appear to work.
Where do I start trying to solve this?
---------------------------------- UPDATE -------------------------------
Here is the PHP script that calls the shell file -
function startAmazonInstance() {
$IPaddress = "1.2.3.4"
$resultBatTemp = system("/cmd /C ec2/ec2_commands.sh");
$resultBat = (string)$resultBatTemp;
$instanceId = substr($resultBat, 9, 10);
$thefile = "ec2/allocate_address_template.txt";
// Open the text file with the text to make the new shell file file
$openedfileTemp = fopen($thefile, "r");
contents = fread($openedfileTemp, filesize($thefile));
$towrite = $contents . "ec2-associate-address -i " . $instanceId . " " . $IPaddress;
$thefileSave = "ec2/allocate_address.sh";
$openedfile = fopen($thefileSave, "w");
fwrite($openedfile, $towrite);
fclose($openedfile);
fclose($openedfileTemp);
system("cmd /C ec2/mediaplug_allocate_address_bytemark.sh");
}
And here is the .sh file - ec2_commands.sh
#!/bin/bash
export EC2_PRIVATE_KEY=$HOME/.ec2/privateKey.pem
export EC2_CERT=$HOME/.ec2/Certificate.pem
export EC2_HOME=$HOME/.ec2/ec2-api-tools-1.3-51254
export PATH=$PATH:$EC2_HOME/bin
export JAVA_HOME=$HOME/libs/java/jre1.6.0_20
ec2-run-instances -K $HOME/.ec2/privateKey.pem -C $HOME/.ec2/Certificate.pem ami-###### -f $HOME/.ec2/aws.properties
I have been able to run this file from the command line so I know that the commands work ok. When I had this working on windows there would be a delay as the instance started up and I could echo the results to the screen. Now there is no delay as if nothing is happening.
Put a hash-bang on the first line of your shell script.
#!/bin/bash
Then give it the executable flag.
$ chmod a+x yourshellscript
You can then call it from PHP with system.
$result = system("yourshellscript");
$result = system("/bin/sh /path/to/shellfile.sh");
Is script executable? If not, make it so:
$ chmod a+x script.sh # shell
system ("/path/to/script.sh"); // PHP
or launch it via interpreter:
system("sh /path/to/script.sh"); // PHP
Is interpreter specified in shell script (ie. #!/bin/sh line)?
have you tried shell_exec() ?

Categories