Not able to execute shell script in php - php

I am able to execute the shell script through php via command line terminal. But I am not able to execute the same script(.sh) using php when clicked on HTML button through browser.
Following is my php script(wettyload.php)-
<?php
shell_exec('/var/www/html/wettyload.sh');
?>
Following is my shell script(wettyload.sh)-
#!/bin/bash
cd /root/wetty
node app.js -p 8080
Following is my html code(url.html)-
<html>
<body>
<form name="form2" method="post" action="http://127.0.0.1/wettyload.php">
<input type="submit" id="url" name="url" value="Connect terminal">
</form>
</body>
</html>

Use
<?php
shell_exec('sh /var/www/html/wettyload.sh');
?>
Update:
You didn't mention /root/wetty before, you've a couple of options:
Move the wetty dir to a location where the webserver user has execute permissions.
Set the permission of /root/wetty to allow the webserver user to execute (unsafe)
Note:
Make sure you've execute permissions to wettyload.sh

Related

Running a bash script from html

I am referring the post ,Execute a shell script from HTML, ,
Here is mine file:
example.php
<?php
if(isset($_POST['submit']))
{
shell_exec('bash script.sh');
}
?>
<form action="" method="post">
<input type="submit" name="submit" value="Call my Shell Script">
</form>
script.sh
filedex="classes.dex"
flag2="--output=$filedex"
filej="apple.java"
filecl="apple.class"
javac $filej
dx --dex "$flag2" "$filecl"
touch hello.txt
When I am running through the browser localhost/example.php , I could see my script is only generating apple.class file and hello.txt file i.e. it is only executing javac $filej and touch hello.txt. It is not executing dx --dex "$flag2" "$filecl" while when I am running through the shell i.e. bash script.sh it is genearting respective file. Why it is not generating through browser, I tried many times but failed. Please someone help me.

Shell script is downloading instead of executing

I wrote a PHP code to execute a shell script .
But when i execute the php script in browser , the shell script is getting downloaded , instead of executing .
Can anyone please help in resolving the issue .
Note :
PHP file source path : myuser $ /tmp/sample/index.php
index.php has the code written to execute the shell script test.sh
index.php
<form action="test.sh" method="get">
<input type="hidden" name="act" value="run">
<input type="submit" value="run">
</form>
test.sh
#!/bin/bash
USERNAME=sfuser
HOSTS="pcsched01.syd.sf.priv"
SCRIPT="touch t.txt"
for HOSTNAME in ${HOSTS} ; do
ssh -l ${USERNAME} ${HOSTNAME} "${SCRIPT}"
done
Try like this
<?php
if (isset($_POST['button']))
{
exec("/path/to/name.sh");
}
?>
<html>
<body>
<form method="post">
<p>
<button name="button">Run Perl</button>
</p>
</form>
</body>
You have a web server (probably Apache?) running that serves content over HTTP.
Once you submit your form, you send a request to the Web Server to serve you with the content 'test.sh' via an HTTP GET request.
The web server serves you the content.
All works as expected!
You need to create a PHP script that actually executes the SH script using, for example: http://php.net/manual/en/function.exec.php
<?php
exec('test.sh');
?>
Succes!

Trying to run a python script via PHP button hosted on raspberry pi but failing

I have a php script that should (I think) run a python script to control the energenie radio controlled plug sockets depending on which button is selected. It seems to work in that it echos back the correct message when the button is pressed but the python scripts don''t appear to run. I have added the line:
www-data ALL=NOPASSWD: /usr/bin/python /home/pi/lampon.py
which should give the apache user privileges to run the python script at least for turning on the power socket but it doesn't work. The script itself does work when run via the pi command line itself. Any suggestions? (the code for the php is below)
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>LED Control</title>
</head>
<body>
LED Control:
<form method="get" action="energenie.php">
<input type="submit" value="ON" name="on">
<input type="submit" value="OFF" name="off">
</form>
<?php
if(isset($_GET['on'])){
shell_exec("python /home/pi/lampon.py");
echo "LED is on";
}
else if(isset($_GET['off'])){
shell_exec("python /home/pi/lampoff.py");
echo "LED is off";
}
?>
</body>
</html>
Have you tried setting the permissions of the lampoff.py and lampon.py to 777?
chmod 777 /home/pi/lampoff.py && chmod 777 /home/py/lampon.py
I think you need add "sudo" the python script for it to work which means you have to add the www-data user to /etc/sudoers.
shell_exec("sudo python /home/pi/lampon.py");
or
exec("sudo python /home/pi/lampon.py");
There was another post that deal with this recently on Execute Python script from Php

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

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