How to pass textbox value to bash script - php

This is my first question, CMIIW :
here my sample php script
<form method="GET" action="/var/www/cgi-bin/mode2.sh">
<table align="center" nowrap>
<tr>><td>IP address :</TD><TD><input type="text" name="ip"></td></tr>
<tr><td>netmask :</TD><TD><input type="text" name="ip2"></td></tr>
</tr></table>
<input type="submit" value="Send">
<input type="reset" value="Reset"></p></form>
bash script :
#!/bin/bash -x
echo "IPADDR=$ip" >>/etc/sysconfig/network-scripts/ifcfg-eth0
echo "NETMASK=$ip2" >>/etc/sysconfig/network-scripts/ifcfg-eth0
I want input textbox value form web browser for example IP and netmask and send the value to bash and save to ifcfg-eth0 file. i know it sound risky, i just want to learn. any sugestion?

Instead of having action attribute point to shell script you want to run, Make action attribute to point to php file like
<form method="GET" action="UserInputIPAddrPHP.php">
And in the UserInputIPAddrPHP.php you can get the User Inputted IP and Netmask as follows
<html>
<body>
<?php
$IP_Addr = $_GET['ip'];
$NetMask = $_GET['ip2'];
$command="/path/to/mode2.sh ".escapeshellarg($IP_Addr)." ".escapeshellarg($NetMask);
exec ($command,$output=array(),$return_value);
if($return_value!==0) {
#print appropriate message
}
?>
</body>
</html>
$command is actual command that you type to run the script at your shell. So set it accordingly.
Write your script mode2.sh like this (mind the quotes):
#!/bin/sh
command "$1" "$2"; # command is actual command you want to run like cp,mv etc
Render it executable:
me#somewhere$ chmod +x mode2.sh
Hope this might help you
Thanks

You should not use bash as cgi script.
But if there is really a need, you can get IP and netmask by manipulating variable $QUERY_STRING
#!/bin/bash
echo Content-type:text/plain
echo ""
read IP MASK <<< $(echo $QUERY_STRING | sed -r 's/&?ip2?=/ /g')

Related

PHP doesn't execute bash script when user input is multiple words

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>

shell script wiork on console but not from php page

I have a problem, I need to launch a .sh script from a web page, just does not go, starting from the terminal the script works and does what it has to do, but from the web no, but in the ' Apache2 error.log does not make any mistakes, I do not understand what it can be ..
HTML:
<tr>
<td>JTS</td>
<td>
<form action="JTSstart.php">
<input type="submit" value="START">
</form>
</td>
<td>
<form action="JTSres.php">
<input type="submit" value="RESTART">
</form>
</td>
<td>
<form action="JTSstop.php">
<input type="submit" value="STOP">
</form>
</td>
</tr>
PHP:
<?php
echo exec('bash JTSstop.sh');
sleep(5);
header("Location: 5ondimba.html");
?>
SH:
#!/bin/bash
cd /home/otaku/JTS3ServerMod_HostingEdition
./jts3servermod_startscript.sh stop
What I have tried / tested:
1) The exec command, such as shell_exec, is not disabled in the php setup.
2) The files were converted with dos2unix.
3) bash -x on the script and does not report any kind of error (in fact, from console works).
what could it be?? how can i make it work?
Thanks so much!
I see a few problems:
1) Get rid of echo. At best, it will display output of the command (if there is any), but if that happens it will cause your subsequent header() to fail - header() must be called before any output is generated.
2) PATHs are not the same in PHP as in your SHELL on the console. Don't assume bash or JTSstop.sh are in PHP's PATH, or in PHP's current working directory. Better to always fully specify paths.
3) Your script already includes #!/bin/bash, no need to call it with bash again.
PHP:
<?php
exec('/full/path/to/JTSstop.sh');
sleep(5);
header("Location: 5ondimba.html");
?>
But why not keep things simple and get rid of JTSstop.sh all together?
<?php
exec('cd /home/otaku/JTS3ServerMod_HostingEdition; ./jts3servermod_startscript.sh stop');
sleep(5);
header("Location: 5ondimba.html");
?>
If you're still having problems, you can see the results of the exec by specifying a 2nd parameter, as described in the docs.
<?php
exec('cd /home/otaku/JTS3ServerMod_HostingEdition; ./jts3servermod_startscript.sh stop', $output);
print_r($output);

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.

