I've written a script to mount a disk when its connected to a volume with a specific UUID.
#!/bin/bash
#run script and store in PATH the path to disks
pathD=$(python /home/pi/scripts/AUTOMOUNT/disco1.py 2>&1)
#echo $pathD ###print disk's path
#mount it in PATH
if [ $pathD == 'None' ] ###Stop script if disk not present###
then
echo 'Disk not present'
exit
fi
sudo mount $pathD /media/Drive1
echo 'Disco 1 montato'
to run this without sudo I've modified the sudoers file, where was added the line
pi ALL= NOPASSWD: /home/pi/scripts/AUTOMOUNT/monta1.sh
and the script works fine from shell.
Now I need to run it in a PHP script, which is triggered from the Apache webserver homepage. The script is
<html>
<body>
<?php
$delay = 10;
if($_POST['button1'] == 'ON')
{
//action for ON
$output1 = shell_exec('sh /home/pi/scripts/PINS/disk1ON.sh');
echo $output1;
$output2 = shell_exec('sh /home/pi/scripts/AUTOMOUNT/monta1.sh')
echo $output2;
}
else if ($_POST['button1'] == 'OFF')
{
//action for OFF
$output = shell_exec('sh /home/pi/scripts/PINS/disk1OFF.sh');
echo $output;
}
?>
</body>
</html>
and works fine until $output2 command is called. Calling the script monta1.sh (the script written on top) nothing happens ...
I tried to add to sudoers the line
wwwuser ALL=NOPASSWD: /home/pi/scripts/AUTOMOUNT/monta1.sh
but nothing happens
How can I make it work ?
Related
I have a python script that i want to run from index.php and they both are in the same directory.
The problem is that there's no output shown on the page(index.php).
I am running the page on a CPanel shared hosting from GoDaddy.
I have the permissions -rwx for both the files.
There's no error in running the python3 script file from the terminal.
The same files run smoothly on my computer's localhost but not on the CPanel.
Am able to run bash files using the same php code.
I have tried using system() in php to call the python3 script file but this doesn't seems to work.
index.php:
<?php
$comm = "python3 test.py";
echo $comm;
$output = system($comm);
echo "\n";
print($output);
?>
test.py:
print ("Hello")
Current Output:
python3 test.py
I expect the output to be:
python3 test.py Hello
I think you will need to use shell_exec to run your python code like that
<?php
$command = escapeshellcmd('/usr/custom/test.py');
$output = shell_exec($command);
echo $output;
?>
and the first line in the python file should be
#!/usr/bin/env python
Don't forget to give your python file the privileges
chmod +x test.py
I make Relay-Website-with-Raspberry now python application is work but on php is no work
this php screp,
<?php
$output = shell_exec('sudo python 1on.py 2>&1');
echo "<pre>$output</pre>";
header("Location: ../index.php");
exit;
?>
This is not a right procedure to run a python script, Instead running python script in sudo mode give your file permission to www-data
chgrp www-data /path/to/python-script.py
put first line in your python script: #!/usr/bin/env python so the script knows which program to open it with..
Make your script executable
chmod +x /path/to/python-script.py
then try running the script
<?php
$output = shell_exec("/path/to/python-script.py");
echo "<pre>$output</pre>";
header("Location: ../index.php");
exit;
?>
When I run it(bash script) on the linux machine from cmd I get online as output but when I run it remotely from other machine's browser I get offline as output.
My bash script:
#!/bin/bash
wget -q --tries=10 --timeout=10 --spider http://google.com
if [[ $? -eq 0 ]]; then
echo "online"
else
echo "offline"
fi
My PHP script is:
<!DOCTYPE html>
<html><head><title>
This is the title
</title></head>
<body>
<?php
$output=shell_exec('/home/pi/checkonline.sh');
echo $output;
?>
</body></html>
Note: I am hosting the web server on raspberry pi 2 running raspbian OS,php5,apache2.
I use proxyserver to connect to internet, I have successfully configured it in apt.conf as well as export http_proxy, so that will not be the problem.
Try to use the full path of wget
WGET=/usr/bin/wget
WGET='which wget' <-- use backquotes
Try to put your script in same folder as your php script and add execution permission. as for me it's working and printing online.
sudo chmod +x checkonline.sh
<!DOCTYPE html>
<html><head><title>
This is the title
</title></head>
<body>
<h1>output</h1>
<?php
$output=shell_exec('./checkonline.sh');
echo $output;
?>
</body></html>
#!/bin/bash
wget -q --tries=10 --timeout=10 --spider http://www.google.com
if [[ $? -eq 0 ]]; then
echo "online"
else
echo "offline"
fi
I have a PHP webpage on my raspberry pi with 2 buttons (on and off)
The on button button redirects to On.php
The off button redirects to Off.php
In "/usr/lib/cgi-bin" I have a python script that I would like to execute (script.py)
I can perfectly execute it from the terminal by typing
cd /usr/lib/cgi-bin
sudo python script.py
It works if I do it from the terminal.
The problem is the PHP file (On.php) in my "/var/www" folder.
This is what I wrote:
<?php
exec('cd /usr/lib/cgi-bin');
exec('sudo python script.py');
?>
Why is the script executing from the terminal, but not from my PHP?
You can't use sudo from a PHP script. Apache is running from an user (www-data generaly), so edit this file : /etc/sudoers
Then add this line :
www-data ALL=(ALL) NOPASSWD:ALL
Care ! this will authorize all functions to be called by a PHP script, you can adapt changing "ALL" by your script or Python command.
Then precise your user in your exec command :
<?php
exec('sudo -u www-data python /usr/lib/cgi-bin/script.py')
Try this out, it should be working:
<?php
system("cd /usr/lib/cgi-bin");
system("sudo python script.py");
?>
Or even this:
<?php
system("cd /usr/lib/cgi-bin && sudo python script.py");
?>
On an older Raspbian distribution you need to place your file in /var/www/file.py. So in your file.php you add:
{
exec("sudo python /var/www/file.py");
}
On a newer Raspbian Jessie you need to place your file in /var/www/html/file.py, so in your file.php you need to add:
{
exec("sudo python /var/www/html/file.py");
}
Or just any file.py
<?php
{
exec("sudo python test.py");
}
?>
Note: For this to work you need to edit a file first to add these lines to allow passwordless sudo
sudo nano /etc/sudoers
then go to the bottom and add this
pi ALL=(ALL) NOPASSWD: ALL<br>
www-data ALL=(ALL) NOPASSWD: ALL
I have a shell script(i.e mat.sh) in /var/www/ and also have a php script(i.e. n1.php) in the same directory. When i am running the shell script from bash command it is running, but when i m executing the same from the apache2 php server it is not executing.
mat.sh contains..
#!/bin/bash
cat <<EOF | /var/www/matlab -nodesktop -nosplash -nodisplay /> result.out
a=add(2,3);
disp('this is done');
disp(a);
exit
EOF
note:/var/www/matlabis the directory of matlab link
n1.php contains..
<html>
<body>
<?php
if ($_GET['run'])
{
# This code will run if ?run=true is set.
echo "Execution starts here...<br/>\n";
echo exec("whoami");
echo "<br/>\n";
exec ("chmod a+x mat.sh", $output, $return);
if ($return != 0)
{
//An error occured, fallback or whatever
echo "Execution failed<br/>\n";
}
exec ("sh mat.sh");
}
?>
<!-- This link will add ?run=true to your URL, myfilename.php?run=true -->
Click Me!
Please help me.....
Do you get your error message when having done chmod?
Make sure that mat.sh has the same owner as your apache2-server-instance otherwise this line
exec ("chmod a+x mat.sh", $output, $return);
will fail, because only the owner can change permissions.