2

I'm having problems to write in the file with my CGI.

I make a simple HTML formular to press a button and write a phrase in the .txt file, but it doesn't work.

The server is an Apache2.

The scrips works if executed in the terminal.

ps. The final line of CGI works, updating a HTML page URL.

file index.html

<html>
<title>BeagleBone</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
<body>

<div class="w3-container w3-green">
  <h1>BeagleBone GPIO Control</h1> 
  <p>Acesso remoto a GPIO's em rede local</p> 
</div>

<div class="w3-row-padding">
  <div class="w3-third">
    <h2>GPIO x</h2>
    <p>Led blue</p>
    <form method="POST" action="/cgi-bin/led.cgi">
  <input type="submit" name="led" value="ON">
  <input type="submit" name="led" value="OFF">
</form>
  </div>

</body>
</html>

file led.gci

#!/bin/bash

LED_FILE="/home/gpcosta/Desktop/data.txt"

read CONTENT

if [ "$CONTENT" == "led=ON" ]; then
    echo "Apertou botao ON" >> $LED_FILE
elif [ "$CONTENT" == "led=OFF" ]; then
    echo "Apertou botao OFF" >> $LED_FILE
fi

echo "Content-type: text/html"
echo ""
echo '<html>'
echo '<meta http-equiv="refresh" CONTENT="0;url=http://192.168.15.20/index.html">'
echo '</html>'

The cgi permissions below

enter image description here

6
  • Stupid question, is your ScriptAlias directive configured with your /usr/lib/cgi-bin directory ? Commented Oct 17, 2018 at 11:49
  • Hi, Andre! Is my directive is configured to /usr/lib/cgi-bin. The cgi files is found and executed, but echo cmd not works. Thanks about your reply! Commented Oct 17, 2018 at 11:54
  • 1
    Then, what about the permission on the data.txt file ? Apache runs typically with a user that should not have write access to most of the files, especially in your home directory, even if you started it with root. Commented Oct 17, 2018 at 11:57
  • It seems to me the mostly likely reason for your problem is that neither if/else block is being accessed, because the value of "$CONTENT" is not what you think it is. Add printf "CONTENT=${CONTENT}XXX\n" before the if` tests to be sure you CONTENT is as expected. Good luck. Commented Oct 17, 2018 at 15:44
  • Or, since it's a cgi script, comment out the meta tag (which should be in a HEAD tag) and do echo "<body><pre>$CONTENT</pre></body>" Commented Oct 17, 2018 at 15:55

1 Answer 1

1

It's because the web server don't have permissions on the directory.

To solve this, run the following script on your cgi server:

echo “content-type: text/html”
echo “”
echo “<html>
        <head>
                <meta charset='utf-8'/>
                <title>What permission?</title>
        </head>
        <body>
                <p>Run this on your server terminal:</p>
                <pre>chown $(whoami) ${PWD}
chmod 755 ${PWD}</pre>
  </body>
</html>”

Then simply copy the script your web browser will display and paste it on the CGI Server terminal, remember to run it with a priviledged user.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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.