shell_exec and vlc issues? - php

I'm making a streaming server with the CLI vlc. To make this easier, I made a WEB control interface using HTML/PHP. The problem is when I run the following command in a terminal it works perfectly, but when launch it from PHP it fails (it makes like there was no options after vlc).
> vlc -vvv movie.mp4 --sout=#rtp{sdp=rtsp://:8554} --sout-keep
Here is my PHP file:
<?php
$output = shell_exec($_POST['command']);
echo $output;
?>
Input file:
<form method="POST" action="textearea.php">
<textarea id="command" name="command">
</textarea>
<input type="submit" value="OK"/>
</form>
the $output variable says that:
VLC media player 2.2.4 Weatherwax Command Line Interface initialized. Type help for help. > Shutting down.
It is exactly the same as if I just typed vlc in the terminal by itself - and of course, the stream doesn't work.

ok I found the answer by myself. you just need to replace vlc by cvlc in the command. it's a right problem of the www-data user.

Related

Use PHP exec to execute Minecraft command

I'm working on creating my own little Website to manage a Minecraft server as fun project. Now what I would need to accomplish is being able to send commands to the screen in which the server is running.
My approach to this was the following:
<?php
if (isset($_POST['startbutton']))
{
exec('sudo screen -S 23971 -X stuff "say hello^M"');
}
?>
<form method="post">
<button type="submit" name="startbutton">Test</button>
</form>
Now that command line works just fine when i execute it in the terminal itself, but as soon as i try to run it over the Website nothing happens.
If i just try to execute
if (isset($_POST['startbutton']))
{
echo exec('whoami');
}
?>
it works just fine as well. I don't know what I am doing wrong.
i'm really not sure about it, but try:
<?php
if (isset($_POST['startbutton']))
{
exec('sudo screen -S 23971 -X stuff "say hello^M"');
exec('YOUR SUDO PSW');
}
?>
<form method="post" action="YOUR PHP PAGE LIKE server.php">
<button type="submit" name="startbutton">Test</button>
</form>
let me know if it works
Seems like you are trying to execute a command that needs superuser permissions. In most setups PHP runs under the webserver user and it does not have those permissions. Also you should keep in mind that giving superuser permissions to PHP is a security risk.
My suggestion would be to have a dedicated service, responsible for executing those shell commands. You could then call that service from your PHP script without needing sudo.
Check these for details:
https://serverfault.com/questions/622271/securely-executing-system-commands-as-sudo-from-php/622277
sudo in php exec()
First of all: Thank you all for your support.
I was doing a bit of further research and found a solution for my problem:
I created a new user which I then gave the ownership of /var/www.
I then changed the apache2 user from www-data to the new user.
Now i just needed to start the screen with the minecraft server as the new user so i can access this screen out of php and I was able to get it to work without having to give any user full root privileges or anything.

PHP can run a bash file from the php shell but not when a button is clicked on webbrowser(ubuntu-firefox))

I hava a bash file that can be executed when I run it from the terminal using php -a (interactive shell), by typing:
shell_exec('sh /home/oceanview/run_test.sh');
I have created a button and when this button is clicked the bash file should be run
<form action="Control.php" method="post">
<input value="Continous Acquisition " name="Continous" type="submit">
</form>
<?php
if (isset($_POST['Continous'])) {
shell_exec('sh /home/oceanview/run_test.sh');
}
?>
But nothing happens when I press the button. I have tried chmod +x and chmod 0777 already and there is no error showing in log.
I have to add that the interactive shell asks for my password when I run the shell_exec command in the terminal.
Under a web post request, you aren't going to be able to run a script that's interactively requesting your password (at least not without a lot of additional infrastructure, like an expect script). How do you expect to be typing in your response, anyway?
Most likely what is happening is that the process is exiting on detecting that it isn't attached to an interactive terminal. You wouldn't be able to see any output it's producing anyway, since you aren't capturing or using the result from shell_exec().
You can use something similar to this in order to input your password and then pass it off to the shell command
<form action="Control.php" method="post">
<label>Password: <input type="password" name="pwd"></label>
<input value="Continous Acquisition " name="Continous" type="submit">
</form>
<?php
if (isset($_POST['Continous'])) {
$command = sprintf(
'echo %s | sh /home/oceanview/run_test.sh',
escapeshellarg($_POST['pwd'])
);
echo shell_exec($command);
}
?>
You can test this out from a command line by using
echo 'mypasswordhere' | sh /home/oceanview/run_test.sh

