PHP execute shell script and provide inputs for prompts - php

I have an apache server on a raspberry pi. I have a basic program where the user inputs a string into an html form box. Clicking submit sends the entry to a php file, which then calls a shell script, and passes the entry as a prompt. If you look at the php below, it has a variable with the user input, and it calls the shell file main.sh. How can I provide the shell program that variable as an input. I have looked at proc_open, but haven't been able to get it to work. Thanks
Index.html:
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<form action="script.php" method="post" enctype="multipart/form-data">
Address: <input type="text" name="Address" value=""><br>
<input type="submit" value="Upload" name="submit">
</form>
</body>
</html>
script.php:
<?php
if(isset($_POST["submit"])) {
if($_POST['Address'] != "")
{
$entry = $_POST['Address'];
$output = shell_exec ("./main.sh");
echo $output;
}
}
?>
and finally the shell file: main.sh:
echo -n "Do you like pie (y/n)? "
read answer
if echo "$answer" | grep -iq "^y" ;then
echo Yes
else
echo No
fi

you can pass variable to shell script as follows
$output = shell_exec("./main.sh $entry");
and in shell script , Add x = $1 . $x would be variable, with your user input value.
Look for this post for further details https://stackoverflow.com/a/19460686/3086531

Related

Html Input into script.sh with PHP excec on Raspberry

I have created a Webserver on my Raspberry. I am able to excecute simple command scripts with PHP, like here:
<?php
if (isset($_POST['on'])) {
exec('sudo /var/www/html/scripts/camOn.sh');
}
?>
<form action="" method="post">
<button type="submit" name="on">On</button>
</form>
I have following Text to Speech script
#!/bin/bash
say() { local IFS=+;/usr/bin/mplayer -ao alsa -really-quiet - noconsolecontrols
"http://translate.google.com/translate_tts?ie=UTF-8&client=tw-ob&q=$*&tl=En-us"; }
say $*
Now I would like to create a Input Text field in Html thats inserts what text I write inside the script, instead of "$*" and executes it after that.
I really have no idea how to do that.
Please help me a little :)
<?php
if (isset($_POST['tts'])) {
exec('sudo /var/www/html/scripts/speech.sh');
}
?>
<form action="" method="post">
Text To Speech:<br>
<input type="text" name="tts"><br>
Send<br>
<input type="submit">
</form>
So in your PHP script you need to do this:
exec('sudo /var/www/html/scripts/speech.sh "' . $_POST['tts'] . '"');
Then in your bash script the following change:
say $1

PHP simple whois script html form

Im just creating a simple tool in PHP what uses the built in whois command in linux and then echo's the response back to the user. Everything I have attempted has either failed or I have messed up somewhere.
<html>
<body>
<form method="POST" action="">
<input type="text" name="cmd1">
<input type="submit" value="Submit">
</form>
</body>
</html>
<?php
$cmd = $_POST['cmd1'];
echo "<pre>".shell_exec('whois ', $cmd)."</pre>";
?>
For me nothing happens, for the record I am new to PHP and I would like just a little expertise. Im the only one that is going to be using the script. Thank you.
You need to check whether a POST variable was submitted, so you don't try to run whois when you're first displaying the form. You should also escape the parameter to prevent command injection.
And you need to display the results inside the <body> of the HTML.
Another problem: you need to concatenate 'whois ' with the parameter, not pass them as separate arguments to shell_exec (it only takes one argument, and ignores the extra argument, so you were just executing the command whois with no domain).
<html>
<body>
<form method="POST" action="">
<input type="text" name="cmd1">
<input type="submit" value="Submit">
</form>
<?php
if (isset($_POST['cmd1'])) {
$cmd = $_POST['cmd1'];
echo "<pre>".shell_exec('whois ' . escapeshellarg($cmd))."</pre>";
}
?>
</body>
</html>
You can also use Net_Whois:
Install:
pear install Net_Whois
Usage:
<?php
require_once "Net/Whois.php";
$server = "whois.denic.de";
$query = isset($_POST['cmd1']) ? $_POST['cmd1'] : 'phpcrawler.de';
$whois = new Net_Whois;
$data = $whois->query($query, $server);
echo $data;
?>

Executing a .sh file with php

