Running bash script using PHP - php

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?

Related

How to grab a form input in HTML using bash?

I am making a little project that grabs the HTML input from a local hosted website and then parses it through bash to open certain amount tabs using Bash.
Pseudo code:
<Input>50<Input> --> Bash grab HTML<Input>50<Input> --> loop 50: chrome.exe google.com
Code of my HTML input field:
<label class="tabAmount">Tab Amount: <input class="tabCount" type="number" name="tabSize" size="4" min="1" max="9999"></label>
So, my main problem is that I need a Bash command to search for the HTML input and then parse it through the script to open the tabs.
Thank you for any help!
This is my html code:
<form action="/cgi-bin/grab_html_input.sh">
Enter Number:<br>
<input type="number" name="number"><br>
<input type="submit" value="Submit">
</form>
This is my bash script:
saveIFS=$IFS
IFS='=&'
parm=($QUERY_STRING)
IFS=$saveIFS
number=${parm[1]}
while (( number > 0 ))
do
<command line command goes here>
done
I don't know the command to open new tab but this is the foundation that I've used to pass HTML input into a bash script.

How to call out names automatically?

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.

How to take real time input output from a python script on PHP

I want to take input from user through a html form, process it through a python script and print the output on html page. The problem is the whole python script is executed each time, while I want the script to give real time output for each input. How can I manage to do this ?
Here is what I am doing so far.
<?php
$vout='';
if(isset($_POST['submit']))
{
$vin=$_POST['input_text'];
$vout=exec('python bot.py '.$vin);
}
?>
<form method="post">
<label>
BotIn: <input type="text" name="input_text">
</label>
<input type="submit" name="submit">
</form>
</br></br>
<label>
<p>Bot: <?=$vout?></p>
</label>
The best way to solve your problem will be running simple python app using, let's say, Flask and hit to specific endpoint you'll make in it. Everything then could take place through localhost, if you'll set it on the same machine.

How to execute a script in a php

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.

Executing two commands on the same page with exec()

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.

Categories