GH-25 Getting the Domain-Record-ID via XML-RPC API

Signed-off-by: Eduard Veit <developer@ev21.de>
This commit is contained in:
Eduard Veit
2020-07-03 21:28:27 +02:00
parent cb382a25bf
commit ef034e519c
3 changed files with 169 additions and 32 deletions

View File

@@ -28,6 +28,7 @@
command -v curl &> /dev/null || { echo >&2 "I require curl but it's not installed. Note: all needed items are listed in the README.md file."; exit 1; }
command -v awk &> /dev/null || { echo >&2 "I require awk but it's not installed. Note: all needed items are listed in the README.md file."; exit 1; }
command -v drill &> /dev/null || command -v nslookup &> /dev/null || { echo >&2 "I need drill or nslookup installed. Note: all needed items are listed in the README.md file."; exit 1; }
command -v xmllint &> /dev/null || { echo >&2 "I recommend xmllint but it's not installed. Note: all needed items are listed in the README.md file."; NO_XMLLINT="true";}
LOG=$0.log
SILENT=NO
@@ -37,15 +38,30 @@ if ls $(dirname $0)/nsupdate.d/*.config &> /dev/null; then
# Loop through configs
for f in $(dirname $0)/nsupdate.d/*.config
do
# Config files could be much cleaner by containing only relevant settings.
# If your User and Password is always the same just set it here once and delete it in the config files.
#INWX_USER="Username"
#INWX_PASS="Password"
# Resets previous set variables to catch wrong or not configured settings and set defaults.
MAIN_DOMAIN=
DOMAIN=
INWX_DOMAIN_ID="unset"
TTL=300
TYPE=A
CONNECTION_TYPE=4
# For backward compatability the following options remain in this script
IPV6="NO"
MX="NO"
source $f
if [[ "$SILENT" == "NO" ]]; then
echo "Starting nameserver update with config file $f ($LOG)"
fi
## Set record type to IPv4
TYPE=A
CONNECTION_TYPE=4
if [[ "$TYPE" == "A" ]]; then
CONNECTION_TYPE=4
fi
## Set record type to MX
if [[ "$MX" == "YES" ]]; then
@@ -55,6 +71,9 @@ if ls $(dirname $0)/nsupdate.d/*.config &> /dev/null; then
## Set record type to IPv6
if [[ "$IPV6" == "YES" ]]; then
TYPE=AAAA
fi
if [[ "$TYPE" == "AAAA" ]]; then
CONNECTION_TYPE=6
fi
@@ -77,8 +96,75 @@ if ls $(dirname $0)/nsupdate.d/*.config &> /dev/null; then
# WAN_IP=`curl -s -$CONNECTION_TYPE ${IP_CHECK_SITE}| grep -Eo '\<[[:digit:]]{1,3}(\.[[:digit:]]{1,3}){3}\>'`
WAN_IP=$(curl -s -$CONNECTION_TYPE ${IP_CHECK_SITE})
# This is relevant for getting the specific domain record id.
API_XML_INFO="<?xml version=\"1.0\"?>
<methodCall>
<methodName>nameserver.info</methodName>
<params>
<param>
<value>
<struct>
<member>
<name>user</name>
<value>
<string>$INWX_USER</string>
</value>
</member>
<member>
<name>lang</name>
<value>
<string>en</string>
</value>
</member>
<member>
<name>pass</name>
<value>
<string>$INWX_PASS</string>
</value>
</member>
<member>
<name>domain</name>
<value>
<string>$MAIN_DOMAIN</string>
</value>
</member>
<member>
<name>name</name>
<value>
<string>$DOMAIN</string>
</value>
</member>
<member>
<name>type</name>
<value>
<string>$TYPE</string>
</value>
</member>
</struct>
</value>
</param>
</params>
</methodCall>"
API_XML="<?xml version=\"1.0\"?>
# The full xpath is
# XPATH='string(/methodResponse/params/param/value/struct/member[name="resData"]/value/struct/member[name="record"]/value/array/data/value/struct/member[name="id"]/value/int)'
# A short version of the xpath
XPATH='string(//member[name="id"]/value/int/text())'
if [[ "$NO_XMLLINT" != "true" ]]; then
if [[ "$NSLOOKUP" != "$WAN_IP" ]]; then
if [[ "$INWX_DOMAIN_ID" == "unset" ]]; then
INWX_DOMAIN_ID=$(curl -s -X POST https://api.domrobot.com/xmlrpc/ \
-H "Content-Type: application/xml" \
-d "$API_XML_INFO" \
| xmllint --xpath $XPATH -)
if [[ "$SILENT" == "NO" ]]; then
echo $(printf "%s - The %s-Type Record-ID of %s is: %s" "$(date)" "$TYPE" "$DOMAIN" "$INWX_DOMAIN_ID")>>$LOG
fi
fi
fi
fi
API_XML_UPDATE_RECORD="<?xml version=\"1.0\"?>
<methodCall>
<methodName>nameserver.updateRecord</methodName>
<params>
@@ -128,7 +214,10 @@ if ls $(dirname $0)/nsupdate.d/*.config &> /dev/null; then
</methodCall>"
if [[ "$NSLOOKUP" != "$WAN_IP" ]]; then
curl -s -XPOST -H "Content-Type: application/xml" -d "$API_XML" https://api.domrobot.com/xmlrpc/
curl -s -X POST https://api.domrobot.com/xmlrpc/ \
-H "Content-Type: application/xml" \
-d "$API_XML_UPDATE_RECORD"
if [[ "$SILENT" == "NO" ]]; then
echo "$(date) - $DOMAIN updated. Old IP: "$NSLOOKUP "New IP: "$WAN_IP >> $LOG
fi
@@ -138,6 +227,8 @@ if ls $(dirname $0)/nsupdate.d/*.config &> /dev/null; then
fi
fi
unset TYPE
unset MAIN_DOMAIN
unset DOMAIN
unset IPV6
unset MX
@@ -147,7 +238,8 @@ if ls $(dirname $0)/nsupdate.d/*.config &> /dev/null; then
unset INWX_PASS
unset INWX_USER
unset INWX_DOMAIN_ID
unset API_XML
unset API_XML_UPDATE_RECORD
unset API_XML_INFO
done
else
echo "There does not seem to be any config file available in $(dirname $0)/nsupdate.d/."