how to set cmd(DOS) in php

I figure out how to execute the input to the cmd program with php using exec function ('cmd.exe')
most of the tutorials available on the internet suggest execute commands using the command line argumen.tapi cmd.exe (DOS) does not have a command line argument
so basically I want to set the (DOS) program like cmd enter input into it without having to use a parameter cmd argument, as we typed directly into cmd itself.
I made a (DOS) program using C ++ language, I want a program that can be run in php website without having to create a parameter in advance, as we typed directly into the program, live stream
code that I use
<?php
$inputcmd = $_POST['inputcmd'];
$output = shell_exec('cmd.exe $inputcmd');
echo "<pre>$output</pre>";
?>
<form method="POST" />
<input type="text" name="inputcmd" />
<input type="submit" value="submit" />
</form>
dos program that I created does not have prameter, so I want to type directly into it blends into the input output php
You can generally get cmd to execute your code as arguments if you use something like:
C:\Users\PaxDiablo\Documents> cmd.exe /c "echo hello"
hello
That's from an actual command interpreter window but the same rules apply anywhere you can run cmd.exe.
I suppose I don't need to mention that it's a spectacularly bad idea to allow some random Joe to execute arbitrary code on your server. So, if you haven't thought that through, give it at least a couple of minutes.

Executing shell commands on a HTML gui - on local machine only

So I'm working on an application of my own where i need to execute shell commands, and even execute csh scripts on button clicks.All the HTML code is doing is displaying results from some sensors i have set up on my arduino via firefox, so im basically using it as a GUI. The HTML page wont ever be online, its purely running on my own machine.
I understand that its not straight forward as having HTML run shell commands on a live webpage is a security issue, but i have been reading about being able to execute shell commands using php scripts, but as it is running on a standalone PC i dont know how to integrate it into running a command from a button click on the page.
Infact i have no experience in using php at all, and i wouldnt even know where to start getting it to run through my HTML code.
Help!
edit:
If not php, is there any other way i can run a command such as 'ls' from a button click on the html page?
Oli
You can use the exec php function, and execute your scripts like this exec('/path/myscript.sh')
But surely you have to setup your php/apache in a correct way. Just try to look for some basic web-server configuration tutorials.
php command exec should execute a command and retrieve standard output.
<?php
$result = exec("wc -l /etc/issue");
print($result);
?>
With that print command you can print any html-text, that will be inserted into the page the client receives and displays. So simply surround that php-snippet with you actual web page.
General idea, you can create a HTML form with buttons that correspond to specific shell commands ( I understand this won't be LIVE on the web, so the security risks might not be an issue).
From there, you would receive the form input in PHP (you can use a framework like Laravel which makes this much easier to handle frontend/backend communication - http://laravel.com/docs/4.2/html http://laravel.com/docs/4.2/requests)
A quick example with HTML and PHP:
<form id="arduino" method="post" action="{{ url('arduino/control') }}">
<input type="text" name="command"/>
<input type="submit">
</form>
<?php
Route::post('arduino/control', function () {
$command = Input::get('command');
exec($command);
});

Executing scanimage command from PHP

I am brand new to PHP and I am trying to execute a shell command to initiate a command line scan on my Ubuntu 12.10 box:
I have the following piece of code in my php file (inside submit click handler):
shell_exec('scanimage --format=pdf > scan.pdf')
Is this even possible? I have been able to execute other terminal commands through this API. However in this case nothing happens. I am able to run the above command in a terminal window
I am trying to do the scan on form submit
<form method="post" action="printApp.php">
<input type="submit" name="scan" value="Start New Scan" />
</form>
<?
if(isset($_POST["scan"])){
shell_exec('scanimage --format=pdf > scan.pdf')
}
?>
Create a new php file called scanimage.php and put just
<?php
shell_exec('scanimage --format=pdf > scan.pdf');
?>
in it. Then at bash type php scanimage.php while in the same directory as the file.
UPDATE: After doing a chat it seems that Apache didn't have the permissions needed to access the scanner somewhere in /dev.

Categories