I have written a very simple Hello World script as a bash file and saved it on my desktop.
I want to execute this file when a Submit button is pressed.
The following script works fine and I see the Hello World on the firefox webbrowser:
<?php
echo shell_exec('sh /home/administrator/Desktop/Helloworld.sh');
?>
However the following script does not give me any result:
<!DOCTYPE html>
<html>
<body>
<?php
if (isset($_POST['Submit1'])) {
echo shell_exec('sh /home/administrator/Desktop/Helloworld.sh');
}
?>
<Input Type = "Submit" Name ="Submit1" Value = "Save Parameters">
</body>
</html>
Any idea why? I don't get any errors in my log file.
You are not posting anything, for that you'll need a form, i.e.:
file.php
<html>
<body>
<?php
if (isset($_POST['Submit1'])) {
echo shell_exec('sh /home/administrator/Desktop/Helloworld.sh');
}
?>
<form action="file.php" method="post">
<input Name= "Submit1" type="submit">
</form>
</body>
</html>
Also, make sure the file /home/administrator/Desktop/Helloworld.sh is executable, i.e.:
chmod +x /home/administrator/Desktop/Helloworld.sh

Executing BashScript with PHP Issues

I have a bash script that's tied into some python scripts that I would like to execute within a webpage and cat the file it creates on to the page. Currently I haven't been able to execute the script at all (tested the script multiple times to ensure it is in fact working), and I can't seem to find where my error is. The first set of code is from my 'dashboard.php' file which is in /var/www. I have changed the permissions for the scripts to 777 and made executable.
<html>
<body>
<form action="welcome.php" method="post">
IP/CIDR Input: <input type="text" name="cidriptext" />
Single IP: <input type="checkbox" name="singlech" value="single" />
CIDR Range: <input type="checkbox" name="cidrch" value="cidr" />
<input type="submit" value="Submit">
</form>
<form action="welcome.php" method="post">
List of IP's: <input type="text" name="listname" />
<input type="submit" value="Submit">
</form>
</body>
</html>
This second script is the page ('welcome.php') it reaches out to execute the script with the input from the form. At this point I don't expect the $listname_text to work properly, but I do expect to get the 'Incorrect Input' error when no boxes are checked, the 'testing' output at the end, and the files being created on the backend.
<?php
$ipcidr_text = $_POST['cidriptext'];
$listname_text = $_POST['listname'];
if !(empty($listname_text):
shell_exec('/var/www/getStarted -l $ipcidr_text');
$output0 = shell_exec('echo "List of IP's"');
echo "<pre>$output0</pre>";
elseif (isset($_POST['cidrch'])):
shell_exec('/var/www/getStarted -c $ipcidr_text');
$output1 = shell_exec('echo "CIDR Range"');
echo "<pre>$output1</pre>";
elseif (isset($_POST['singlech'])):
shell_exec('/var/www/getStarted -s $ipcidr_text');
$output2 = shell_exec('echo "Single IP"');
echo "<pre>$output2</pre>";
else:
$output = shell_exec('echo "Incorrect input"');
echo "<pre>$output</pre>";
endif;
$testing = shell_exec('echo "testing"');
echo "<pre>$testing</pre>";
?>
PHP is working, I am able to execute a basic script:
<?php
$output = shell_exec('echo "Incorrect"');
echo "<pre>$output</pre>";
?>
If you need anymore information, please let me know. Appreciate any help!
I've had issues with shell_exec() in the past and tend to lean on system() instead, also try " instead of ' to encapsulate the command. You may also need to change how you call your script:
From:
/var/www/getStarted
To (call the python interpreter specifically):
/usr/bin/python /var/www/getStarted

php script to excute bash script and display results on webpage.

I'm trying to create very simple page that will look up email address in a logfile
I wrote a bash script (with complex awk and sed queries ) that accepts single argument (email address) and it will display the output as follows.
DATE email phone
31/1/2013 test#example.com 1800-000-000
how can I go about creating a webpage that will only take an email address and a search button that will simply execute the bash script in the backend and display the output to the screen?
Thank you
exec ("/path/to/script", $output);
That will output result into the $output variable.
You can then create page.
<html>
<body>
<form action="index.php">
<input type="text" name="address" />
<input type="submit" value="Post!" />
</form>
</body>
</html>
In the index.php you can put such code:
<?php
if ($_POST && $_POST['address']) {
exec ("/path/to/script " . escapeshellarg($_POST['address']), $output);
echo $output;
}

Categories