Executing a command on server - php

I am developing an handwriting recog tool for android and I have planned to use a server for this purpose. I will be getting data from the android interface and send it to the server where it's stored in a file. I have a toolkit written in c which I will be using and I have to execute a few commands which will have text files as its parameters.
An example of a command: 
HVite -A -D -T 1 -H C:\htk\model\hmm0\Single.mmf -i C:\htk\data\train\lab\1.mlf -w C:\htk\def\net.slf C:\htk\def\dict.txt C:\htk\model\hmm0\hmmlist.txt C:\htk\data\train\user\h1.txt
Where single.mmf, 1.mlf, net.slf, hmmlist.txt are the parameters to the command hvite. Is there a way to execute such commands on server and if yes what are the proposed solutions?
Secondly is sending data from android to the server and then doing all the processing on the server side a good choice? I would have to post the result back to android as well.

Try this command http://www.php.net/manual/en/function.pcntl-exec.php and notice that "Executes specified program in current process space" - this limits your program and on the other hand gives you safe environment. You may also want to look at this list http://php.net/manual/en/book.exec.php

Related

Is there a way to know when the PHP -sever has is ready to serve requests?

One may start a local PHP server, e.g for testing:
php -S localhost:8080
One can also execute a PHP statement, e.g.
php -r "echo 'Hello';"
We initially hoped we could use this to tell when the server was started, i.e. using systemd-notify or some other process readiness protocol. However, using -r and -S together seems to ignore -r.
My question is thus, when starting a local server using php -S, is it possible to execute some code after the server is ready to receive incoming connections? This would allow us to execute something like systemd-notify --ready and enable the parent process to know when to proceed with testing.

How to call php (stored on a server) using vba/vbs

I want to call php using vba or vbs. The php file is stored on a server.
I am able to do it if the php file is stored locally:
Sub asasdsad
Call Shell("C:\xampp\php\php.exe C:\path\file.php", 1)
End Sub
This calls the php which executes a code for me. My problem is, the .php file I want to call is stored on a server, for which I've got username and password of course. Copying file to local directory is not an option as it's got a lot of includes.
My idea is to use PuTTY to connect to the server, and use it to execute above command, all from cmd using vba/vbs.
UserName = "un"
Passwrd = "pw"
'this would need additional parameters at the end to call php.exe like above
Call Shell("""C:\Program Files (x86)\PuTTY\putty.exe"" " & "-ssh " & UserName & "#ip address -pw " & Passwrd, 1)
As you can imagine there will be a lot of parameters so it just get complicated, not ever sure if this would work. I've never used PuTTY and all of this is quite new to me. I'm sure there's a better way?
First, do not use PuTTY, use Plink (PuTTY connection tool). It's a console application designed for automation (contrary to PuTTY, what is GUI application, designed for an interactive use).
Plink (again contrary to PuTTY) can accept a command to be executed on its command-line (it has a similar command-line syntax as OpenSSH ssh):
"C:\...\plink.exe" -ssh username#ip_address -pw password /path/to/php /path/to/script.php

How to get result from a command executed in PHP on remote SSH server using PuTTY?

I'm trying to execute a command on my Raspberry Pi via SSH and get the result of it in my PHP script on my Windows machine. Currently I can execute the command on my RasPi, but I do not get any results back into the PHP script.
The code I'm Using for this:
<?php
$cmd = "C:\\path_to_putty\\putty.exe -ssh pi#RasPiIP -pw raspberry -m C:\\path_to_test.txt\\test.txt";
$result = shell_exec($cmd);
echo $result;
?>
For sending commands to my RasPi the code works. I have tested multiple times by as example changing test.txt to sudo reboot and it worked as intended.
I'm using PuTTY to send my command (test.txt is currently nfc-list which returns connected Scanners etc not important right here) to the RasPi.
What I want to achieve is that $result contains the returned data when my command is executed.
Is it even possible to do that? If yes how (any help appreciated). If no, are they maybe other ways to approach this?
Addressing the possible duplicate: I am using a Windows Machine and also I'm trying to get the result (of the one command) to reuse in my PHP script. In the other question, user is trying to save the full console log and save it to another file.
First, do not use PuTTY. PuTTY is a GUI application intended for an interactive use. Use Plink, which is command-line/console equivalent of PuTTY intended for command automation. Being a console application, it has a standard output, which can be read in PHP (PuTTY as a GUI application does not have standard output).
With Plink, you can also specify the command on Plink command line, so you do not need to create the test.txt command file.
In any case, there's no way to make PuTTY or Plink separate an output of command only (at least not from a command-line).
But what you can do, is to print some header/trailer to distinguish the start and end of the command output, like:
plink.exe -ssh pi#RasPiIP -pw raspberry "echo start-of-command && command && echo end-of-command"
And then in PHP, you can look for the start-of-command and end-of-command to identify what part of Plink output is really the command output.
In any case, you better use a PHP SSH library to achieve what you want, rather then driving an external application. For example phpseclib. But that's a completely different question.

PHP CLI Connecting to WebSerbvice

I am having an issue with running my PHP application from the command line.
I recently created a new Google Cloud Engine CentOS instance to host my PHP application.
This application has been running away fine on a different RHEL box.
The application is kicked off from a PHP script using a command similar to...
$command = 'bash -c "exec nohup setsid runPHPScript > /dev/null 2>&1 &"';
exec($command, $output, $returnVar);
runPHPSCript is a linux script that essentially runs the actual PHP command...
php myScript.php
myScript.php then goes off and connects to various webservices etc...
When I try to run this on my new instance (bearing in mind this all worked fine on my RHEL box) I get the following SOAP error...
(faultcode: HTTP, faultstring: Could not connect to host)
The SOAP setup/connection to the endPoint WSDL is actually successful but as soon as I try to send the request I get the error above.
I've been debugging this and reading up a bit and can confirm the following...
HTTP & HTTPS are both enabled on the GCE instance.
I have verified that both PHP & Apache are using the same php.ini (more on this below)
I have checked both configurations using phpinfo() and php -i and can see the various SSL entries in the data
The strange part is that if I open myScript.php in a browser (its a LAMP stack) it connects fine to the webservice and I can see the valid response. This lead me to think the problem was different php.ini's being used.
Also, at the command prompt, if I just run myScript.php directly it also works fine...
php 'myScript.php'
returns a valid response too.
So the problem only seems to occur when I try to kick off the application using BASH.
Anyone got any ideas?
Robert
Cleaning up.
As per Raidenace suggestion, just running exec directly worked fine too...
"just curious...why cant you just do an exec("php myScript.php"); in your code direcctly?"

curl not executing properly when invoked by php

So I want to execute a bash command from PHP on my web server. I can do this using shell_exec. However, one of the commands I want to execute is curl. I use it to send a .wav file to another server and record its response. But when invoked from PHP, curl doesn't work.
I reduced the error to the following small example. I have a script named php_script.php which contains:
<?php
$ver=shell_exec("curl -F file=#uploads/2013-7-24-17-31-43-29097-flash.wav http://otherserver");
echo $ver
The curious thing is that when I run this php script from command line using php php_script.php, the result I get is
Status: 500 Internal Server Error
Content-type: text/html
However, if I run curl -F file=#uploads/2013-7-24-17-31-43-29097-flash.wav http://otherserver directly, I get the response I was expecting:
verdict = authentic
(Edit:) I should probably mention that if I put some bash code inside the shell_exec argument which does not contain curl, the bash command executes fine. For example, changing the line to $ver = shell_exec("echo hello > world"); puts the word "hello" into the file "world" (provided it exists and is writable). (End edit.)
Something is blocking the execution of curl when it is invoked from PHP. I thought this might be PHP's running in safe mode, but I found no indication of this in php.ini. (Is there a way to test this to make 100% sure?) What's blocking curl and, more importantly, how can I bypass or disable this block?
(And yes, I realize PHP has a curl library. However, I prefer to use commands I can run from the command line as well, for debugging purposes.)
cheers,
Alan
The reason is the administrative privileges when you run the command directly you are running it as root and thus the command gets executed. But, when you run the command through PHP it runs as an user. By, default user has not the privileges to run the shell_exec commands.
You have to change the settings of shell_exec through CPanel/Apache config file. But, it is not recommended to provide the shell_exec access to the user as it help hackers to attack on server and thus, proper care should be taken.
It would be more appropriate to use the curl library provided in PHP.

Categories