Solved

Execute a shell script from an html link - Passing varables back and forth?

Posted on 2010-11-16
15
409 Views
Last Modified: 2013-12-25
Still need some help this this:

Think I am missing something fundamental here; the submit button works even without entering in the name.

Also how do I pass the name to myscript.sh ???
One other option I would like to log with the name is the Users IP address to be passed to myscript.sh.


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<div style="margin: 10px;">

<h2 class='specialT'> To restart environment </h2><div class='display'>
<h2 class='specialT'>Please Enter your name:</h2><div class='display'>

</p>
<form name="input" action="/cgi-bin/myscript.sh" method="get">
<input type="hidden" name="MANDATORY" value="UserName">
<pre>
          <input type="text" name="UserName"> <input type="submit" value="Submit" />
</pre>
</form>
<p>
0
Comment
Question by:hdaz
  • 10
  • 5
15 Comments
 
LVL 16

Expert Comment

by:jmatix
ID: 34146538
1. To make the UserName mandatory add an onsubmit handler to your form as:

<form name="input" action="/cgi-bin/myscript.sh" method="get" onsubmit="ret = this.UserName.replace(/^\s+/, '') != ''; if (!ret) alert('Name is mandatory'); return ret;">

If you want to

2. UserName will be automatically passed to user shell script when you submit. You shell script can access it in environment variable named $QUERY_STRING.

3. User's hostname and IP address will be available in environment variables $REMOTE_HOST and $REMOTE_ADDR respectively.
0
 
LVL 1

Author Comment

by:hdaz
ID: 34147768
So doing the following in myscript.sh

echo "$QUERY_STRING $REMOTE_HOST $REMOTE_ADDR"

should give me the UserName and Remote host and Remote address but nothing is shown???
0
 
LVL 1

Author Comment

by:hdaz
ID: 34147808
I thought $QUERY_STRING was just for php?
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 16

Expert Comment

by:jmatix
ID: 34148097
QUERY_STRING is a CGI env variable set by the web server. Did you print an HTTP header? Try this:

echo -e "Content-type: text/html\n\n"
echo "<html><head></head><body>"

echo $QUERY_STRING
echo $REMOTE_ADDR

echo "</body></html>"

exit 0
0
 
LVL 1

Author Comment

by:hdaz
ID: 34148453
I added the following:

<form name="input" action="/cgi-bin/myscript.sh" method="get" onsubmit="ret = this.UserName.replace(/^\s+/, '') != ''; if (!ret) alert('Name is mandatory'); return ret;">

I saw it working and was happy not touched anything as far as I know and its stopped working

hmmm
0
 
LVL 1

Author Comment

by:hdaz
ID: 34148499
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<div style="margin: 10px;">

<h2 class='specialT'> To restart environment </h2><div class='display'>
<h2 class='specialT'>Please Enter your name:</h2><div class='display'>

</p>


<form name="input" action="/cgi-bin/myscript.sh" method="get" onsubmit="ret = this.UserName.replace(/^\s+/, '') != ''; if (!ret) alert('Name is mandatory'); return ret;">
<input type="hidden" name="MANDATORY" value="UserName">
<pre>
          <input type="text" name="UserName"> <input type="submit" value="Submit" />
</pre>
</form>
<p>
0
 
LVL 16

Expert Comment

by:jmatix
ID: 34148527
Not sure why it stopped working. Put it in a separate JS function as:

<form name="input" action="/cgi-bin/myscript.sh" method="get" onsubmit="return checkData(this);">


Then define  checkData() in your <script> block in <head> section as:

<script>
function checkData(frm)
{
   var ret = frm.UserName.replace(/^\s+/, '') != '';
   if (!ret)
        alert('Name is mandatory');
   return ret;
}
</script>
0
 
LVL 1

Author Comment

by:hdaz
ID: 34148792
Iv tried but nothing displays

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script type="text/javascript">

<div style="margin: 10px;">

<h2 class='specialT'> To restart environment </h2><div class='display'>
<h2 class='specialT'>Please Enter your name:</h2><div class='display'>

</p>

<form name="input" action="/cgi-bin/myscript.sh" method="get" onsubmit="return checkData(this);">

<input type="hidden" name="MANDATORY" value="UserName">
<pre>
          <input type="text" name="UserName"> <input type="submit" value="Submit" />
</pre>
</form>
<p>


<script>
function checkData(frm)
{
   var ret = frm.UserName.replace(/^\s+/, '') != '';
   if (!ret)
        alert('Name is mandatory');
   return ret;
}
</script>
0
 
LVL 16

Expert Comment

by:jmatix
ID: 34149024
My bad. Forgot to say frm.UserName.value. Try this:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script type="text/javascript">
function checkData(frm)
{
   var ret = frm.UserName.value.replace(/^\s+/, '') != '';
   if (!ret)
        alert('Name is mandatory');
   return false;
}
</script>
</head>
<body>
<div style="margin: 10px;">

<h2 class='specialT'> To restart environment </h2><div class='display'>
<h2 class='specialT'>Please Enter your name:</h2><div class='display'>

<p>

<form name="input" action="/cgi-bin/myscript.sh" method="get" onsubmit="return checkData(this);">

<input type="hidden" name="MANDATORY" value="UserName">
<pre>
          <input type="text" name="UserName"> <input type="submit" value="Submit" />
</pre>
</form>
<p>

</body>
</html>

Open in new window

0
 
LVL 1

Author Comment

by:hdaz
ID: 34149197
Thanks just tried the above still pressing submit without entering name or any data allows the script to be run...

0
 
LVL 16

Accepted Solution

by:
jmatix earned 500 total points
ID: 34149324
I just realized that there is another problem. It should be:

   return ret;

I tested the following HTML and it works for me.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script type="text/javascript">
function checkData(frm)
{
   var ret = frm.UserName.value.replace(/^\s+/, '') != '';
   if (!ret)
        alert('Name is mandatory');
   return ret;
}
</script>
</head>
<body>
<div style="margin: 10px;">

<h2 class='specialT'> To restart environment </h2><div class='display'>
<h2 class='specialT'>Please Enter your name:</h2><div class='display'>

<p>

<form name="input" action="/cgi-bin/myscript.sh" method="get" onsubmit="return checkData(this);">

<input type="hidden" name="MANDATORY" value="UserName">
<pre>
          <input type="text" name="UserName"> <input type="submit" value="Submit" />
</pre>
</form>
<p>

</body>
</html>

Open in new window

0
 
LVL 1

Author Comment

by:hdaz
ID: 34149443
Interesting what are you using to do your testing ?? as it still allowed me to use the submit button

I am using CentOS 5.5 and Apache httpd-2.2.3-31.el5.centos.4

At this moment in time I am using w3m to test, i'll recheck again at but that wont be for a couple of days.
0
 
LVL 1

Author Comment

by:hdaz
ID: 34155488
I have tested with IE Firefox and Chrome as browsers...
0
 
LVL 1

Author Comment

by:hdaz
ID: 34156631
Sorry jmatix: I must apologize, I was giving false information....

i was testing the wrong script..... :)

Many thanks for your help and time.

hdaz
0
 
LVL 1

Author Closing Comment

by:hdaz
ID: 34156652
Thanks for all the help and time and please except my apologize for any confusion yesterday was a really long day.
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes how to create custom column layout styles for Bootstrap. The article uses 5 columns to illustrate the concept, but the principle can be extended to any number of columns.
Use these top 10 tips to master the art of email signature design. Create an email signature design that will easily wow recipients, promote your brand and highlight your professionalism.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

815 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now