Skip to content

antony@notes:~/ops-automation$ cat "cgidb.sh-program.md"

cgidb.sh program

2023-07-01· ops-automation ·Shell Script

cgidb.sh program

2023/07/01

#!/bin/bash

echo "Content-type: text/html"
echo ""
echo '<html>'
echo ' <head><meta charset="utf-8" /> </head>'
echo '<body>'

# MySQL Server Connection test
[ ! -f /opt/www/cgi/dagger.ini ] && echo "<h1><center>pls setup first !  <a href="/setup">setup</a></p></center></h1>" && exit 0
source "/opt/www/cgi/dagger.ini"
if ! mysql --connect-timeout=1 -u "$DB_user" -h "$DB_ip" -p"$DB_passwd" -e "show databases;" &>/dev/null; then
  echo '<h1><center>MySQL Cluster connection <font color="red">failed</font> !
  <p>pls Setup again, thx!
  <a href="/setup">setup</a></p></center></h1>' && exit 0
fi

# var
HTML=$(echo ${QUERY_STRING%=*} | cut -d "=" -f 1)
Action=$(echo $QUERY_STRING | cut -d '=' -f3 | cut -d '+' -f1)
ALL_COMMAND=$(echo $REQUEST_URI | cut -d '=' -f3 | sed 's/+/ /g' | sed 's/%28/(/g' | sed 's/%29/)/g' | sed 's/%2C/,/g' | sed 's/%3B/;/g' | sed "s/%27/'/g" | sed 's/%22/"/g' | sed 's/%3D/=/g' | sed 's/%0D%0A/ /g')
ALL_COMMAND_tbl=${ALL_COMMAND%%&*}
Action_tbl=${QUERY_STRING##*=}
tbl=$(mysql -u "$DB_user" -h "$DB_ip" -p"$DB_passwd" -e "use $DB_name;$ALL_COMMAND_tbl")
tbl_name=$(echo ${QUERY_STRING%&*} | cut -d "=" -f 3)


# Debug
#echo "<br>REQUEST_URI=${REQUEST_URI}</br>"
#echo "<br>QUERY_STRING=${QUERY_STRING}</br>"
#echo "<br>DBname=$DB_name</br>"
#echo "<br>HTML=$HTML</br>"
#echo "<br>Action=$Action</br>"
#echo "<br>Action_tbl=$Action_tbl</br>"
#echo "<br>ALL_COMMAND=${ALL_COMMAND}</br>"
#echo "<br>ALL_COMMAND_tbl=${ALL_COMMAND_tbl}</br>"
#echo "<br>tbl_name=$tbl_name</br>"

# function
DB_list() {
  set -f # disable globbing
  saveIFS=$IFS
  IFS=$'\n' # set field separator to NL (only)
  ALL_DB=$(mysql -u "$DB_user" -h "$DB_ip" -p"$DB_passwd" -e "show databases;")
  cat /dev/null >/opt/www/cgi/db.txt
  for i in ${ALL_DB[@]}; do
    if [ "$i" != "Database" ]; then
      echo "        <option>$i</option>" >>/opt/www/cgi/db.txt
    else
      echo "        <option>請選擇資料庫名稱</option>" >>/opt/www/cgi/db.txt
    fi
  done
  IFS=$saveIFS
  set +f
  sed -i "/<option>/d" /opt/www/html/tbl.html
  line=$(cat /opt/www/html/tbl.html | grep -n 'select type' | cut -d ":" -f 1)
  sed -i "${line} r /opt/www/cgi/db.txt" /opt/www/html/tbl.html
}

tbl_action() {
  mysql --show-warnings -u "$DB_user" -h "$DB_ip" -p"$DB_passwd" -e "use $DB_name;$ALL_COMMAND_tbl" 1> /tmp/out.txt 2> /tmp/err-out.txt
  [[ "$?" == "0" ]] && echo -e "<h1 style="color:blue">\nCommand success</h1>"
  [[ -s /tmp/err-out.txt ]] && show_err
}

show() {
  echo "<pre>"
  echo "<font size="6">"
  cat /tmp/out.txt
  echo "</pre>"
  echo "</font>"
}

show_err() {
  echo "<pre>"
  echo "<font size="6">"
  cat /tmp/err-out.txt
  echo "</pre>"
  echo "</font>"
}


show_db() {
  echo "<pre>"
  echo "<font size="6">"
  cat /tmp/dbout.txt
  echo "</pre>"
  echo "</font>"
}

DB_list
if [[ "$HTML" == "tblname" ]]; then
  DB_name=$(echo ${QUERY_STRING%%&*} | cut -d "=" -f 2)
  case ${Action_tbl} in
  DROP)
    if ! echo "$ALL_COMMAND_tbl" | grep 'drop'; then
      if mysql -u "$DB_user" -h "$DB_ip" -p"$DB_passwd" -e "use ${DB_name}; DROP TABLE ${tbl_name}"; then
        echo -e "<h1 style="color:blue">\nCommand success</h1>"
      else
        echo -e "<h1 style="color:red">\nCommand fail</h1>"
      fi
    else
      tbl_action
      show
    fi
    ;;
  *)
    tbl_action
    show
    ;;
  esac
  echo '<h1><center><a href="../html/tbl.html">返回上一頁</a></center></h1>'
else
  if [[ $Action == "SHOW" ]]; then
    mysql -u "$DB_user" -h "$DB_ip" -p"$DB_passwd" -e "SHOW DATABASES" >/tmp/dbout.txt && show_db
  else
    DB_name=$(echo ${QUERY_STRING%%&*} | cut -d "=" -f 2)
    mysql --show-warnings -u "$DB_user" -h "$DB_ip" -p"$DB_passwd" -e "${Action} DATABASE ${DB_name}" >/tmp/dbout.txt
    if ! cat /tmp/dbout.txt | grep 'Error'; then
      mysql -u "$DB_user" -h "$DB_ip" -p"$DB_passwd" -e "SHOW DATABASES" >/tmp/dbout.txt && show_db && DB_list
    else
      show_db && DB_list
    fi
  fi
fi

echo '</body></html>'
exit 0

2023/1/10 最新版

#!/bin/bash
echo "Content-type: text/html"
echo ""
echo '<html>'
echo ' <head><meta charset="utf-8" /> </head>'
echo '<body>'
echo '<h1>call from mydb.sh</h1>'

# var
DB_user="dagger"
DB_passwd="dagger"
DB_ip="10.98.0.203"
DB_name=$(echo ${QUERY_STRING%%&*} | cut -d "=" -f 2)
HTML=$(echo ${QUERY_STRING%=*} | cut -d "=" -f 1)
Action=$(echo $QUERY_STRING | cut -d '=' -f3 | cut -d '+' -f1)
ALL_COMMAND=$(echo $REQUEST_URI | cut -d '=' -f3 | sed 's/+/ /g' | sed 's/%28/(/g' | sed 's/%29/)/g' | sed 's/%2C/,/g' | sed 's/%3B/;/g' | sed "s/%27/'/g" | sed 's/%22/"/g' | sed 's/%3D/=/g' | sed 's/%0D%0A/ /g')
ALL_COMMAND_tbl=${ALL_COMMAND%%&*}
Action_tbl=${QUERY_STRING##*=}
tbl=$(mysql -u "$DB_user" -h "$DB_ip" -p"$DB_passwd" -e "use $DB_name;$ALL_COMMAND_tbl")
tbl_name=$(echo ${QUERY_STRING%&*} | cut -d "=" -f 3)

# Debug
echo "<br>REQUEST_URI=${REQUEST_URI}</br>"
echo "<br>QUERY_STRING=${QUERY_STRING}</br>"
echo "<br>DBname=$DB_name</br>"
echo "<br>HTML=$HTML</br>"
echo "<br>Action=$Action</br>"
echo "<br>Action_tbl=$Action_tbl</br>"
echo "<br>ALL_COMMAND=${ALL_COMMAND}</br>"
echo "<br>ALL_COMMAND_tbl=${ALL_COMMAND_tbl}</br>"
echo "<br>tbl_name=$tbl_name</br>"

# function
DB_list() {
  set -f        # disable globbing
  saveIFS=$IFS
  IFS=$'\n'     # set field separator to NL (only)
  ALL_DB=$(mysql -u "$DB_user" -h "$DB_ip" -p"$DB_passwd" -e "show databases;")
  cat /dev/null > /opt/www/cgi/db.txt
  for i in ${ALL_DB[@]}
  do
    if [ "$i" != "Database" ]; then
      echo "        <option>$i</option>" >> /opt/www/cgi/db.txt
    else
      echo "        <option><b>請選擇資料庫名稱</b></option>" >> /opt/www/cgi/db.txt
    fi
  done
  IFS=$saveIFS
  set +f
  sed -i "/<option>/d" /opt/www/html/tbl.html
  line=$(cat /opt/www/html/tbl.html | grep -n 'select type' | cut -d ":" -f 1)
  sed -i "${line} r /opt/www/cgi/db.txt" /opt/www/html/tbl.html
}

display() {
  echo "<h1>${tbl}</h1>"
  if [ "$?" == "0" ]; then
    echo -e "<h1 style="color:blue">\nCommand success</h1>"
  else
    echo -e "<h1 style="color:red">\nCommand fail</h1>"
  fi
}

if [[ $HTML == tblname ]]; then
  case ${Action_tbl} in
    CREATE)
      display
    ;;
    DROP)
      if ! echo "$ALL_COMMAND_tbl" | grep 'drop'; then
        if mysql -u "$DB_user" -h "$DB_ip" -p"$DB_passwd" -e "use ${DB_name}; DROP TABLE ${tbl_name}"; then
          echo -e "<h1 style="color:blue">\nCommand success</h1>"
        else
          echo -e "<h1 style="color:red">\nCommand fail</h1>"
        fi
      else
        display
      fi
    ;;
    *)
      display
    ;;
  esac
