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);
Related
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.
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.
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.
I am attempting to load a webpage on my own server which will run a .bat script (on the same server) as below.
When I access the page, called test.php, it display the 'DO IT!' button and when I press it, it just display the content on the .bat file rather than executing it on the server...
What do I need to configure on the server, I assume in the PHP settings, to force it to run the script rather than just display it on the webpage?
For the purpose of the question, I am happy about the security implications of what I am doing.
I am running a Windows machine with IIS and PHP.
<html>
<head>
<title>Restarting</title>
</head>
<body>
<?php
if(isset($_POST['submit']))
{
echo exec('c:\scripting.bat');
echo "Done!";
} else {
// display the form
?>
<form action="" method="post">
<input type="submit" name="submit" value="DO IT!">
</form>
<?php
}
?>
</body>
</html>
I think that the echo exec('c:\scripting.bat'); line it's causing you the problem. Try to just execute it without the echo statement.
If you trying to see the output of the function, you must use the second functions parameter: &$output, acording to the documentation itself. See it in the docs here.
I hope it will be useful to you! :D
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')