Launch shell script with parameters from a webpage button


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Launch shell script with parameters from a webpage button
# 1  
Old 08-27-2014
Launch shell script with parameters from a webpage button

I want to create a simple html page that should contain 2 fields in which the user can write the input. Then I want to have a button that should launch a shell script with the parameters inserted by user in the fields from the webpage.
This is the code behind my webpage:
Code:
<html>
<form action="/EMM/script.php" method="get">
  Start date: <input type="text" name="sdate"><br>
  End date: <input type="text" name="edate"><br>
  Recipient adress: <input type="text" name="adress"><br>
  <input type="submit" value="Submit">
</form>
</html>

This is how my php script looks like :
Code:
#!/usr/bin/php
<?php
    $uri = $_SERVER['REQUEST_URI'];
$output = shell_exec('echo $uri > /tmp/test.txt');
echo "<pre>$output</pre>";
?>

I can seem to be able to grab the URL with the needed variables:
Code:
http://10.21.289.132/EMM/script.php?sdate=345&edate=er&adress=sdfg

And use these variables in the shell_exec section where I can launch a shell script with them as parameters.
What I have tried was to echo the content of $uri to the /tmp/test.txt and to check after execution. The file is re-created each time, but empty.

In the browser, after I press the submit button I can see URL formed correctly (as shown above) and a blank page is loaded with onle the header of my php script:
#!/usr/bin/php displayed.

Observations:
  1. my html page and the php script have full rwx rights, are placed in the /DocumentRoot directory of apache/EMM/ path.
  2. I am not sure at all about my .php syntax, don't know if the scripts needs an interpretor or not.

I do not know if this was the right way to accomplish my task (using html > php > bash). But in this context I don't understand:
  1. why am I not able to pass the variables from the URL to bash shell using my php script (if I would be able I'd also like some php code to break down the URL into the respective variables sdate , edate and adress.
  2. why after I press my button a blank page with just the header of my php script is loaded?

Any ideas how to accomplish my task (preferably using html , php and bash shell). ?

Last edited by rbatte1; 08-27-2014 at 12:03 PM.. Reason: Added LIST=1 tags
# 2  
Old 08-28-2014
Bug

1. The actual reason why the output file test.txt is empty is because the URL contains some special symbols, such as "&", which breaks the issued command. This can be fixed with proper quoting:
Code:
$output = shell_exec("echo '$uri' > /tmp/test.txt");

2. The syntax looks good, apart from the mentioned quoting. The interpretor/hashbang/shebang is not needed. It's outside the php section and is treated as plain text, so you can remove it. Inside the php section it would be a comment.

3. You write you get a blank page, but what did you expect? Due to echo'ing a string and redirecting it to a file there is no output to be returned. Add this to your php script to see some output:
Code:
$output2 = shell_exec('ls -l /tmp/test.txt');
echo "<pre>$output2</pre>";

4. Breaking down the URL is not a good idea. Try to figure out why.

Complete demo incl. shell script for processing the form data:

html file: unchanged

php file (script.php):
PHP Code:
<?php
$field1 
$_GET["sdate"];
$field2 $_GET["edate"];
$field3 $_GET["adress"];
echo 
"$field1 $field2 $field3";
//Obviously, this is a temporary location :D
$output shell_exec("/tmp/process-fields.sh '$field1' '$field2' '$field3'");
echo 
"<pre>$output</pre>";
?>
shell script (process-fields.sh):
Code:
#!/bin/bash
echo "BASH FIELD 1:    $1"
echo "BASH FIELD 2:    $2"
echo "BASH FIELD 3:    $3"

output:
Code:
08/25/2014 08/28/2014 info@example.com

BASH FIELD 1:    08/25/2014
BASH FIELD 2:    08/28/2014
BASH FIELD 3:    info@example.com

All in all it's nice to play with and stuff, but from the security point of view it's a big disaster, imho.
Learn the difference between GET and POST method, implement some form validation etc etc
These 2 Users Gave Thanks to junior-helper For This Post:
# 3  
Old 09-01-2014
Thank you, this solved my problem.
I am aware about the security issues, however this page woulnd;t be visible from internet.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Executing Shell Script from PHP Webpage

Hello , I am trying to execute a small shell script from a PHP Webpage. Below is the shell script : 1.sh $ cat /home/oracle/rahul/1.sh #!/bin/ksh echo "`date` : Report generation in progress" > /home/oracle/rahul/report.txt echo "Run successfully script"; Below is the PHP... (1 Reply)
Discussion started by: rahul2662
1 Replies

2. Shell Programming and Scripting

Read webpage from shell script

Hey experts, I am trying to read a webpage and want to search for a text patter (case insensitive). Please help me acquire this. If the text is found i will receive a mail which can be hardcoded in the script. I am not a big fan of using PERL script. Please help. Machine: AIX... (15 Replies)
Discussion started by: bankimmehta
15 Replies

3. Shell Programming and Scripting

Help to create a Return Button using shell script CGI

Hello Guys, I would like to know how can I create a radio button on that its possible to return to the last page, using a ksh CGI shell script. Can someone help ? Thanks so much !!!:b: I tried this, but it is a javascript code ! <INPUT TYPE="button" VALUE="BACK" ... (2 Replies)
Discussion started by: robdcb
2 Replies

4. Shell Programming and Scripting

Shell Script to launch C program

Hi there, im new too shell scripting and was wondering if it is possible to create a shell script to take in a variable and load a c program. My C program is a file monitor, and is started by using the terminal and using to following code ./monitor FileToBeMonitored is it possible to have... (12 Replies)
Discussion started by: gazmcc182
12 Replies

5. Shell Programming and Scripting

launch vnc session from unix shell script

Hi All, OS:AIX 64 bits Would like to know what is the command to launch vnc session from unix shell script so that Reports server is started from vnc session which should be launched from within the shell script. Thanks for your time! Regards, (0 Replies)
Discussion started by: a1_win
0 Replies

6. Shell Programming and Scripting

Run shell commands via HTML webpage/button?

Hey guys, I got a issue here... we have a development box and a UAT box that our webmasters use, they do the webpage development on the development box and and save changed files to a particular directory on the dev machine. At a certain time of the day a cronjob kicks off and the... (3 Replies)
Discussion started by: zeekblack
3 Replies

7. Shell Programming and Scripting

BASH: how to launch a program with parameters

Hi, I'm a pretty big fan of BASH scripting. I've got a bunch I use for random things and lately a couple issues have been plaguing me. Both are somewhat related, in that they deal with filenames with spaces and "escaped" characters and with launching a program with command line arguements... (5 Replies)
Discussion started by: TinCanFury
5 Replies

8. Shell Programming and Scripting

How to launch a Csh shell script using Excel Macro ?

Hi all. I need to use excel macro at my desktop to launch a csh script which is in a solaris environment. What is the code that i can use in macro to help me with that ? Basically, the code need to telnet or ftp to the solaris environment and just run the script and the macro will output in an... (1 Reply)
Discussion started by: Raynon
1 Replies

9. UNIX for Dummies Questions & Answers

Script for using the Back button and the Close button

Here's a question I have for anyone that might be able to help me: I can write a html script that will allow the user to return to the previous page using the back button, and I can write a script that will allow the user to return to the previous page using the close button, but...is there a... (1 Reply)
Discussion started by: mdgibson
1 Replies
Login or Register to Ask a Question