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
Related
I want my PHP script can mont a remote system with sshfs command.
But it doesn't seem to work, the folder has been created but the folder still empty after execution. I also tried with a user without SU and it was working fine.
mkdir ("/var/mont/remote/");
$cmd = "sshfs -o password_stdin -o allow_other enzo#192.168.0.29:/home/enzo/remote/ /var/mont/remote/ <<< 'MyRemotePassword'";
$output = nl2br(shell_exec($cmd));
echo $output;
This script (test.sh) should work :
#!/usr/bin/env bash
php -r 'mkdir ("/var/mont/remote");
$cmd = "sshfs -o password_stdin -o allow_other enzo#192.168.0.29:/home/enzo/remote/ /var/mont/remote/ <<< 'MyRemotePassword'";
$output = nl2br(shell_exec($cmd));
echo $output;'
Run it as bash test.sh
I have a bash code like this:
sudo docker exec -it container_id /bin/bash -c 'cp test1.txt test2.txt'
rm test3.txt
It worked well in terminal. But I want to call it from html. My html code is :
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>run bash</title>
</head>
<body>
<?php
if(isset($_POST['submit']))
{
$output=shell_exec('sh /Users/path/script.sh');
}
?>
<p><?php echo $output; ?></p>
</body>
Basically, I want to click the button "submit" (the php code I put above will be called by another html file) and it will execute the bash code to login to docker and run the command cp test1.txt test2.txt.
But it only deleted test3.txt while it didn't copy the file on docker when I ran it from html. Also, I didn't get any output from website. This was weird cause it could work in terminal just by sh /Users/path/script.sh.
I am new to both html and docker. Will be really appreciate it if anyone could give me advice.
Since you are executing docker with "sudo" you'd need the command to be executed via shell_exec() with sudo as well.
Furthermore, if your server environment uses chroot, the docker binary might not be even available to the user running the PHP process.
I suggest checking with your system administrator.
/off As this is not a programming question, you might have better luck approaching people in Server Fault
First of all you need to allowed ( probably www-data user ) to use sudo without password for this one script.
You can do this via sudoers:
vim /etc/sudoers
Add line:
<user> ALL=(ALL) NOPASSWD: /path/to/your/script.sh
Then your shell_exec should looks like this:
$output=shell_exec('/usr/bin/sudo /path/to/your/script.sh');
But be careful, use sudo only if know what you are doing ;)
I also suggest to print output using "pre"
<pre><?php echo $output; ?></pre>
Good luck !
In my case, the error was not about sudo.
To get the stdout, I ran this
<pre><?php echo exec('sh /path/to/my/script/script.sh 2>&1', $output, $return_var); ?></pre>
<pre><?php print_r($output); ?></pre>
<pre><?php echo $return_var; ?></pre>
Noted that when an error occurs during the execution, or the process does not produce output, shell_exec() will return NULL. Therefore, using function shell_exec() cannot detect whether the process is successfully executed by the return value. If you need to check the exit code executed by the process, use the exec() function instead.
It outputed the input device is not a TTY.
And in my bash script, I changed sudo docker exec -it container_id /bin/bash -c 'cp test1.txt test2.txt' into docker exec -i container_id /bin/bash -c 'cp test1.txt test2.txt'.
Basically, ranning docker exec -i rather than docker exec -it fixed my problem. Hope this will help guys have same problem.
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 ?
I created a .sh file in my linux and want to run it from webpage (php + Apache).
Some of the simple example execute without problem. But I can't run with echo Pipe:
#!/bin/sh
set +v
cp /tmp/test/test1.tar.gz.gpg /tmp/test/ts2.gpg
echo 'myPassword' | /usr/bin/gpg --passphrase-fd 0 --output /tmp/test/test1.tar.gz --decrypt /var/backups/test1.tar.gz.gpg
exit 0
It can copy ts2.gpg but can't run the second command (decrypt).
You want shell_exec.
<?php
$output = shell_exec('ls -lart');
echo "<pre>$output</pre>";
?>
Enjoy
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.