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.
Related
I have an account on Gator.
I am trying to run a php script as follows
;$command = escapeshellcmd('./simple.py 2>&1');
$output = shell_exec($command);
echo $output
I would like to get as output anything that the python script prints to screen, but the output is empty no matter what I try (I use standard print in python
Use my method as a reference.
I have this two files
run.php
mkdir.py
Here, I've created a html page which contains GO button. Whenever you press this button a new folder will be created in directory whose path you have mentioned.
run.php
<html>
<body>
<head>
<title>
run
</title>
</head>
<form method="post">
<input type="submit" value="GO" name="GO">
</form>
</body>
</html>
<?php
if(isset($_POST['GO']))
{
shell_exec("python /var/www/html/lab/mkdir.py");
echo"success";
}
?>
mkdir.py
#!/usr/bin/env python
import os
os.makedirs("thisfolder");
$command = escapeshellcmd('./simple.py 2>&1');
escapeshellcmd is a form of surrender, it's php stupidity, it gets used as a DWIM band-aid when the programmer has lost control of the command-line.
It's breaking your stderr redirection.
Use escapeshellarg where you need to escape shell stuff.
$command = escapeshellarg('./simple.py') . ' ' . '2>&1';
I'm new to php and html (internet languages in general) and I would like to execute a python script that takes a picture from my php page.
I did some research, edited some code (according to my understanding) and came up with this, but it doens't do anything.
<!DOCTYPE html>
<html>
<head>
<title>Test</title>
</head>
<body>
<form action="" method="post">
<button type="submit" name="sub" value="call">Click</button>
</form>
</body>
</html>
<?php
if(isset($_POST['sub']))
{
exec('sudo python take_picture.py');
echo "Picture captured";
}
?>
I managed to execute the python script from a html page invoking a simple php script separately, but when it takes the picture it sends me to a new page (blank page with the text displayed) which I don't want. Here are the codes:
HTML button:
<form action="take_picture.php" method="post">
<button>Click</button>
</form>
PHP script:
<?php
exec('sudo python take_picture.py');
?>
What I need is to simply press the "click" button and take the picture without sending me to anywhere else.
Could you please guys aid me to achieve what I need and explain with apples what I was doing wrong or what I missed.
Thank you in advance!
You can make an :
<?php
$command = escapeshellcmd('/usr/custom/test.py');
$output = shell_exec($command);
echo $output;
?>
More information on this Stack Overflow Question.
Running a Python script from PHP
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.
This is a repost because my question before was not clear enough and deleted it to prevent further confusion.
I have a shell script that takes one argument. It uses the argument to cURL a website which then uses grep, head and ${variable#*>} to extract some data from a website.
In terminal everything works well with the command below. $stockCode being the argument. ./priceAlert.sh $stockCode
I tried to make this into a working webpage on my local host. (MAMP osx) I tried different solutions but the website will always spit out "not from same origin" except for the shell script.
After searching for how to integrate shell scripts with web I came across PHP and now I am figuring out how to get PHP to talk to my shell script.
Structure of my project:
HTML page with an input inside a form
<form action="priceAlert.php" method="post">
<input type="text" name="stockCode"></input>
<input type="submit" name="submit" value="Submit me!">
</form>
A PHP page that receives the input from the HTML page via POST
$stockCode2 = $_POST['stockCode'];
echo $stockCode2;
$output = shell_exec('sh priceAlert2.sh ' . $stockCode2);
echo "<pre>$output</pre>";
Now the interesting part...
The value of $stockCode2 from the HTML form is 033360.
If I use $stockCode2 as POST, the shell script extracts the part of the website that I do not want. (The code above extracts the part I do not want)
If I hard code the value, 033360, into $stockCode2 the shell script extracts the part I want. The code below extracts the part of the website I do want.
$stockCode2 = '033360';
$output = shell_exec('sh priceAlert2.sh ' . $stockCode2);
echo "<pre>$output</pre>";
If I echo everything they all show the value 033360 but behave differently.
Is there a way to fix this? Could this be a way for the target site to prevent scraping?
Thank you.
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.