LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 12-01-2009, 12:11 PM   #1
simplified
Member
 
Registered: May 2007
Location: London, UK
Distribution: Kubuntu 9.04 x64 / Ubuntu Server 9.04 x64
Posts: 50

Rep: Reputation: 15
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
 
Old 12-01-2009, 04:32 PM   #2
simplified
Member
 
Registered: May 2007
Location: London, UK
Distribution: Kubuntu 9.04 x64 / Ubuntu Server 9.04 x64
Posts: 50

Original Poster
Rep: Reputation: 15
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!

***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!
 
Old 12-01-2009, 04:35 PM   #3
simplified
Member
 
Registered: May 2007
Location: London, UK
Distribution: Kubuntu 9.04 x64 / Ubuntu Server 9.04 x64
Posts: 50

Original Poster
Rep: Reputation: 15
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

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');
?>
 
  


Reply

Tags
bash, form, php, script


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
array variables from html form not processed hil4d Linux - Server 2 04-20-2008 11:11 PM
Script to get form data..in bash? jrfly Linux - General 1 06-29-2005 11:04 AM
capture html form data blizunt7 Programming 12 06-19-2005 09:31 PM
PHP:Unable to post variables from html form to mysql database vikram_cvk Linux - Software 1 09-29-2004 04:01 PM
Problems with php 4.2.1 form-data and hidden variables coea77 Programming 1 10-10-2002 04:24 AM


All times are GMT -5. The time now is 08:48 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration