I'm building a web app that has to run command line tools that all work fine while SSH'd in. Trying to use the exec() command to call the program I want to run on button click but no luck. Code below.
<?php if (isset($_POST['setup'])) { exec('home/ubuntu/pathtoprogram'); } ?>
<form method="post">
<button name="setup">Click to setup</button>
</form>
Is there something I'm missing or a mistake calling the program in the exec() function?
You should have this C program inside your project directory. Always use absolute path and it should works fine
Related
I'm using XAMPP on Windows 10, and trying to run a simple PHP script that uses the shell_exec function to call a simple R script in the same directory. Here's the PHP index.php file:
<?php
echo shell_exec('Rscript test.R');
And here's the test.R file:
print("Hello, World!")
From the command line (i.e., Git for Windows), when I run php index.php in the folder, I get the following output:
[1] "Hello, World!"
However, when I run index.php from the web browser via XAMPP and localhost, I don't see anything on the page. At first, I thought it might be a permissions issue, so I gave Full control access to all users on the folder, but it didn't seem to change anything.
Any ideas on how to get this working from the browser? Thank you.
Found the answer to the problem I was having. Apparently, even though Rscript is part of my Windows 10 PATH variable, when I try to execute an R script via shell_exec in PHP, I have to have the whole path to the Rscript executable (I guess because the PHP script / Apache server doesn't know about the path variable).
I changed the shell_exec function call as follows, and it worked:
echo shell_exec('"C:\Program Files\R\R-4.1.0\bin\Rscript" test.R');
Here's a link to the SO post that helped me figure this out:
shell_exec does not execute from browser requests
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.
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);
});
I want to run a python script in a button press event of a php/html file. I tried following. Is this possible with PHP exec()? But following didn't work. What is wrong with my code?
HTML file
<html>
<head></head>
<body>
<H3>Run Python Script...</H3>
<form name="input" action="test2.php" method="get">
<input type="submit" value="Submit">
</form>
</body>
</html>
PHP exec()
<html>
<head></head>
<body>
<H3>Executing...</H3>
<?php exec("first.py"); ?>
<H3>Completed...</H3>
</body>
</html>
exec Executes a command as if on the command line, running first.py just via bash won't work. You have two options:
Option A
You can tell the python parser explicitly to run the file with:
exec('python first.py');
Option B
Make sure your python script's first line is the path to python:
#!/usr/bin/python
#Python code...
Then call exec('./first.py')
(If you chose this option, you'll need to make sure the script is executable)
Conclusion
I'd probably go with option A as it's simplest and gets the job done without fuss.
Hope this helps :) x
There can be several problems with even your python script.
First: Can you run this script from console? (Do you have a #!/path/to/env/python in the script's beginning)?
If not, then either add it to your script or to the exec function (without #!)
You can also use passthru instead of exec, so you can display the raw output of the script.
try exec("/usr/bin/python path/to/first.py")
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.