else
  if [[ $Action == SHOW ]]; then
    if ! mysql -u "$DB_user" -h "$DB_ip" -p"$DB_passwd" -e "SHOW DATABASES"; then
      echo -e "<h1 style="color:red">\nCommand fail</h1>"
    fi
  else
    if mysql -u "$DB_user" -h "$DB_ip" -p"$DB_passwd" -e "${Action} DATABASE ${DB_name}"; then
      DB_list && echo -e "<h1 style="color:blue">\nCommand success</h1>"
    else
      DB_list && echo -e "<h1 style="color:red">\nCommand fail</h1>"
    fi
  fi
fi
echo '</body></html>'
exit 0

舊版

#!/bin/bash
echo "Content-type: text/html; charset=utf-8"
echo ""
echo '<html><body>'
echo 'Hello From cgidb.sh'
echo '<pre>'

# var
DB_name=$(echo ${QUERY_STRING%&*} | cut -d "=" -f 2)
action=${QUERY_STRING##*&}
Action=${action%%=*}
DB_user="bobo"
DB_ip="120.96.143.84"
DB_passwd="bobo123"

# Debug
#echo "REQUEST_URI=${REQUEST_URI}"
#echo "QUERY_STRING=${QUERY_STRING}"
#echo "DBname=$DB_name"
#echo "Action=$Action"

Action_Db() {
  mysql -u "$DB_user" -h "$DB_ip" -p"$DB_passwd" <<EOF
${Action,,} DATABASE ${DB_name};
EOF
}

DB_list() {
  set -f        # disable globbing
  saveIFS=$IFS
  IFS=$'\n'     # set field separator to NL (only)
  ALL_DB=$(mysql -u "$DB_user" -h "$DB_ip" -p"$DB_passwd" -e "show databases;")
  cat /dev/null > /www/cgi/db.txt
  for i in ${ALL_DB[@]}
  do
    if [ "$i" != "Database" ]; then
      echo "        <option>$i</option>" >> /www/cgi/db.txt
    else
      echo "        <option><b>請選擇資料庫名稱</b></option>" >> /www/cgi/db.txt
    fi
  done
  IFS=$saveIFS
  set +f
  sed -i "/<option>/d" /www/html/tbl.html
  line=$(cat /www/html/tbl.html | grep -n 'select type' | cut -d ":" -f 1)
  sed -i "${line} r /www/cgi/db.txt" /www/html/tbl.html
}

case ${Action,,} in
  create)
    if Action_Db; then
      DB_list && echo "<h1 style="color:red">Create ${DB_name} Database Success!</h1>"
    else
      echo "<h1 style="color: red">Error! pls try again!</h1>"
    fi
    ;;
  drop)
    if Action_Db; then
      DB_list && echo "<h1 style="color:red">Success!</h1>"
    else
      echo "<h1 style="color:red">Error! pls try again!</h1>"
    fi
    ;;
  show)
    if [ "$DB_name" != "" ]; then
      echo "<h1> 資料庫查詢結果:</h1>"
      mysql -u "$DB_user" -h "$DB_ip" -p"$DB_passwd" -e "show database like ${DB_name};"
    else
      mysql -u "$DB_user" -h "$DB_ip" -p"$DB_passwd" -e "show databases;"
    fi
    ;;
  *)
    echo -e "Error ! \nYour SQL Command now is > ${Action} DATABASE ${DB_name};"
    ;;