HTML form button to run PHP to execute Python script

I am building an HTML document that is meant to run locally. On it is a button that I would like to have run a Python script when clicked. I'm trying to use a PHP-generated button. There's no input or output that I want to associate with the button; I just want it to run the script, which produces charts as image files that the rest of the page uses. I have tried a couple different ways to do it, including putting the PHP part in the HTML document, before the HTML:
<?php
if (isset($_POST['update']))
{
exec('python myScript.py');
}
>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<form name="update" method="post" >
<button name = "update" type="submit"> Update charts </button>
</form>
</body>
I've also tried making the PHP code its own .php document in the same directory and calling it from the HTML code as so:
<form action = "updateCharts.php" method="post">
<input type="submit" name="update" />
</form>
where updateCharts.php is:
<?php
system('cd C:\My\Script\Path');
system('python MyScript.py');
?>
I've also tried substituting "system" with "exec" but to no avail. In the first part, I click the button and nothing happens. In the second, I click the button and I am taken to the text of the PHP document. In neither case does my Python script run!
Does anyone see what I'm missing? I'm admittedly a novice with php so it may be something glaring. Thanks for the help!
What this:
$command = escapeshellcmd('python /My/Script/Path/myscript.py');
// or
// $command = escapeshellcmd('/My/Script/Path/test.py');
// But you have to make your script executable doing: chmod +x myscript.py
$output = shell_exec($command);
echo $output;
This can not work.
<?php
system('cd C:\My\Script\Path');
system('python MyScript.py');
?>
You can not have a "session" over multiple system calls. Every system call has its own shell environment.
At least on Linux systems you can cascade commands by semicolons. If you need a work directory to be set before script execution, you could either do
system('cd C:\My\Script\Path ; MyScript.py');
or
chdir('C:\My\Script\Path');
system('MyScript.py');
However, script execution from PHP can be blocked, or apache might not have appropriate file permissions to the script, or, or, or.
You need to check log files and also post what actually is output.

shell script executed in HTML/PHP

I have some basic knowledge of HTML/PHP. The situation I am facing is frustrating. What I want to accomplish is to create a simple search box on a web page, when the user puts in input and clicks submit then my shell script is executed and then presented on a php page. I have been successful in getting other commands to run when I click submit to make sure the PHP exec shell command is working.
I will see the output on the web page. Just not my script. My script uses an argument to pass and works thru command line. Below is the details of my script, HTML, and PHP page. Also, I'm using a FreeBSD 10 box.
My Script
Command Line -
$ csearch "argument"
#!/bin/sh
grep -ir -B 1 -A 4 "$*" /usr/local/var/rancid/CiscoDevices/configs
My HTML page
<html>
<body>
<form method="POST" action="csearch.php">
<input type="text" name="searchText">
<input type="submit" value="Search">
</form>
</body>
</html>
My PHP page
<?php
$searchText=$_POST['$searchText'];
?>
<html>
<?php
$output = shell_exec('/usr/local/bin/csearch $searchText');
echo "<pre>$output</pre>";
?>
</html>
Any help is greatly appreciated.
shell_exec('/usr/local/bin/csearch $searchText'); This isn't what you expect it to be:
<?php
$searchText = 'foobar';
$cmd = '/usr/local/bin/csearch $searchText';
echo $cmd;
?>
outputs:
/usr/local/bin/csearch $searchText
Change the string to use double quotes and $searchText will actually be what you want to it be:
$output = shell_exec("/usr/local/bin/csearch $searchText");
More info on the use of quotes in PHP.
As #uri2x hints in a comment:
$searchText=$_POST['$searchText']; should be changed to $searchText=$_POST['searchText']; for a similar reason.

Categories