How to run 2 dependent shell commands by php - php

i have 2 shell commands and i want to exec in php the first command that act like an application and stay into it and exec another command there (in this application).
i write in my shell: wpa_cli
and get the result:
Interactive mode
>
now i can write commands in that software, like:
Interactive mode
> status
and get the result:
wpa_state=DISCONNECTED
p2p_device_address=34:b1:f7:1f:2d:bb
address=34:b1:f7:1f:2d:bb
>
my problem is that i want to do that outside the shell by php.
how can i do that?

Your requirement is similar to this interactive shell using PHP, but it requires execution of PHP script in terminal, which you don't want.
So, in this case, you can use any of these php functions: shell_exec, exec, system or passthru and run both wpa_cli status commands together, you will get the required result.
e.g. run the following script in your php file and see the output,
echo '<pre>'. shell_exec('wpa_cli status') .'</pre>';
You can also use PHP Ajax Shell or similar scripts to execute your commands and get response on the same page without reloading it.

Related

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 shell_exec() Behaves Differently Than Terminal Command Line MacOS

Scanline is a simple command line utility used for scanning documents from a twain scanner. http://blog.scottkleper.com/scanline-command-line-scanner-for-mac/
I am trying to use Scanline through a PHP script using shell_exec(); the same way that I would use it directly from the terminal in MacOS.
When I run Scanline directly from the command line, it detects all the attached scanners and prints them out ./scanline -list
When I run Scanline using shell_exec(), it does not detect any devices.
So far, I have changed the apache user to my local user, and have added the local user to the sudoers file. If I run 'whoami' in shell_exec() it is the same result as if I run it in command line.
I have printed the environment using printenv in command line, and have setup all of the same variables in my php script before executing shell_exec() using putenv(); If I run shell_exec('printenv 2>&1'), it is the exact same environment as when I run printenv in command line.
All the permissions are correct and allow access, and scanline runs when executed through shell_exec() without error (I checked apache's error logs, as well as put a error_reporting(E_ALL); in the top of the PHP file to printout any issues along the way). The only difference in how the program is executed is that in command line the devices are detected, and run through shell_exec(), no devices are found.
Any ideas as to what else I could be missing between command line and using shell_exec() ?
I also tried using system(), exec(), and shell_exec() interchangeably with the same result.

Run cmd in php with > operator

I want to run a simple shell command, say:
dir > bau.txt
Using php:
$cmd = escapeshellcmd ('dir > bau.txt');
shell_exec($cmd);
But it does not work (bau.txt stays empty).
Is there any reason for that?
A normal $cmd would work (say just 'dir').
This seems like a similar scenario:
How to run multiple commands in system, exec or shell_exec?
Essentially when you use the > operator, you're piping your standard output to the file you've specified. It seems that shell_exec doesn't directly support piping, but rather, the output needs to be stored from shell_exec and then run through shell_exec with the next command.

know when ffmpeg completes converting

am using ffmpeg in one of my sites with PHP , i convert files using the php exec function , actually it did me some headache trying to figure out WHEN this ffmpeg completes the file conversion after executing the exec command :( is there anyway to do that ?
Thanks
From what I've found, the exec function blocks until the ffmpeg conversion is complete.
For example, you can run ffmpeg like this in your PHP script:
exec($encode, $output);
(Where $encode is the ffmpeg command as a string, and $output is an array of each line of output from ffmpeg.)
For me, this exec command blocks my PHP script from continuing until ffmpeg conversion is complete, at which point my PHP script continues on, which seems to be how it is described in the PHP manual:
http://php.net/manual/en/function.exec.php
So, you can tell when exec is complete by following the exec command with another PHP command on the next line in your script that notifies you conversion is complete, or updates a database, or what-have-you.
FYI, I believe that pushing the exec command "into the background" means running the exec command but having the PHP script continue on simultaneously (i.e. asynchronously). For running the exec command in the background, Google "PHP background exec" or "php multi-process", such as:
http://www.php.net/manual/en/ref.exec.php#80241
http://www.sitecrafting.com/blog/to-run-php-code-in/

Run shell commands with PHP?

Occasionally my media server goes down and I'm wondering if it's possible to start it remotely using php to check the port and if it's not running invoke cron (or some other way) to run a shell command. Is this possible because this is not a strong area for me. Here's the process I use with PuTTy.
login to shell
cd to source/red5/dist
screen
./red5.sh
CTRL-A then D to detach
logout
Simplest thing is to write a shell script. And then login to remote console via PHP.
shell_exec: execute a shell command and returns the output as string.
exec: just executes an external program
A simple way to achieve what you want is to run this in screen:
while /bin/true ; do ./red5.sh ; done
If you can write a shell script that does what you need, then PHP's has exec(), system() and passthru() for you.
PHP actually has a special operator for executing shell commands, the backtick:
`cd source/red5/dist`
will go to the specified directory. (But I don't know much about shell, so I can't implement you the whole thing.)
If you need much control over the execution (I don't know whether you need here) use proc_open.
you can use corn job on php and put all command on .sh file and run like this
59 11 * * 1,2,3,4,5 root command file.sh?token
something like this ,it will be save
There is more than one good answer here, but you should opt for executing the init script for red5 instead of the .sh or .bat. There are pre-made init scripts here: http://code.google.com/p/bigbluebutton/downloads/detail?name=red5&can=2&q= and here: http://www.videowhisper.com/forum.php?ftid=48&t=init-file-red5-linux-installations-red5-linux-init.d-chkconfig

Categories