I have an overhead controller (sort of a magic wand for some robots) plugged into my usb port.
I am trying to create a web server on linux that allows users to send a preloaded code (so no one is inputting anything) to the robots. I have embedded the command in a script file that I am trying to execute in a php file. The bit of code that executes the script file in php is run when a button is pressed on the web server. However, when I press the button nothing happens. The code doesn't get sent to the controller and no errors are given.
I can run the script in the command line with no problems. But when I try to execute it run it from the web server, nothing happens. Is there a way I can resolve this?
This is what's in the php file
<html>
<body>
<h1><font face="verdana" color="green">SwarmArena</font></h1>
<hr>
<form action = "<?php $_PHP_SELF ?>" method = "POST">
Name: <input type ="text" name = "name"/>. ; <br>
Age: <input type ="text" name="age"/>. ; <br>
New Code: <textarea cols=40 rows=10 name="Newcode">Default text</textar$
<input type = "submit" name="enter" value="Enter" />
<input type = "submit" name="insert" value="insert" />
<input type = "submit" name="select" value="select" />
<input type = "submit" name="sleep" value="sleep" />
<input type = "submit" name="reset" value="reset" />
</form>
</body>
</html>
<?php
if(isset($_POST['sleep'])){
$out = shell_exec('./sleep.sh');
echo "$out";
}
?>
this is what's in the sleep.sh
#!/bin/sh
cd kilocmd && ./kcmd -s /dev/ttyUSB0 -c sleep
echo "done";
Thanks
I don't know how you are doing this. But onclick the button it should submit to a php page which will run the shell scripts using exec and shell_exec php commands .
More Over that Several Possibilities May Cause Issues like
- exec and shell_exec are disabled in php.ini
- The path to the executable is wrong. If the script is in the same
directory as the php file, try exec(dirname(__FILE__) .
'/myscript.sh');
Assuming you are executing the command correctly from the PHP script, it may be useful to look into permission issues.
Log into the user that the web-server is running under and try executing the command manually from there. I personally have my webserver running under a user named apache.
To view your current user run whoami in the command line.
To change user run su - newuserhere.
If this works, or you are running the web-server under root then you can most likely rule-out any permission issues.
Related
I'm using PHP's exec function to execute a bash script.
<?php
if(isset($_POST['submit']))
{
$input = $_POST['submit'];
exec("sudo -u root sh /var/www/html/script.sh '$input'");
}
?>
<form action="" method="POST">
<input type="text" name="submit" value="">
</form>
As you can see, the exec command is dependent on user input. When the user's input is just one word, everything work fine and the bash script receives the input. When the user input is multiple words however, PHP does not run the exec command. The exact same command that PHP is executing works perfectly fine with multiple words when run from the command line, so it's definitely a problem with PHP.
I have just tried to test it and with your code and my script like this:
printf "$1\n" >> f.txt
it worked.
Only edition was <input type="submit"> to submit the form.
If you put 1 2 3 the result should be 1 2 3.
Check the code below
/var/www/html/script.sh
#!/bin/sh
for i in "$*"
do
echo $i
done
test.php
<?php
if(isset($_POST['submit']))
{
$input = $_POST['submit'];
print exec("sh /var/www/html/script.sh $input");
}
?>
<form action="" method="POST">
<input type="text" name="submit" value="">
</form>
I wanted to implement an automatic calling machine using Raspberry Pi 3, which pronounces the things I send to it via a web form.
I turned my Raspberry into a web server using Apache2 and had the following code in its /var/www/html. I am using google_speech 1.0.15 TTS.
PHP
<?php
if($_POST){
$name=$_POST['name']
shell_exec("/var/www/html/call.sh $name")
}
<html>
<body>
<form action="" method="post">
Name<input type="text" value="name">
<input type="submit" name="submit">
</form>
<\body>
<\html>
call.sh
#!/bin/bash
google_speech -l en "Hello $1"
echo "call.sh executed"
I have my audio system connected to the Pi. The script gets executed, as the last echo statement get printed but the speech output is not coming out of the Pi. I believe the line google_speech -l en "Hello $1" is not getting executed. The script works fine when executed over terminal. What should I do? Is there any better way of implementing the same?
Your tag doesn't have a "name" defined, so it's not being sent:
It is:
Name<input type="text" value="name">
And should be:
Name <input type="text" name="name">
Your code has some glaring security problems, but those lay beyond the scope of your question. I assume you are going to be the sole user for that application hosted on your Pi.
I am working on little project. Task is get ip or address from input field and then run ping command for that ip and finally print result in the webpage.
I have written code:
<FORM ACTION="pingIp.php">
Enter Host: <INPUT name="host">
<INPUT TYPE=SUBMIT VALUE="Submit">
</FORM>
<div class="results"></div>
I don't know how to run ping command and retrieve results on webpage. Can you please give little php code as an example?
I am using the following code to try and execute two different commands from button clicks in a php webpage.
<?php
if (isset($_POST['button'])) { exec('/usr/local/bin/node desktop/server.js'); } ?>
<form method="POST">
<p>
<input type="hidden" name="button" value="1">
<input type="submit" value="Start Video">
</p>
</form>
<?php
if (isset($_POST['button'])) { exec('/usr/local/bin/node desktop/test.js'); } ?>
<form method="POST">
<p>
<input type="hidden" name="button" value="1">
<input type="submit" value="Test">
</p>
</form>
I can get the first command to execute ok to start the server and it works fine but when I then click on the second one nothing happens. I have tried each of them just on the webpage individually and they both work fine on their own but what i want to do is start the server then run the other script, but it won't allow me for some reason?
Please note that nodejs applications normally don't stop. And that exec will wait for the end of the called program. The combination of both will freeze PHP until the nodejs process has finished. Depending on your web server, there can be more or fewer simultaneous PHP processes or threads, but normally this number is limited, so you should avoid this situation. And there is another reason to avoid this situation: Your browser will trying to load the page forever (until timeout) because it never receives the end of the page.
You can simply avoid this problem, if you append an ampersand to the end of the command line:
exec('/usr/local/bin/node desktop/server.js &');
This will make the nodejs progress starting in background, and your PHP script can continue.
Currently I have 2 PHP files. 1 is the user interface and another fetches data from the backend and inserts into the database.
Currently if I use the following in the UI php:
<html>
<body>
<form action = "test.php" name="form" method="post">
<input type="text" name="text_box" size="50"/>
<input type="submit" id="search-submit" value="submit"/>
</form>
</body>
</html>
Upon clicking submit it goes to the test.php. If it possible to execute test.php in the background while remaining on the UI php?
Some of the previous posts talk about using ajax etc which I am not sure how to implement. Possible to do this in php?
In test.php you can use the exec function and call whatever php file you want using php by command line:
exec("php test.php > /dev/null 2>/dev/null &");
Just notice that the session that it will have is not the same as what you have on the browser, and it can be tricky to send parameters to the command line instance that is being initiated, take a look here.