Bash script for deployment as a cgi - apache

i have a bash script that copies and modifies a bunch of configuration files in my app running as a cgi for fast deployment and testing.
This script is in the public_html folder of my hosting and it works perfectly, permissions set to 755.
I am making other cgi scripts in a subfolder public_html/foo/bar.cgi and giving it 755 permission.
#!/bin/bash
echo "Content-type: text/html"
echo ""
# test if we are in the correct place
touch foo 2>&1
echo '<html>'
echo '<head>'
echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">'
echo '</head>'
echo '<body>'
echo '<p>Hello World</p>'
echo '<p> </p>'
echo '<hr/>'
echo '</body>'
echo '</html>'
exit 0
This doesn't work, the server answers with a 403.
I have checked the folder permission, and set it to 755 and 777 without results
UPDATE: Changed name from test.cgi to testscript.cgi and it's woking now

Related

ssh on remote server with nohup - not able to create files

I'm calling a shell script on a remote server with ssh ssh user#host '/dir/myscript.sh'and myscript.sh then runs another script with nohup, but the 2nd script isn't writing to nohup / to any other file with echo "" > test.txt.
myscript.sh
#!/bin/bash
shopt -s expand_aliases
source /home/user/.bash_profile
nohup /dir/myscript-real.sh > my.out 2> my.err < /dev/null &
myscript-real.sh
#!/bin/bash
shopt -s expand_aliases
source /home/user/.bash_profile
echo -e "test"
sleep 15
echo "2nd test"
echo "test" > test.out
exit
When ran via SSH - no file is being created (my.out, my.err or test.out), but when running myscript.sh directly - those 3 files are being created.
Any ideas where is the problem?
Thanks.

Strange behaviour with CGI

I've a small project almost finished. In it, I use some .cgi files in which I make communication between a microcontroler and a webserver. The problem is the following:
If I use this code the project works fine:
#!/bin/bash
./moveRight
echo "Status: 204 No Content"
echo "Content-type: text/html"
echo ""
but when I use this code, nothing happens:
#!/bin/bash
echo "Status: 204 No Content"
echo "Content-type: text/plain"
echo ""
mkdir /tmp/stream
raspistill --nopreview -w 640 -h 480 -q 5 -o /tmp/stream/pic.jpg -tl 100 -t 9999999 -th 0:0:0 &
chmod 777 /dev/ttyAMA0
LD_LIBRARY_PATH=/usr/local/lib mjpg_streamer -i "input_file.so -f /tmp/stream -n pic.jpg" -o "output_http.so -p 8080 -w /var/www" &
What I want is to execute those bash commands when the .cgi is called, do you think what can be my problem? Or any workaround for this issue?

shell script not working

I have a shell script to do the following things
sudo as a user (johnsmith) and perform few things
Exit from that user and check url status
If status is not equal to 1 , ssh to one more server and execute a
script.
But when I am running it, the lines inside 'ENDBASH' are not getting executed at all.
#!/bin/ksh
echo "Outside ENDBASH ${###*/}"
sudo -u johnssmith bash <<'ENDBASH'
echo "Inside ENDBASH ${###*/}"
#Obtaining the new version file
for file in "${###*/}"
do
if echo "$file" | grep -E "abc_cde_efg"; then
echo "Version found: $file"
else
echo "Version not found"
fi
done
exit
ENDBASH
urlArray=('http://server:port/servicename1/services/servicename1?wsdl' 'http://server:port/servicename2/services/servicename2?wsdl')
status=0
for url in "${urlArray[#]}"
do
result=`curl -s $url`
if (echo $result | grep '<?xml' >/dev/null 2>&1); then
service=$(echo $url | cut -d"/" -f4)
echo "$service is Running"
else
service=$(echo $url | cut -d"/" -f4)
echo "$service is not Running"
status=1
fi
done
if [ $status != 1 ] ; then
ssh -t username#hostname /home/dev_was/test1.sh
fi
You need to explicitly pass the arguments received by your script to the internal script:
sudo -u johnssmith bash -s "$#" <<'ENDBASH'

Shell script to append new lines to etc/hosts and Apache httpd-vhosts.conf in Mac OSX 10.6