esac

echo '</pre>'
echo '</body></html>'
exit 0

  • bebo 不可執行 curl (命令攔截), bobo 可以 curl ,openssh container
  • mysql rbean 使用者

mydb

#!/bin/bash
echo "Content-type: text/html; charset=utf-8"
echo ""
echo '<html><body>'
echo 'Hello From MyDb'

# Debug
echo "REQUEST_URI=${REQUEST_URI}"
echo "QUERY_STRING=${QUERY_STRING}"

# QUERY_STRING="DB_user_account&DB_user_password&DB_Server_IP"
# e.g., curl 'http://120.96.143.110:80/login?rbean&rbean&120.96.143.111'
login_info() {
  # var
  DB_USER=$(echo "${QUERY_STRING%%&*}")
  DB_PASSWD=$(echo "${QUERY_STRING%&*}" | cut -d "&" -f 2)
  DB_IP=$(echo "${QUERY_STRING##*&}")

  if mysql -u ${DB_USER} -h ${DB_IP} -p${DB_PASSWD}; then
    echo "${DB_USER},${DB_PASSWD},${DB_IP}" > /tmp/hello
    [ "$?" == "0" ] && echo -e "\n${DB_USER} login success !" || echo "login failed"
  fi
}

create_Db() {
  mysql -u ${DB_USER} -h ${DB_IP} -p${DB_PASSWD} <<EOF
CREATE DATABASE ${db_name};
EOF
}

list_Db() {
  mysql -u ${DB_USER} -h ${DB_IP} -p${DB_PASSWD} <<EOF
SHOW DATABASES;
EOF
}

rm_Db() {
  mysql -u ${DB_USER} -h ${DB_IP} -p${DB_PASSWD} <<EOF
DROP DATABASE ${db_name};
EOF
}

# var
DB_USER=$(cat /tmp/hello | cut -d "," -f 1)
DB_PASSWD=$(cat /tmp/hello | cut -d "," -f 2)
DB_IP=$(cat /tmp/hello | cut -d "," -f 3)

# Login check
if [ "$DB_USER" == "" ] || [ "${DB_PASSWD}" == "" ] || [ "${DB_PASSWD}" == "" ]; then
  echo "MySQL Login Error,pls login !" && exit 1
fi

# Login, Create, list, rm Database
a=$(echo ${REQUEST_URI%?$QUERY_STRING}
db_name=${QUERY_STRING}

case ${a,,} in
/login)
  login_info
  ;;
/db/create)
  create_Db
  ;;
/db/list)
  list_Db
  ;;
/db/rm)
  rm_Db
  ;;
esac

echo '</body></html>'
exit 0

new gocgi

package main

import (
  "net/http"
  "net/http/cgi"
)

func cgiDB(w http.ResponseWriter, r *http.Request) {
handler := cgi.Handler{Path: "./www/cgi/cgidb.sh"}
handler.ServeHTTP(w, r)
}

func cgiTBL(w http.ResponseWriter, r *http.Request) {
handler := cgi.Handler{Path: "./www/cgi/cgitbl.sh"}
handler.ServeHTTP(w, r)
}

func main() {
  static := http.FileServer(http.Dir("./www/"))
  http.Handle("/",static)

  http.HandleFunc("/db", cgiDB)
  http.HandleFunc("/tbl", cgiTBL)

  http.ListenAndServe(":8080", nil)
}
  • index : bobo Database application

http://bl.ocks.org/ndarville/7075823