diff --git lib/func.sh lib/func.sh index f6dd13e..f9cb744 100644 --- lib/func.sh +++ lib/func.sh @@ -3,6 +3,24 @@ # Functions used in >1 tools # +function get_export_domain() { + EXPORT_DOMAIN="" + local response=$(curl -k -s -X GET -H X-Auth-Token:\ $TOKEN $DNSSVR/$USERID/domains/$DOMAINID/export|tr -s '[:cntrl:]' "\n") + local callback_url=$(echo "$response" | perl -ane 'if(/"callbackUrl":\"(.*?)"/){print $1;}') + # async method + for i in $(seq 1 3); do + sleep 5 + local callback_responce=$(curl -k -s -X GET -H X-Auth-Token:\ $TOKEN $callback_url|tr -s '[:cntrl:]' "\n") + local status=$(echo "$callback_responce" | perl -ane 'if(/"status":\"(.*?)"/){print $1;}') + if [ "$status" == "COMPLETED" ];then + EXPORT_DOMAIN=$(curl -k -s -X GET -H X-Auth-Token:\ $TOKEN "${callback_url}?showDetails=true"|tr -s '[:cntrl:]' "\n") + EXPORT_DOMAIN=$(echo "$EXPORT_DOMAIN" | perl -ane 'if(/"contents":"(.*?)","/){$t=$1;$t=~s/\\n/\n/g;$t=~s/\\t/\t/g;$t=~s/\\"/"/g;print $t;}') + break + fi + done +} + + #gets the domains associated with an account. function get_domains() { DOMAINS=`curl -k -s -X GET -H X-Auth-Token:\ $TOKEN $DNSSVR/$USERID/domains|tr -s '[:cntrl:]' "\n" |sed -e 's/{"domains":\[{//' -e 's/}\]}//' -e 's/},{/;/g' -e 's/"name"://g' -e 's/"id"://g' -e 's/"accountId"://g' -e 's/"updated"://g' -e 's/"created"://g' -e 's/"totalEntries"://g'` diff --git rsdns-export.sh rsdns-export.sh new file mode 100644 index 0000000..a1d3478 --- /dev/null +++ rsdns-export.sh @@ -0,0 +1,105 @@ +#!/bin/bash +# +# rsdns-export.sh - Used to export domain on rackspace cloud dns +# + +# config file for variables. +if [ -e ~/.rsdns_config ] +then + . ~/.rsdns_config +fi + +# load up our auth & funct library +if [ -n "$RSPATH" ] +then + . $RSPATH/lib/auth.sh + . $RSPATH/lib/func.sh +else + . lib/auth.sh + . lib/func.sh +fi + +#prints out the usage information on error or request. +function usage () { + printf "\n" + printf "rsdns export -u username -a apiKey -d domain\n" + printf "\t-k Use London/UK Servers.\n" + printf "\t-h Show this.\n" + printf "\n" +} + +#prints out the domains associated with an account. +function print_domains () { + + get_domains + + echo $DOMAINS | ( + echo "ID|Domain" + awk -F, 'BEGIN { RS = ";" } { gsub(/\"/,"") ; print $2 "|" $1 }' | + sort -t '|' -k 2 + ) | column -t -s '|' + +} + +#prints out the records for a given domain. +function print_export() { + + check_domain + + if [ $FOUND -eq 1 ] + then + + get_export_domain + + echo -e "$EXPORT_DOMAIN" + fi +} + +#prints words for master rsdns script output +function words () { + printf "Export domain by rackspace \n" +} + +#Get options from the command line. +while getopts "u:a:c:d::hkqw" option +do + case $option in + u ) RSUSER=$OPTARG ;; + a ) RSAPIKEY=$OPTARG ;; + c ) USERID=$OPTARG ;; + d ) DOMAIN=$OPTARG ;; + h ) usage;exit 0 ;; + q ) QUIET=1 ;; + k ) UKAUTH=1 ;; + w ) words;exit 0 ;; + esac +done + +#All actions require authentication, get it done first. +#If the authentication works this will return $TOKEN and $MGMTSVR for use by everything else. +get_auth $RSUSER $RSAPIKEY +if test -z $TOKEN + then + if [[ $QUIET -eq 0 ]]; then + echo Auth Token does not exist. + fi + exit 98 +fi +if test -z $MGMTSVR + then + if [[ $QUIET -eq 0 ]]; then + echo Management Server does not exist. + fi + exit 98 +fi + +#if a domain is given, print records, else print domaints +if [ -z "$DOMAIN" ] + then + print_domains +else + print_export +fi + +#done +exit 0 \ No newline at end of file