How to run a win32 console application on Linux/Apache web-hosting? - php

The executable uses 2 un-managed dlls & gives output on console.
I'm currently running it on XAMPP/localhost (on my PC) & grabbing the console output with PHP's Exec()/PassThru() function.
Will it run the same way on a typical Apache/Linux based Web-hosting account? If not , what can I do?
In PHP :
<?php
exec("TradeLogin.exe",$output);
//TradeLogin.exe is the win32-console-app,
//situated at Xampp/htdocs, with couple of supporting binary files & dlls
//(compiled on Visual-Studio-2015)
echo $output[0]."<br/>";
echo $output[1]."<br/>";
?>

I can't tell if there will be permission problems with it (I'm on a Windows atm. so can't try.)
If you want it to run this way, you need a Windows emulator (like Wine for example), and use it like
exec("wine TradeLogin.exe 2>&1",$output);
Or full path may be necessary. Note that the execution of the PHP code will stop until your program has finished running.

Related

PHP on Windows - check if working in background

I have Windows 10 with xampp installed.
Let's assume I have a PHP script, containing
set_time_limit(0);
ignore_user_abort(true);
$counter = 0;
while($counter < 60){
file_put_contents('runtime_log.txt', $counter." \r\n", FILE_APPEND);
$counter++;
sleep(1);
}
I can close the browser window and the script will be still writing output to runtime_log.txt
If I wanted to check if it's running in the background on Linux, I would use
ps aux | grep php
How to do the same thing on Windows?
EDIT: I have simplified my question, because I can see that it's been misunderstood.
You can't get that information directly from the process list in Windows, because by default Apache is configured to load PHP as a module (a .DLL) so it will not be listed as a different process when it's running (unless PHP is configured to run as CGI, but nowadays that option is not used anymore).
I think that you can search for that using Process explorer, open it and then search (with the binoculars icon) for opened handles for runtime_log.txt, if the file is opened by PHP an open handle should appear for the httpd (Apache) process (this is method I use for finding the program that prevents me from deleting a file).
Note that you probably need to run Process Explorer elevated (as an administrator) since Apache is running as system service.
Don't put too much effort, just use the constant PHP_OS as also described here: How to get the OS on which PHP is running?

python script executed from php gives error

On my debian i run xampp. I want to execute a python script using php shell_exec.
This is my php code:
shell_exec("/opt/lampp/htdocs/news/hello.py 2>1 &");
When i run it from the browser i get this error:
/usr/bin/python: /opt/lampp/lib/libz.so.1: no version information available (required by /usr/bin/python)
If i run it from the terminal window using this: php /opt/lampp/htdocs/page/index.php it works without any problems.
So any ideas how can i make it work from the browser?
Thanks
I'm guessing xampp comes with it's own libraries instead of using the system libraries, and that probably means it's setting LD_LIBRARY_PATH to it's local library directory.
This will also cause other programs started from php to use this libraries, which they may not be compatible with.
To veryfy that, try system("env");, that sould show you all the exported environment variables. If LD_LIBRARY_PATH is set, use:
shell_exec("LD_LIBRARY_PATH= /opt/lampp/htdocs/news/hello.py 2>1 &");
That unsets it before running the python script.

Run a script on a computer from php

I'm unsuccessfully trying to execute a shell command from php. The goal is to switch on/off my music player of my computer/server via internet (with my phone for example). Here is what I would be able to do :
I have a very simple file "play.sh" :
Code:
xdotool key XF86AudioPlay;
echo "switched";
if I run it ./play.sh, that works (music player switches on/off)
then I have an other very simple php file "play.php" :
Code:
<?php echo shell_exec("./play.sh"); ?>
These two files are in the main folder of my server which is a partition of my computer. (I'm using lampp) But music is playing from my computer.
But when I'm going on localhost/play.php, I can see "switched" that showed me the sh file as been executed, but the sound doesn't turn off .
I just tried exec(), shell_exec(), passthru(), system(), .. with ./play.sh and /opt/lampp/.../play.php the result is exactly the same, and there is no error message.. :/
But that's weird, I'm not sure, but I think that what I run on my computer side is not the same than what I run on my server side. I mean that it's like the sound was turning on/off on the virtual server but had no link with the sound of my computer. That could be an explanation.. but then, how to execute a script on my computer from internet/my server..?
Does someone have an idea to do that ? Is it a configuration of apache..?
Thanks !
EDIT :
Here is how I solved my problem :
xhost + local:www-data
Thanks for all your replies
It may be a permissions issue - keep in mind that when PHP runs a command, it's run as the user the webserver is running as, e.g. www-data or apache or whatever. If your music player is running as your own personal user, your script may not have the ability to change it when run as a different user.
Shell_exec works. Try to put in your .sh script absolute path to the executables
/usr/bin/xdotool key XF86AudioPlay;

Accessing kd Debugger via php

I work at a small computer shop, and we have to analyze windows minidumps all the time. My idea was to install the Windows Debugging Tools on a windows PC and use apache/PHP as an interface to it. That way I could just set up an HTML upload form that would accept the minidump file, run it through KD, then spit out the output.
It nearly works. I created a special user just for apache so I could assign it write privaleges to C:\symbols, and I use the following code:
<?php
$kdScript = "\"\\Program Files\\Debugging Tools for Windows (x86)\\kd.exe\" -c \"!analyze -v;Q\" -y srv*c:\symbols*http://msdl.microsoft.com/download/symbols -z ";
$kdScript .= $_FILES["myFile"]["tmp_name"];
$output = `$kdScript`;
print("<pre>$output</pre>");
?>
The problem I'm having is that the symbols are not downloaded as they should be. I've verified apache is running as the user I think it is by calling "whoami" from inside backticks. I've verified that I can run the windows version of wget from within backticks, so I have access to the network. I can file_put_contents() into a new file under C:\symbols, so I have file creation permissions.
Also, I tried having PHP simply output the command to the browser so I could copy and paste it into a terminal. I was able to run a command prompt as my apache user via "runas", paste the command from PHP's output into the prompt, and it worked as expected, downloading all the symbols it needed to C:\symbols. Of course, I had to point it to a dump file NOT in the PHP temp directory, but this shouldn't make a difference.
What could be the problem? Just as a side note, all of this is local on a trusted pc in a company that has a total of 3 employees/owners. Security for this project is irrelavent.
Not sure what your exact problem is, but the symbol server client code is finicky and not very debuggable, it took us lots of tinkering to implement our version of this. You can always direct folks there or use it yourself:
http://www.osronline.com/page.cfm?name=analyze
-scott

PHP exec on Windows Server 2008

I have installed PHP as a module in IIS on Windows Server 2008, am am having great difficulty trying to execute command-line programs from within a PHP script.
I can execute the PHP code fine using php-cgi.exe and php-cli.exe, which leads me to believe that it might be a permissions issue.
However, I can execute some commands, like shutdown.exe and dir.
I have tried the same process in an ASP.NET file and am having exactly the same problem.
Basically, I would like to do this:
exec("path-to-exe-file");
And actually have it work.
Try
exec('path-to-exe-file 2>&1', $output);
var_dump($output);
The 2>&1 part should redirect error messages to stdout (on win32, too) which therefore should show up in $output.
I spent several hours before realising what was wrong...
Use IIS Manager, Application Pools, select the pool associated to your application and make sure that:
ProcessModel.Identity = LocalSystem
and
ProcessModel.Load User Profile = true

Categories