Running a bash script from html - php

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.

Related

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!

Not able to execute shell script in 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

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

Execute looping bash script via PHP without interrupting server function

I have an apache server with a login page where I want to pass the credentials provided at the login to authenticate an upload of a file sent to the server. I'm very new to this
When I upload a file to the server, it responds with a 500 code. I think the problem is with autoupload.sh because when I comment out line 3 of upload.php, the error code ceases and the uploads to the server work, but naturally now the files are never uploaded to the external service.
The point of autoupload.sh is watch for monitor for added files to the /uploads/ directory, upload them to a webservice, then delete them locally on completion, and then exit.
index.php is a form that looks like
<form action="uploadpage.php" method="POST">
<input name="field1" type="text"/>
<input name="field2" type="password"/>
<a href='/tmp/data.txt></a>
//html content
//more html content
uploadpage.php contains another form:
`<form action="upload.php" class="dropzone" id="my-dropzone"></form>
//more html and css`
upload.php looks like:
`$old_path=getcwd(); //preserve working directory
chdir('/path/to/dir/');
shell_exec("./autoupload.sh $_POST['field1'] $_POST['field2']");
chrdir($old_path);
//other stuff`
and finally autoupload.sh looks like:
user=$1
pw=$2
while IFS= read -r file; do
curl -u $user:$pw -T "/path/to/$file" https://web#site.com && rm -f "path/to/$file"
exit 1
done < <(inotifywait -m q -r -e MOVED_TO --format '%f' /var/www/html/uploads/)

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