Find the answer to your Linux question:
Results 1 to 5 of 5
hi guys, i created a simple web hosting registration bash script that creates a new user in my web hosting system. Problem is that i want to automate it, that ...
  1. #1
    Just Joined!
    Join Date
    Feb 2009
    Posts
    3

    run bash script from cgi-bin or as cgi

    hi guys,
    i created a simple web hosting registration bash script that creates a new user in my web hosting system. Problem is that i want to automate it, that user can register themselves from html web form sending their request to this bash script in cgi-bin directory (or maybe modify this bash script as cgi etc..). How can i do that, if its only possible...
    her's my script:

    Code:
    #!/bin/bash 
    # 
    # 
     
    usernamePrefix="" 
    username="$usernamePrefix$1" 
    password="$2" 
    epastas="$3" 
    quota=100000 
    dbname_user="$username" 
     
    userLen=`perl -e "print length(\"$username\")"` 
     
    if [ $userLen -gt 20 ]; then 
       echo "Username: $username is too long ($userLen chars > 20), exiting" 
       exit 
    fi 
     
    cryptPassword=`perl -e "print crypt(\"$password\",\"xx\")"` 
     
     
    echo "Username = $username Password = $password" 
    echo "-----------------------------------------" 
     
    # create the group and user 
     
    echo "* Creating group $username" 
    groupadd $username 
     
    echo "* Creating user $username" 
    useradd --create-home --password $cryptPassword --shell /bin/false --comment hosting -g  $username $username 
     
    # personalise skel files 
     
    echo "* Copying helper files and personalising" 
    sed -e "s/<h1>Tips/<h1>Site for $username/g" /home/$username/public_html/tips.htm > /home/$username/public_html/index.htm 
     
    # set quota 
     
    echo "* Applying quota" 
    setquota -u $username $quota $quota 0 0 -a 
     
    # create database 
     
     
    echo "* Creating MySQL database for User" 
    echo "CREATE DATABASE $dbname_user; GRANT ALL ON $dbname_user.* to '$username'@'localhost' identified by '$password';" | mysql -u root -ppassword 
     
    # setting permissions 
     
    echo "* Setting permissions and ownership" 
     
    chmod 755 /home/$username/public_html 
    chmod 644 /home/$username/public_html/* 
    chmod -R 755 /home/$username/public_html/cgi-bin 
    chown -R $username.website-hosting /home/$username/public_html 
     
    echo 
    echo "* All done!" 
    echo
    How can i extract $1 $2 and $3 variables from html form to this script?
    When i put this script to cgi-bin directory i always get 500 server error...

    waiting for the answers,
    Thank you,
    Justin

  2. #2
    Linux Newbie danielsmw's Avatar
    Join Date
    Nov 2006
    Location
    Clemson, SC / Charleston, SC
    Posts
    110
    To be honest, I didn't even realize you could use bash scripts for CGI, but I researched it after I saw this and saw that you could. Very neat!

    Anyway, you may want to take a look at this tutorial: Create a GCI using bash scripting. It has a very basic explanation of writing a form with bash, although I'm not entirely sure that this is what you want. This guy basically puts conditionals in the beginning of his script that deal with what to do when someone submits a query and the page is reloaded with a new argument. The important part is that he gives you an example of how to interpret submitted parameters with sed.

    Also make sure that bash scripts are registered as a type in your httpd.conf file, and that your script has the right permissions to be executed!
    Registered Linux User: #479567
    Asking a question? Read this page first.
    Now... sudo make me a sandwich.
    ratiocinativeroot.blogspot.com

  3. #3
    Just Joined!
    Join Date
    Feb 2009
    Posts
    3
    well, thank you !
    i followed the guide that you posted and tried to run sh examples as cgi in cgi-bin directory, but suddenly faced internal server error 500

    malformed header from script. Bad header=<html>: cgi2.sh

    well i googled around and found that thers smth with headers:

    so i tried several variants but still no success:

    #!/bin/sh
    echo "Content-type: text/html\n"

    or

    #!/bin/sh
    echo "Content-type: text/html\n\n"

    or

    #!/bin/sh
    echo "Content-type: text/html\r\n"

    still error 500

    well i also didint find wher /how to register bash scripts as a type in apache2.conf

    maybe this causing an error ?

  4. #4
    Just Joined!
    Join Date
    Apr 2009
    Posts
    1
    Hey, totally new to this forum but I found this trying to search around the net for more info on bash-cgi.

    I thought I'd reply because a month or so doesn't really seem like utter necromancy and someone might still benifit from help.

    I was having the same problem as this with my script

    I needed

    echo "Content-type: text/html"
    echo ""

    at the top.

    This fixed the header error

    EE.

  5. #5
    Just Joined!
    Join Date
    Jan 2008
    Posts
    53

    Smile

    you can use the following lib Creating CGI Programs with Bash: Handling POST Data - TigerTronics

    How to use it?

    Easy, your code will look something like this:

    form.html:
    <html>
    <head><title></title></head>
    <body>
    <form action="/cgi-bin/test.sh" method="post">
    Type name here:
    <input name="username" type="text"/>
    <br />
    Type password here:
    <input name="password" type="password"/>
    <br />
    Type the pasta here:
    <input name="epastas" type="text"/>
    </form>
    </body>
    </html>
    test.sh:
    #!/bin/bash

    #Adding lib i have downloaded from
    #Creating CGI Programs with Bash: Handling POST Data - TigerTronics
    . get_post_lib.sh

    echo "Content-type: text/html"
    echo ""
    echo "<html><head><title></title></head><body>"
    echo "Your username is: "$username", your password is: "$password", and your pasta is: "$epastas
    # adding your script here:
    usernamePrefix=""
    # I comment this because you get it already from the lib.
    #username="$usernamePrefix$1"
    #password="$2"
    #epastas="$3"
    quota=100000
    dbname_user="$username"

    userLen=`perl -e "print length(\"$username\")"`

    if [ $userLen -gt 20 ]; then
    echo "Username: $username is too long ($userLen chars > 20), exiting"
    exit
    fi

    cryptPassword=`perl -e "print crypt(\"$password\",\"xx\")"`


    echo "Username = $username Password = $password"
    echo "-----------------------------------------"

    # create the group and user

    echo "* Creating group $username"
    groupadd $username

    echo "* Creating user $username"
    useradd --create-home --password $cryptPassword --shell /bin/false --comment hosting -g $username $username

    # personalise skel files

    echo "* Copying helper files and personalising"
    sed -e "s/<h1>Tips/<h1>Site for $username/g" /home/$username/public_html/tips.htm > /home/$username/public_html/index.htm

    # set quota

    echo "* Applying quota"
    setquota -u $username $quota $quota 0 0 -a

    # create database

    echo "* Creating MySQL database for User"
    echo "CREATE DATABASE $dbname_user; GRANT ALL ON $dbname_user.* to '$username'@'localhost' identified by '$password';" | mysql -u root -ppassword

    # setting permissions

    echo "* Setting permissions and ownership"

    chmod 755 /home/$username/public_html
    chmod 644 /home/$username/public_html/*
    chmod -R 755 /home/$username/public_html/cgi-bin
    chown -R $username.website-hosting /home/$username/public_html

    echo
    echo "* All done!"
    echo
    echo "</html>"


    Some recomendations:
    1) Add a captcha in the form.
    2) Only trying like this you will have perm problems, try to add a sudo way.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...