PHP: Executing Command Line Application That Prompts Users - php

I have a command line application I need to execute from my PHP web application. Say the command is the following:
foo -arg1 -arg2 -arg3
Based on certain conditions, the command line application will prompt user to hit the enter key (e.g. "Please press enter to continue.").
From my PHP application, how do I execute the command line AND send the enter key as response to the prompt?
I'm developing on WAMP. Production code is LAMP.

That's what the 'yes' program is for. It dumps an endless stream of 'y\n' (or whatever you tell it to via arguments) to the program. It exists for this purpose (answering 'yes' to "do you want to continue" prompts).
shell_exec('yes | foo -arg1 -arg2 -arg3')

You will really need to open a process handle and parse the programs output and write appropriate output in response.
Check out the expect extension though, which can make this sort of thing easier.

$value = fgets(STDIN);
This will allow the user to enter in a value, which you can then access via $value.

Have you tried echo "\n" > foo -arg1 -arg2 -arg3 ?

Related

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.

Shell script command "ldap_search" is not working with php exec or shell_exec command

I'm developing a code which uses ldap_search Shell Script Command for extracting user information from Active Directory using user id and by proper LDAP Server Authentication. I am getting accurate result from ldap_search script.
But, whenever I put the shell script inside exec or shell_exec PHP command, I'm not getting anything.
All the other shell scripts are working fine with the help of PHP exec command except ldap_search.
Is there some additional task left for me to do?
Is ldap_search and exec/shell_exec not compatible with each other?
You must use echo exec('your command or script');
Make sure to have permissions to run it. I mean, the web user must have permissions to execute that.
May seem obvious, but I think your failure is in something basic like this. You must put echo to show the result of the command.
EDIT After reading your new comments about it and using that new info... I saw you are trying to redirect the output to a file... but maybe you have 2 different problems.
Have the user which is executing php (usually www-data) permission to write on the folder where the php is?
Your code has quotes inside quotes that must be escaped using . Try this:
<?php exec("ldapsearch -x -v -h 'LDAP://server' -p '389' -D 'uid=\"domain_user_id\",ou=users,ou=internal,o=\"organization\"' -w 'domain_password' -b 'ou=users,ou=internal,o=organization' 'uid=person's_user_id' >> result.txt"); ?>
So you don't need echo if you want the output in a file. And the redirection >> can be inside the command executed, not in php.
Remember that > replaces de file and what you have >> add at the end of the file.

Running solr server from php

Currently, I have to navigate to the example directory of solr and execute java -jar start.jar.
However, I want users to have the ability of running the server automatically by selecting an option. Lets say,
1.The user downloads the `solr` directory on his/her server.
2.Enters the location of the example directory he/she just downloaded.
3.Selects/Clicks an option saying "Start Solr server".
4. And the server is started.
Is this possible? I'm looking to do this through php.
If you can do it via command line, you can do it via php's exec() assuming that is not disabled on your server.
Note: This is a very dangerous function if you allow user input to go through it.
I think you need something like the following command to do this. Just add the path to the given command:
<?php
exec("nohup java -jar start.php > output.log 2>&1 &");
?>
nohup is important to decouple the solr process from your php request.
Your idea is dangerous, because you allow the user to enter any location to start their scripts. So you really need to think about path validation to ensure that only solr is started and nothing else.

How to execute an interactive shell program from a PHP webpage

Let's say I have a local webpage, that when a button is pressed, executes a program in C. I already can do that. The problem is that this program outputs logs and needs input from the user. My question is: how can I display bash-like window and run this program through it, so that output and input is seen through the screen?
Perhaps you could use ajaxterm, and specify the program that you want to run as the shell:
ajaxterm.py --command=/your/command --port=yourfavoriteport
You could redirect the user to the specified port, or display the terminal via an iframe.
I develop plugin for jquery "Terminal Emulator" you can check this out. If you write your C program as JSON-RPC you can create your terminal with one line of javascript.
The other solution is to not use PHP at all, and creating your code as CGI written in C or even in Bash.
Update: You can modify your program to be CGI script
int main() {
printf("Content-Type: text/plain\n\n");
// code of your program here
}
and put it in cgi-bin directory and run it through browser http://yourserver.com/cgi-bin/program it display the output in browser

Categories