Re: Untitled

From Ivory Water Vole, 5 Years ago, written in Bash, viewed 766 times. This paste is a reply to from - view diff
URL https://paste.simplylinux.ch/view/371d9c07 Embed
Download Paste or View Raw
  1. #!/usr/bin/env sh
  2.  
  3. set -o posix
  4.  
  5. fulldomain=$1
  6. txtvalue=$2
  7.  
  8. #Here is a sample custom api script.
  9. #This file name is "dns_myapi.sh"
  10. #So, here must be a method   dns_myapi_add()
  11. #Which will be called by acme.sh to add the txt record to your api system.
  12. #returns 0 means success, otherwise error.
  13.  
  14. #ISPConfig 3.1 API
  15.  
  16. ISPC_User="remoteTest"
  17. ISPC_Password="xxx"
  18. ISPC_Api="https://10.0.0.8:8080/remote/json.php"
  19.  
  20.  
  21. ########  Public functions #####################
  22.  
  23. #Usage: dns_myapi_add   _acme-challenge.www.domain.com   "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  24. dns_myapi_add() {
  25.   fulldomain=$1
  26.   txtvalue=$2
  27.   _err "Not implemented!"
  28.   return 1
  29. }
  30.  
  31. #fulldomain
  32. dns_myapi_rm() {
  33.   fulldomain=$1
  34.  
  35. }
  36.  
  37. ####################  Private functions bellow ##################################
  38.  
  39. _login() {
  40.     _info "Getting Session ID"
  41.     curData="{\"username\":\"${ISPC_User}\",\"password\":\"${ISPC_Password}\",\"client_login\":false}"
  42.     curResult=$(curl -k --data "${curData}" "${ISPC_Api}?login")
  43.     if _contains "${curResult}" '"code":"ok"'; then
  44.         sessionID=$(echo $curResult | _egrep_o "response.*" | cut -d ':' -f 2)
  45.         sessionID=${sessionID:1:-2}
  46.         _info "Successfully retrieved Session ID."
  47.     else
  48.         _err "Couldn't retrieve the Session ID. Aborting.";
  49.     fi
  50. }
  51.  
  52. _getZoneInfo () {
  53.     _info "Getting Zoneinfo"
  54.     zoneEnd=false
  55.     curZone="${fulldomain}"
  56.     while [ ${zoneEnd} = false ]; do
  57.         curZone="${curZone#*.}"         # we can strip the first part of the fulldomain, since it's just the _acme-challenge string
  58.         curData="{\"session_id\":\"${sessionID}\",\"primary_id\":[{\"origin\":\"${curZone}.\"}]}"         # suffix . needed for zone -> domain.tld.
  59.         curResult=$(curl -k --data "${curData}" "${ISPC_Api}?dns_zone_get")
  60.         if _contains "${curResult}" '"id":"'; then
  61.             zoneFound=true
  62.             zoneEnd=true
  63.             _info "Successfully retrieved zone data."
  64.         fi
  65.         if test "${curZone#*.}" != "$curZone"; then
  66.             _debug2 "$curZone still contains a '.' - so we can check next higher level"
  67.          else
  68.             zoneEnd=true
  69.             _err "Couldn't retrieve zone info. Aborting."
  70.          fi
  71. #        if _contains "${curZone}" '.'; then  # For some reason this check fails... hence the line above using test for checking
  72.             # we can still go on with checking
  73. #            echo "";
  74. #        else
  75. #            _err "Couldn't retrieve zone info"
  76. #        fi
  77.     done
  78.     if [ ${zoneFound} = true ]; then
  79.         server_id=$(echo $curResult | _egrep_o "server_id.*" | cut -d ':' -f 2)
  80.         server_id=${server_id:1:-10}
  81.         case ${server_id} in
  82.             ''|*[!0-9]*) _err "Server ID is not numeric. Aborting" ;;
  83.             *) echo _info "Successfully retrieved Server ID" ;;
  84.         esac
  85.         zone=$(echo $curResult | _egrep_o "\"id.*" | cut -d ':' -f 2)
  86.         zone=${zone:1:-14}
  87.         case ${zone} in
  88.                ''|*[!0-9]*) _err "Zone ID is not numeric. Aborting" ;;
  89.                 *) _info "Successfully retrieved Zone ID" ;;
  90.         esac
  91.         client_id=$(echo $curResult | _egrep_o "sys_userid.*" | cut -d ':' -f 2)
  92.         client_id=${client_id:1:-15}
  93.         case ${client_id} in
  94.                ''|*[!0-9]*) _err "Client ID is not numeric. Aborting" ;;
  95.                 *) _info "Successfully retrieved Client ID" ;;
  96.         esac
  97.         unset zoneFound
  98.         unset zoneEnd
  99.     fi
  100. }
  101.  
  102. _addTxt () {
  103.     params="\"server_id\":\"${server_id}\",\"zone\":\"${zone}\",\"name\":\"${fulldomain}\",\"type\":\"txt\",\"data\":\"${txtvalue}\",\"aux\":\"0\",\"ttl\":\"3600\",\"active\":\"y\",\"stamp\":\"1479327993\",\"serial\":\"1\""
  104.     curData="{\"session_id\":\"${sessionID}\",\"client_id\":\"${client_id}\",\"params\":[{${params}}]}"         # suffix . needed for zone -> domain.tld.
  105.     echo $curData
  106.     curResult=$(curl -k --data "${curData}" "${ISPC_Api}?dns_txt_add")
  107.     echo $curResult;
  108. }
  109.  
  110. echo '
  111. {
  112.    "session_id":"xxxx",
  113.    "client_id":"2",
  114.    "params":[{
  115.        "server_id":"1",
  116.        "zone":"1",
  117.        "name":"acme_test.domain.tld",
  118.        "type":"txt",
  119.        "data":"acme_test_data",
  120.        "aux":"0",
  121.        "ttl":"3600",
  122.        "active":"y",
  123.        "stamp":"1479327993",
  124.        "serial":"1"
  125.    }]
  126. }';
  127.  
  128. # server_id
  129. # zone
  130. name="${fulldomain}"
  131. type="txt"
  132. data="${txtvalue}"
  133. aux=0
  134. ttl=3600
  135. active="y"
  136. #--stamp
  137. #--serial
  138.  
  139.  
  140.  
  141. _contains() {
  142.   _str="$1"
  143.   _sub="$2"
  144.   echo "$_str" | grep -- "$_sub" >/dev/null 2>&1
  145. }
  146.  
  147. _hasfield() {
  148.   _str="$1"
  149.   _field="$2"
  150.   _sep="$3"
  151.   if [ -z "$_field" ]; then
  152.     _usage "Usage: str field  [sep]"
  153.     return 1
  154.   fi
  155.  
  156.   if [ -z "$_sep" ]; then
  157.     _sep=","
  158.   fi
  159.  
  160.   for f in $(echo "$_str" | tr ',' ' '); do
  161.     if [ "$f" = "$_field" ]; then
  162.       _debug2 "'$_str' contains '$_field'"
  163.       return 0 #contains ok
  164.     fi
  165.   done
  166.   _debug2 "'$_str' does not contain '$_field'"
  167.   return 1 #not contains
  168. }
  169.  
  170. _getfield() {
  171.   _str="$1"
  172.   _findex="$2"
  173.   _sep="$3"
  174.  
  175.   if [ -z "$_findex" ]; then
  176.     _usage "Usage: str field  [sep]"
  177.     return 1
  178.   fi
  179.  
  180.   if [ -z "$_sep" ]; then
  181.     _sep=","
  182.   fi
  183.  
  184.   _ffi="$_findex"
  185.   while [ "$_ffi" -gt "0" ]; do
  186.     _fv="$(echo "$_str" | cut -d "$_sep" -f "$_ffi")"
  187.     if [ "$_fv" ]; then
  188.       printf -- "%s" "$_fv"
  189.       return 0
  190.     fi
  191.     _ffi="$(_math "$_ffi" - 1)"
  192.   done
  193.  
  194.   printf -- "%s" "$_str"
  195.  
  196. }
  197.  
  198. _normalizeJson() {
  199.   sed "s/\" *: *\([\"{\[]\)/\":\1/g" | sed "s/^ *\([^ ]\)/\1/" | tr -d "\r\n"
  200. }
  201.  
  202. _egrep_o() {
  203.   if _contains "$(egrep -o 2>&1)" "egrep: illegal option -- o"; then
  204.     sed -n 's/.*\('"$1"'\).*/\1/p'
  205.   else
  206.     egrep -o "$1"
  207.   fi
  208. }
  209.  
  210.  
  211.  
  212.  
  213. _info() {
  214.     if [ -z "$2" ]; then
  215.         echo "[$(date)] $1"
  216.     else
  217.         echo "[$(date)] $1='$2'"
  218.     fi
  219. }
  220.  
  221. _err() {
  222.     _info "$@" >&2
  223.     return 1
  224. }
  225.  
  226. _debug() {
  227.     if [ -z "$DEBUG" ]; then
  228.         return
  229.     fi
  230.     _err "$@"
  231.     return 0
  232. }
  233.  
  234. _debug2() {
  235.     if [ "$DEBUG" ] && [ "$DEBUG" -ge "2" ]; then
  236.         _debug "$@"
  237.     fi
  238.     return
  239. }
  240.  
  241. _login
  242. _getZoneInfo
  243. _addTxt

Reply to "Re: Untitled"

Here you can reply to the paste above