1

I have written a simple HTML code that upon submit, it executes a bash script. The script is in the correct folder, (cgi-bin). When I load this page on firefox. It gives an error as

/somePATH/cgi-bin/script.sh could not be opened because an unknown error occurred.

Is the problem about script permissions? I have tried:

chmod 777

Here is a a part of the HTML code.

<form action="cgi-bin/script.sh" method="post">
<input type="submit" value="Call my Shell Script">
</form>

EDIT: The script basically just prints the date:

#!/bin/bash 
# get today's date
OUTPUT=$(date)
# script
echo "Content-type: text/html"
echo ""
echo "<html><head><title>Demo</title></head><body>"
echo "Today is $OUTPUT <br>"
echo "You can do anything with this Schell Script"
echo "</body></html>"
Nathan Ernst
4,62027 silver badges38 bronze badges
asked Jun 8, 2017 at 18:32
9
  • What does the script look like? Commented Jun 8, 2017 at 18:47
  • @ShammelLee edited in the question Commented Jun 8, 2017 at 18:52
  • What is the contents of the cgi-gin/script.sh? Also, please specify your server/config. Commented Jun 8, 2017 at 18:55
  • @ShammelLee I added that script in the question. I am running it on my local host and HTTP service is running Commented Jun 8, 2017 at 18:57
  • 1
    Are you specifying a shebang line (#!/usr/bin/env bash or similar) at the top of the script? Commented Jun 8, 2017 at 19:00

2 Answers 2

5

You can use a server side language. This is fairly easy with PHP

<?php
 if(isset($_POST['submit']))
 {
 $output=shell_exec('sh /somePATH/cgi-bin/script.sh');
 echo $output;
 }
?>
<form action="" method="post">
<input type="submit" name="submit" value="Call my Shell Script">
</form>

Include all your other HTML and save the file with an extension .php

answered Jun 9, 2017 at 4:07
Sign up to request clarification or add additional context in comments.

Comments

1

Here is a list of things you could check in your httpd.conf regarding CGI:

If you are loading or not the CGI module:

LoadModule cgi_module modules/mod_cgi.so

Verify your aliases for CGI

# ScriptAlias: This controls which directories contain server scripts. 
# ScriptAliases are essentially the same as Aliases, except that
# documents in the target directory are treated as applications and
# run by the server when requested rather than as documents sent to the
# client. The same rules about trailing "/" apply to ScriptAlias
# directives as to Alias.
#
ScriptAlias /cgi-bin/ "/etc/your_httpd_path/cgi-bin/"

The rules of your directory of scripts:

# "/etc/your_httpd_path/cgi-bin/" should be changed to whatever your ScriptAliased
# CGI directory exists, if you have that configured.
#
<Directory "/etc/your_httpd_path/cgi-bin/">
 AllowOverride All
 Options None
 Require all granted
</Directory>

Handlers for different file extensions (add .sh)

# AddHandler allows you to map certain file extensions to "handlers":
# actions unrelated to filetype. These can be either built into the server
# or added with the Action directive (see below)
#
# To use CGI scripts outside of ScriptAliased directories:
# (You will also need to add "ExecCGI" to the "Options" directive.)
#
AddHandler cgi-script .cgi .pl .asp .sh

Check if everything is configured as expected in your environment.

Btw as a good practice: Do not give 777 to the script, give specific permissions for the apache user, find out which user is running the httpd service (it's usually www-data) and do something like:

# remove extra file permissions for others
chmod o-wx /somePATH/cgi-bin/script.sh
# Define the user of the script (same as the user who runs httpd)
chown www-data /somePATH/cgi-bin/script.sh
answered Jun 8, 2017 at 19:36

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.