I am using Mac OSX 10.6 and doing web development on it. I know a small amount about writing shell scripts, but I am not really versed in them as of yet.
What I would like to do is to write a shell script that will simply ask for a local site alias and the document directory and it will then append the new alias onto hosts with something like "127.0.0.1 mysite.local" on a new line at the bottom of etc/hosts.
Then the script would append Apache's httpd-vhosts.conf file with something like this:
<VirtualHost *:80>
DocumentRoot "/Repositories/myproject/mysite.com/trunk/htdocs"
ServerName mysite.local
ServerAlias mysite.localhost
</VirtualHost>
Then it would finally run the command to restart my Apache server. Now I know the terminal command to restart Apache, that is simple enough. I also know how to read in the site name and path from the user running the script. Such as below:
#!/bin/bash
read -p "New local site name: " site
read -p "Site path (ex:/Repositories/myproject/mysite.com/trunk/htdocs): " sitepath
What I don't know how to do is to append text to a file from terminal.
Any thoughts or helpful ideas?
Thanks,
Patrick
Untested, but it should work:
#!/bin/bash
read -p "New local site name: " SITE
read -p "Site path (ex:/Repositories/myproject/mysite.com/trunk/htdocs): " SITEPATH
#/etc/hosts
cp /etc/hosts /etc/hosts.original
echo -e "127.0.0.1\t${SITE}.local" >> /etc/hosts
#httpd-vhosts.conf
VHOSTSFILE="/etc/apache2/httpd-vhosts.conf"
cp $VHOSTSFILE ${VHOSTSFILE}.original
echo "<VirtualHost *:80>" >> $VHOSTSFILE
echo -e "\tDocumentRoot \"${SITEPATH}\"" >> $VHOSTSFILE
echo -e "\tServerName ${SITE}.local" >> $VHOSTSFILE
echo -e "\tServerAlias ${SITE}.localhost" >> $VHOSTSFILE
echo '</VirtualHost>' >> $VHOSTSFILE
#restart apache
>> redirects the output to the given file, appending the contents to the file. I’m also using -e to allow \t to be expanded to a tab character.
Note that you need to run this script with sudo. I've also included commands to backup the original files before modifying them, just in case.
I made some tweaks and did some extra stuff in the above answer, because this didn't work for me but it helped me to come up with another solution. This answer is only for Mac users.
First go in your /etc/apache2/httpd.conf and uncomment virtual host reference, which is this line Include /private/etc/apache2/extra/httpd-vhosts.conf
Now create a bash file I did that in my user’s home named as imran, you need to replace it with your username.
I placed it inside /Users/imran named as create_new_site.sh. I gave it executeable permissions, so it can be easily executed using chmod +x create_new_site.sh
Code for the script is as below:
#!/bin/bash
read -p "New local site name (prefix to .local): " SITE
SITEPATH=$SITE
sudo chmod -R a+w /Users/imran/Sites/web
SITEPATH="/Users/imran/Sites/web/${SITEPATH}"
mkdir -p $SITEPATH
#/etc/hosts
cp /etc/hosts /etc/hosts.original
echo -e "127.0.0.1\t${SITE}.local" >> /etc/hosts
#httpd-vhosts.conf
VHOSTSFILE="/etc/apache2/extra/httpd-vhosts.conf"
cp $VHOSTSFILE ${VHOSTSFILE}.original
echo "<VirtualHost *:80>" >> $VHOSTSFILE
echo -e "\tDocumentRoot \"${SITEPATH}\"" >> $VHOSTSFILE
echo -e "\tServerName ${SITE}.local" >> $VHOSTSFILE
echo '</VirtualHost>' >> $VHOSTSFILE
echo '<?php phpinfo();' > "${SITEPATH}/phpinfo.php"
sudo chmod -R a+w $SITEPATH
#restart apache
sudo apachectl restart
echo "All done! visit, let's visit http://${SITE}.local/phpinfo.php"
Once that is done you can start creating new sites using sudo ./create_new_site.sh. Remember that you need to be in your home directory, which you can go via cd ~ command. Now let's suppose you created a site with name test. You should be able to visit http://test.local/phpinfo.php to see your vhost working.

CGI : 502 Bad Gateway The CGI was not CGI/1.1 compliant.

I have a form in an HTM page that, after pressing the submit button, calls the index.cgi !
#!/usr/bin/sh
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/nastools/pysqlite2/pysqlite2
export PYTHONPATH=$PYTHONPATH:/nastools/pysqlite2/:/path/impo
export PATH=/nastools/python64/bin/:$PATH
python /remote/path/impo/manager.py 2>&1
I just want to run the manager.py that is stored in folder /remote/path/impo !!
I am totally going crazy with this stupid error and cannot find the way to solve it ...
Any suggestion ??
Thanks NineBarry but I found the problem ... After trying with CGI scripts surely good like :
#!/bin/sh
echo Content-type: text/html
echo
echo
echo "<HTML>"
echo "<HEAD>"
echo "</HEAD>"
echo "<BODY>"
echo "<H2>Users logged in are:</H2>"
echo "<PRE>"
who
echo "</PRE>"
echo "</BODY>"
echo "</HTML>"
I've remembered by chance that I haven't set the access rights !!!! I LOST 2 HOURS on it ... I've fixed it with just chmod 777 name.cgi ...
Sorry to all of u if I ve wasted your time !
Bye