LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   HTML Form that sends data entered to a bash script as variables (https://www.linuxquestions.org/questions/programming-9/html-form-that-sends-data-entered-to-a-bash-script-as-variables-772710/)

simplified 12-01-2009 11:11 AM

HTML Form that sends data entered to a bash script as variables
 
Hi All

I've been banging my head against the wall with this one. I have a script that runs as follows:

Code:

./script.sh var1 var2 var3
This all works well in the shell (bash) with the variable arguements. However, what I really want to do it run this script from a webpage so that a user can complete a form and the separate entries for each form box are read into the script as arguements. Unfortunately I can't get it to work... here's the HTML code:

Code:

<html>
<link href="stylesheets/common.css" type="text/css" rel="stylesheet">

<br>
<br>
<h1>Enter Customer Details Below:</h1>

<form name="input" action="/cgi-bin/addcustomer.sh" method="get">
<type="text" name="1" value="webui">
Customer name (Company): <br>
<input type="text" name="CUSTOMER_NAME" />

<br>
IP Address: <br>
<input type="text" name="IP_ADDRESS" />

<br>
<input type="submit" value="Submit" />

</form>

</html>

I've got the script "addcustomer.sh" in the right directory for the CGI as specified in the apache2 configuration but it doesn't run. Permissions should be correct as well as I'm basically tweaking the Nagios gui to add in new "customers". The script works fine in the shell, but just won't happen when I use the webpage. I get the following error onscreen:

The requested URL /cgi-bin/addcustomer-ips.sh was not found on this server

... but it definately is in the cgi-bin directory!

It's worth noting that the bash script is written so that if $1 = "webui" it knows that it's being run from the apache2 server and not the command line so it skips the interactive parts of the script... I didn't want to write the same thing twice ;) . Also "CUSTOMER_NAME" and "IP_ADDRESS" are variables in the script.

Any ideas or a point in the right direction would be most appreciated!

TIA, Simplified

simplified 12-01-2009 03:32 PM

Hi All

Here's an update...

I've actually gotten this to work by using a php page. Here's the block of code I used, if you have this problem I hope it helps you out!

The first page is a form: (filename: new_customer.php)
Code:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Add new customer </title>
</head>
<link href="stylesheets/common.css" type="text/css" rel="stylesheet">

<body>

<h1>Add new customer for monitoring</h1>

<form method="post" action="web-add_new_customer.php">

Customer Name: <br />
<input type="text" name="CUSTOMER_NAME" size="35" />
<br />

IP Address: <br />
<input type="text" name="IP_ADDRESS" size="35" />
<br /> <br />

<input type="submit" value="Submit new customer" />
<br />
</form>

</body>
</html>

...and then after submission, I coded a page that displayed what had been entered and then ran the variables through the script. (filename web-add_new_customer.php)

Code:

<?php
$CUSTOMER_NAME = $_POST['CUSTOMER_NAME'];
$IP_ADDRESS = $_POST['IP_ADDRESS'];


if(empty($CUSTOMER_NAME ) || empty($IP_ADDRESS )) {
echo "<h2>You must fill in all fields</h2>\n" ;
die ("Click Back to start again.");
}
echo "<h2>You Entered the following information:</h2>";
echo "<b>Customer name:</b><br><br>";
echo $CUSTOMER_NAME;
echo "<br><br><b>IPS5500 IP Address:</b><br><br>";
echo $IP_ADDRESS;
?>

<?php
exec ( "/scripts/addcustomer.sh \"$CUSTOMER_NAME\" $IP_ADDRESS" );
?>

Yes - the code does need clearing up but I just thought that you'd like to see hte working article! :D

***Big hint*** - you will need to ensure that the script (in the above example /scripts/addcustomer.sh) is executable by the user www-data (or equivalent user).

Thanks to all that took a peek!

simplified 12-01-2009 03:35 PM

p.s. if you're not having any luck with the user permissions for user www-data, try sticking this code in - it'll tell you who the user is :D

Code:

<?php
// outputs the username that owns the running php/httpd process
// (on a system with the "whoami" executable in the path)
echo exec('whoami');
?>



All times are GMT -5. The time now is 01:37 PM.