Skip to content

Commit

Permalink
Merge pull request #26 from EV21/GH-25
Browse files Browse the repository at this point in the history
GH-25 Getting the Domain-Record-ID via XML-RPC API
  • Loading branch information
chrisb86 committed Jul 3, 2020
2 parents cb382a2 + ef034e5 commit 8beff3d
Show file tree
Hide file tree
Showing 3 changed files with 169 additions and 32 deletions.
40 changes: 35 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,54 @@ In order to run the script you need to have installed the following command line
- _awk_
- _nslookup_ or _drill_

recommendation

- _xmllint_ (libxml2-utils)
If _xmllint_ is not on your system you have to set the domain record id in your config files.

Note: 2-Factor-Authentification method (2FA) is not implemented.

## Installation

Simply clone this project or download the `master.zip` and extract it, e.g., using `wget` and `7z x master.zip`.

Place your config files in the `nsupdate.d` folder. A `dist.config.sample` is provided. At least one config file needs to exist, ending with `.config.
Place your config files in the `nsupdate.d` folder. A `dist.config.sample` file with all possible options is provided. At least one config file needs to exist, ending with `.config.
All .config files (one for each dns-record) will be processed by looping them.
Simply copy the provided dist.config.sample and adjust your config to your needs.
For home.example.com you may create:
home.example.com-ipv4.config and/or
home.example.com-ipv6.config

For home.example.com you may create:
A-Record Update configuration e.g.
`myV4.config`
```
INWX_USER="USERNAME"
INWX_PASS="PASSWORD"
MAIN_DOMAIN="example.de"
DOMAIN="home.example.de"
TYPE="A"
IP_CHECK_SITE="https://api.ipify.org"
```
AAAA-Record Update configuration e.g.
`myV6.config`
```
INWX_USER="USERNAME"
INWX_PASS="PASSWORD"
MAIN_DOMAIN="example.de"
DOMAIN="home.example.de"
TYPE="AAAA"
IP_CHECK_SITE="https://api6.ipify.org"
```

## Run nsupdate by cron
With `crontab -e` you can add the following line for running the script every 5 minutes:
`*/5 * * * * bash /home/$USER/nsupdate/nsupdate.sh`

## Changelog

**2020-07-03**

- Rearranged config.sample
- Updated Readme
- Getting the Domain-Record-ID via XML-RPC API

**2020-05-11**

- Updated Readme with some hints
Expand Down
57 changes: 36 additions & 21 deletions nsupdate.d/dist.config.sample
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,52 @@
# The nsupdate.sh is processing all .config files by looping.
# If you want to update an IPv4 and an IPv6 record you need two config files.

# Log file name.
LOG="$0.log"
# Login credentials for the inwx admin interface
INWX_USER="USERNAME"
INWX_PASS="PASSWORD"

# The domain that you want to update AND its main/zone domain
# The MAIN_DOMAIN is needed for the API-Request to get the record ID
MAIN_DOMAIN="example.com"
DOMAIN="home.example.com"
# If it is the same domain:
#DOMAIN="example.com"

# Set the record type to either A, AAAA or MX
# For MX you would normaly just set a domain and not an ip, so this is just a option.
TYPE="A"

# From which site should we get your WAN IP?
# Note that for IPv4 or IPv6 the IP_CHECK_SITE should work
# e.g. for IPv4 use api.ipify.org and for IPv6 use api6.ipify.org
IP_CHECK_SITE="https://api.ipify.org/"

# Use drill instead of nslookup for hostname lookup.
USE_DRILL="NO"

# Use IPv6 connection.
IPV6="NO"

# Update an MX record.
MX="NO"

# Suppress all messages and deactivates logging.
SILENT="NO"

# TTL: Time to Live
# default TTL setting by inwx is 3600 (1 hour)
# minimum TTL allowed by inwx is 300 (5 minutes) for regular nameserver record updates
TTL=300
# In this script the defaul setting is 300.
#TTL=300

# Login credentials for the inwx admin interface
INWX_USER="USERNAME"
INWX_PASS="PASSWORD"
# Log file name.
LOG="$0.log"

# Suppress all messages and deactivates logging.
# default is SILENT="NO"
#SILENT="YES"

# Use drill instead of nslookup for hostname lookup.
#USE_DRILL="YES"

# The hostname that you want to update and it's ID from the inwx interface.
# The domain-Record-ID from the inwx interface.
# If the command-line-tool "xmllint" is working you dont need to set this option.
# Note: You can get the specific domain record ID while editing the given nameserver entry by
# inspecting the target URL of the save button.
DOMAIN="DOMAIN"
INWX_DOMAIN_ID="123456789"
#INWX_DOMAIN_ID="123456789"

# Use IPv6 connection.
# deprecated option for backward compatibility, use TYPE instead
#IPV6="NO"

# Update an MX record.
# deprecated option for backward compatibility, use TYPE instead
#MX="NO"
104 changes: 98 additions & 6 deletions nsupdate.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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

Expand All @@ -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>
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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/."
Expand Down

0 comments on commit 8beff3d

Please sign in to comment.