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.
Related
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.
I'm making a streaming server with the CLI vlc. To make this easier, I made a WEB control interface using HTML/PHP. The problem is when I run the following command in a terminal it works perfectly, but when launch it from PHP it fails (it makes like there was no options after vlc).
> vlc -vvv movie.mp4 --sout=#rtp{sdp=rtsp://:8554} --sout-keep
Here is my PHP file:
<?php
$output = shell_exec($_POST['command']);
echo $output;
?>
Input file:
<form method="POST" action="textearea.php">
<textarea id="command" name="command">
</textarea>
<input type="submit" value="OK"/>
</form>
the $output variable says that:
VLC media player 2.2.4 Weatherwax Command Line Interface initialized. Type help for help. > Shutting down.
It is exactly the same as if I just typed vlc in the terminal by itself - and of course, the stream doesn't work.
ok I found the answer by myself. you just need to replace vlc by cvlc in the command. it's a right problem of the www-data user.
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
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
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