Skip to content

Instantly share code, notes, and snippets.

@pepa65
Last active August 17, 2016 03:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pepa65/c3bde3049bce2aea2fae1a482806ff54 to your computer and use it in GitHub Desktop.
Save pepa65/c3bde3049bce2aea2fae1a482806ff54 to your computer and use it in GitHub Desktop.
Accessing the gandi.net API by XML-RPC
#!/bin/bash
### Gandi API access
## Default values for APIKEY
APIKEY=''
Usage(){ ## $1: optional error message
cat <<-EOF
Usage: $0 <method> <parameter> ... [-l] [-v] [-t] [-h] [-a <APIKEY>]
<method> is the Gandi API method, must come before the parameters
<parameter> ... are the Parameters to the method, consisting of:
1. Datatype: struct | array | string | int | boolean
2.struct: {Name Parameter} ... (Name is string, Parameter is string|int|boolean)
2.array: Value ... (can only be string)
2.string|int|boolean: Value
Shorthands: $ instead of stuct, @ for array, %1 and %0 for boolean values;
string and int can be left out
(The apikey not listed under parameters, but given under -a or inline)
-l: Longhand, disallow the use of shorthand
-v: Verbose, debug info to stderr
-t: Test, don't send it to Gandi, just build the query (implies Verbose)
-h: Help on usage (this text)
-a <APIKEY>: Gandi Production API key
All according to http://doc.rpc.gandi.net/domain/reference.html
EOF
[[ $1 ]] && echo -e "\nERROR: $1"
exit 1
}
Debug(){
((verbose)) && echo -e "DEBUG: $1" 1>&2
}
Rpc(){
gandi="rpc.gandi.net:443"
Debug "RPC $@"
tmp_xml="<?xml version=\"1.0\"?><methodCall><methodName>$1</methodName><params><param><value><string>$2</string></value></param>"
shift 2
while (($#))
do
if ((longhand))
then
if [[ $1 = struct ]]
then
tmp_xml+="<param><value><struct>"
shift
while (($#))
do
if ! [[ $1 = struct ]]
then
tmp_xml+="<member><name>$1</name><value><$2>$3</$2></value></member>"
shift 3
else # Second struct, go to the end of the while loop
break
fi
done
tmp_xml+="</struct></value></param>"
elif [[ $1 = array ]]
then
tmp_xml+="<param><value><array><data><value>"
shift
while (($#))
do
if ! [[ $1 = struct ]] && ! [[ $1 = boolean ]]
then
tmp_xml+="<string>$1</string>"
shift
else # End of the array, go to end of the while loop
break
fi
done
tmp_xml+="</value></data></array></value></param>"
else
tmp_xml+="<param><value><$1>$2</$1></value></param>"
shift 2
fi
else ## Shorthand
if [[ $1 = '$' ]]
then
tmp_xml+="<param><value><struct>"
shift
while (($#))
do
if [[ $1 = '$' ]]
then ## Second struct, go to the end of the while loop
break
else
[[ ${2:0:1} = '%' ]] && v=${2:1} && t=boolean || v=$2
[[ $2 =~ ^[1-9][0-9]*$ ]] && t=int || t=string
tmp_xml+="<member><name>$1</name><value><$t>$v</$t></value></member>"
shift 2
fi
done
tmp_xml+="</struct></value></param>"
elif [[ $1 = '@' ]]
then
tmp_xml+="<param><value><array><data><value>"
shift
while (($#))
do
if [[ $1 = '$' ]] || [[ ${1:0:1} = '%' ]]
then ## End of the array, go to end of the while loop
break
else
tmp_xml+="<string>$1</string>"
shift
fi
done
tmp_xml+="</value></data></array></value></param>"
else
t=string
[[ ${1:0:1} = '%' ]] && v=${1:1} && t=boolean || v=$1
[[ $1 =~ ^[1-9][0-9]*$ ]] && t=int
tmp_xml+="<param><value><$t>$v</$t></value></param>"
shift
fi
fi
done
tmp_xml+="</params></methodCall>"
printf -v tmp_post "POST /xmlrpc/ HTTP/1.0\nUser-Agent: Gandi Automatic DNS shell script/0.4\nHost: %s\nContent-Type: text/xml\nContent-Length: %d\n\n" "$gandi" $(echo "$tmp_xml" |wc -c)
tmp_message="$tmp_post$tmp_xml"
Debug "Sending XML-RPC message:\n\n$tmp_message\n"
((test)) || echo "$tmp_message" |openssl s_client -quiet -connect "$gandi" 2>/dev/null
}
## Read commandline options
verbose=0 test=0 longhand=0
while (($#))
do
case "$1" in
-v) verbose=1 ;;
-t) test=1 verbose=1 ;;
-l) longhand=1 ;;
-a) apikey="$2"; shift ;;
-h) Usage ;;
*) [[ $method ]] && params+=" $1" || method=$1
esac
shift
done
[[ $apikey ]] || apikey=$APIKEY
[[ $apikey ]] || Usage "Need Gandi API key"
[[ $method ]] || Exit "Need a method for the RPC call"
Debug "Initial flags:\n method: $method\n apikey: $apikey\n params:$params"
Rpc $method $apikey $params
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment