Skip to content
Advertisement

CGI web application, struggle with variables

this is my first post here. Been reading stackoverflow for a long time and i want to take a time to appreciate everything you do to help community. Thank you.

A lack of knowledge in scripting area leads me to creating this post. Due to complication of my problem (in my opinion) i wasnt able to google the solution. First of all – i would really appreciate any related material links to be able to leard by myself. If there is any modern solution for what im trying to do – i’d rather switch from bashcgiphp to it since i dont know much.

I have an apache 2.4 and what im doing is trying to create web application. It now has bash syntacsis but i will convert it to CGI later on when figure out how to build working bash script. It does:

#!/bin/bash

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

sv00dbtest01=$(5d0b3f1b-16f4-4d6a-8fac-0e746887f71d)
sv00dbtest02=$(010eeaea-f56b-42b3-b4e4-26ef235860a9)
sv00dbtest03=$(0688e718-c807-4629-ab90-1cc729fd1513)
sv00dbtest04=$(4e9bf96b-3311-4936-a877-4e66ca3a860a)
sv00dbtest05=$(fce66f7a-2a50-4856-978c-cbe44dd2acb6)
sv00dbtest06=$(271476de-97d0-4915-895c-98da9be2d6d2)
sv00dbtest07=$(9c5a7112-1685-4a0e-9d07-30669383ec12)
sv00dbtest08=$(f7a7f06a-adcb-4eda-8f68-3a2d7874ff86)
sv00dbtest09=$(5d1f2088-5148-4ec7-b169-8e674dd6ba40)
sv00dbtest10=$(fc4ad441-a997-4a28-888e-97e53e965447)
file=$(ssh -p 48563 root@$srv1 'ls -1t /app/docker/pg/backup/daily | head -n 1')

echo '<html>'
echo '<head>'
echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">'
echo '<title>Form Example</title>'
echo '</head>'
echo '<body>'

  echo "<form method=GET action="${SCRIPT}">"
       '<table nowrap>'
          '<tr><td>Input</TD><TD><input type="text" name="srv1" size=12></td></tr>'
          '<tr><td>Section</td><td><input type="text" name="srv_list" size=12 value=""></td>'
          '</tr></table>'

  echo '<input type="radio" name="srv2" value="sv00dbtest01" checked> Test server №1<br>'
       '<input type="radio" name="srv2" value="sv00dbtest02"> Test server №2<br>'
       '<input type="radio" name="srv2" value="sv00dbtest03"> Test server №3'
       '<input type="radio" name="srv2" value="sv00dbtest04"> Test server №4'
       '<input type="radio" name="srv2" value="sv00dbtest05"> Test server №5'
       '<input type="radio" name="srv2" value="sv00dbtest06"> Test server №6'
       '<input type="radio" name="srv2" value="sv00dbtest07"> Test server №7'
       '<input type="radio" name="srv2" value="sv00dbtest08"> Test server №8'
       '<input type="radio" name="srv2" value="sv00dbtest09"> Test server №9'
       '<input type="radio" name="srv2" value="sv00dbtest10"> Test server №10'

  echo '<br><input type="submit" value="Process Form">'
       '<input type="reset" value="Reset"></form>'

    scp -3 -P 48563 root@$srv1:/app/docker/pg/backup/daily/$file root@$srv2:/app/docker/pg/backup/$file
    ssh -p 48563 root@$srv2 "systemctl stop docker-compose"
    ssh -p 48563 root@$srv2 "rm -Rfv /app/docker/pg/data"
    ssh -p 48563 root@$srv2 "tar -zsxvf /app/docker/pg/backup/$file --strip=3 -C /app/docker/pg/"
    ssh -p 48563 root@$srv2 "chown -R 1000:1000 /app/docker/pg/data"
    ssh -p 48563 root@$srv2 "systemctl start docker-compose"
    ssh -p 48563 root@$srv2 "rm -Rfv /app/docker/pg/backup/$file"
    ssh -p 48563 root@sv00apptest01 "docker exec server1c /opt/1C/v8.3/x86_64/rac infobase update --cluster=7f122b3b-799c-4c98-a98f-4953a78eff48 --infobase=$srv_list --infobase-user=123 --infobase-pwd=123 --db-server=$srv1"

  if [ "$REQUEST_METHOD" != "GET" ]; then
        echo "<hr>Script Error:"
             "<br>Usage error, cannot complete request, REQUEST_METHOD!=GET."
             "<br>Check your FORM declaration and be sure to use METHOD="GET".<hr>"
        exit 1
  fi

  # If no search arguments, exit gracefully now.

  if [ -z "$QUERY_STRING" ]; then
        exit 0
  else
     # No looping this time, just extract the data you are looking for with sed:
     srv1=`echo "$QUERY_STRING" | sed -n 's/^.*srv1=([^&]*).*$/1/p' | sed "s/%20/ /g"`
     srv2=`echo "$QUERY_STRING" | sed -n 's/^.*srv2=([^&]*).*$/1/p' | sed "s/%20/ /g"`
     srv_list=`echo "$QUERY_STRING" | sed -n 's/^.*srv_list=([^&]*).*$/1/p' | sed "s/%20/ /g"`
     echo "Restored base: " $XX
     echo '<br>'
     echo "On the server: " $YY

  fi
echo '</body>'
echo '</html>'

exit 0

So the problem is that i cant figure out how to use a pool of created variables and assign them to another variable, $srv_list. So that when users enter text into a web form, the script reacts only to values from variables. And another problem is to create web form in which for variables $srv1 and $srv2 entered text goes to script and executes it. And the time when people enter text from web-form application, variable will evalute to script and execute it. Im reading this post atm http://www.yolinux.com/TUTORIALS/BashShellCgi.html And trying to create something similar. Thanks in advance.

Advertisement

Answer

Instead of creating each variable once create an array:

srv_list=(
    "5d0b3f1b-16f4-4d6a-8fac-0e746887f71d"
    "010eeaea-f56b-42b3-b4e4-26ef235860a9"
    "0688e718-c807-4629-ab90-1cc729fd1513"
    "4e9bf96b-3311-4936-a877-4e66ca3a860a"
    "fce66f7a-2a50-4856-978c-cbe44dd2acb6"
    "271476de-97d0-4915-895c-98da9be2d6d2"
    "9c5a7112-1685-4a0e-9d07-30669383ec12"
    "f7a7f06a-adcb-4eda-8f68-3a2d7874ff86"
    "5d1f2088-5148-4ec7-b169-8e674dd6ba40"
    "fc4ad441-a997-4a28-888e-97e53e965447"
)

And to process this list you can loop over the array with for:

for i in ${srv_list[@]}; do
        ssh -p 48563 root@sv00apptest01 "docker exec server1c /opt/1C/v8.3/x86_64/rac infobase update --cluster=7f122b3b-799c-4c98-a98f-4953a78eff48 --infobase=${i} --infobase-user=123 --infobase-pwd=123 --db-server=$srv1"
done
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement