Scripts/dynpowerup: Difference between revisions

From fakedWiki
Jump to: navigation, search
No edit summary
m (6 revisions imported)
 
(6 intermediate revisions by the same user not shown)
Line 5: Line 5:
/etc/ppp/ip-up.d/dynpowerup
/etc/ppp/ip-up.d/dynpowerup
</pre>
</pre>
This is the original version of the script, which requires you to add you records manually:
<pre>
<pre>
#!/bin/bash
#!/bin/bash


RECORDS=("example.com" "ns1.example.com" "ns2.example.com" "example.org" "example.net")
RECORDS=("example.com" "ns1.example.com" "ns2.example.com" "example.org" "example.net")
SOA="ns1.example.com. hostmaster.example.com. `date +%Y%m%d%H` 10800 3600 604800 3600"


DBUSER="your-powerdns-user"
DBUSER="your-powerdns-user"
Line 23: Line 25:


if [ "$IP" != "$OLD_IP" ]; then
if [ "$IP" != "$OLD_IP" ]; then
   echo "IP address changed from $OLD_IP to $IP"
   echo "IP address changed from $OLD_IP to $IP" | logger -t DynPowerUp
   for RECORD in ${RECORDS[@]}; do
   for RECORD in ${RECORDS[@]}; do
     echo "updating record: $RECORD"
     echo "updating record: $RECORD" | logger -t DynPowerUp
     COMMAND="$COMMAND UPDATE ${DBNAME}.records SET content = \"$IP\" WHERE name = \"$RECORD\";"
     COMMAND="$COMMAND UPDATE ${DBNAME}.records SET content = \"$IP\" WHERE name = \"$RECORD\" AND type = 'A';"
    COMMAND="$COMMAND UPDATE ${DBNAME}.records SET content = \"$SOA\" WHERE name = \"$RECORD\" AND type = 'SOA';"
   done
   done
   mysql -u${DBUSER} -p${DBPASS} ${DBNAME} -e "$COMMAND"
   mysql -u${DBUSER} -p${DBPASS} ${DBNAME} -e "$COMMAND"
   echo "$IP" > /tmp/nsup.IP
   echo "$IP" > /tmp/nsup.IP
   echo "IP updates done"
   echo "IP updates done" | logger -t DynPowerUp
fi
</pre>
 
And this is a version which looks for a specific NS record and updates all A records with a TTL of 60:
<pre>
#!/bin/bash
 
NS="ns1.example.com"
TTL="60"
SOA="ns1.example.com. hostmaster.example.com. `date +%Y%m%d%H` 10800 3600 604800 3600"
 
DBUSER="your-powerdns-user"
DBPASS="your-powerdns-password"
DBNAME="your-powerdns-database"
 
RECORDS="SELECT name FROM records WHERE type = 'A' AND ttl = '"${TTL}"' AND domain_id IN
        (SELECT domain_id FROM records WHERE type = 'NS' AND content = '"${NS}"');"
 
IP=`lynx -dump http://checkip.dyndns.org | awk '{print $4}'`
 
if [ -e "/tmp/nsup.IP" ]; then
  read OLD_IP < "/tmp/nsup.IP"
else
  OLD_IP="0.0.0.0"
fi
 
if [ "${IP}" != "${OLD_IP}" ]; then
  echo "IP address changed from ${OLD_IP} to ${IP}" | logger -t nsup
  for RECORD in `mysql -u${DBUSER} -p${DBPASS} ${DBNAME} -NB -e "${RECORDS}"`; do
    echo "updating record: ${RECORD}" | logger -t nsup
    COMMAND="${COMMAND} UPDATE ${DBNAME}.records SET content = \"$IP\" WHERE name = \"${RECORD}\" AND type = 'A';"
    COMMAND="${COMMAND} UPDATE ${DBNAME}.records SET content = \"$SOA\" WHERE name = \"${RECORD}\" AND type = 'SOA';"
  done
  mysql -u${DBUSER} -p${DBPASS} ${DBNAME} -e "$COMMAND"
  echo "${IP}" > /tmp/nsup.IP
  echo "IP updates done" | logger -t nsup
fi
fi
</pre>
</pre>

Latest revision as of 20:10, 26 August 2016

This script will update the A records for zones you host on your local PowerDNS nameserver to your (external) dynamic IP.

I'm running this script after every reconnect of my VDSL link through ppp's ip-up scripts by putting it here:

/etc/ppp/ip-up.d/dynpowerup

This is the original version of the script, which requires you to add you records manually:

#!/bin/bash

RECORDS=("example.com" "ns1.example.com" "ns2.example.com" "example.org" "example.net")
SOA="ns1.example.com. hostmaster.example.com. `date +%Y%m%d%H` 10800 3600 604800 3600"

DBUSER="your-powerdns-user"
DBPASS="your-powerdns-password"
DBNAME="your-powerdns-database"

IP=`lynx -dump http://checkip.dyndns.org | awk '{print $4}'`

if [ -e "/tmp/nsup.IP" ]; then
  read OLD_IP < "/tmp/nsup.IP"
else
  OLD_IP="0.0.0.0"
fi

if [ "$IP" != "$OLD_IP" ]; then
  echo "IP address changed from $OLD_IP to $IP" | logger -t DynPowerUp
  for RECORD in ${RECORDS[@]}; do
    echo "updating record: $RECORD" | logger -t DynPowerUp
    COMMAND="$COMMAND UPDATE ${DBNAME}.records SET content = \"$IP\" WHERE name = \"$RECORD\" AND type = 'A';"
    COMMAND="$COMMAND UPDATE ${DBNAME}.records SET content = \"$SOA\" WHERE name = \"$RECORD\" AND type = 'SOA';"
  done
  mysql -u${DBUSER} -p${DBPASS} ${DBNAME} -e "$COMMAND"
  echo "$IP" > /tmp/nsup.IP
  echo "IP updates done" | logger -t DynPowerUp
fi

And this is a version which looks for a specific NS record and updates all A records with a TTL of 60:

#!/bin/bash

NS="ns1.example.com"
TTL="60"
SOA="ns1.example.com. hostmaster.example.com. `date +%Y%m%d%H` 10800 3600 604800 3600"

DBUSER="your-powerdns-user"
DBPASS="your-powerdns-password"
DBNAME="your-powerdns-database"

RECORDS="SELECT name FROM records WHERE type = 'A' AND ttl = '"${TTL}"' AND domain_id IN
        (SELECT domain_id FROM records WHERE type = 'NS' AND content = '"${NS}"');"

IP=`lynx -dump http://checkip.dyndns.org | awk '{print $4}'`

if [ -e "/tmp/nsup.IP" ]; then
  read OLD_IP < "/tmp/nsup.IP"
else
  OLD_IP="0.0.0.0"
fi

if [ "${IP}" != "${OLD_IP}" ]; then
  echo "IP address changed from ${OLD_IP} to ${IP}" | logger -t nsup
  for RECORD in `mysql -u${DBUSER} -p${DBPASS} ${DBNAME} -NB -e "${RECORDS}"`; do
    echo "updating record: ${RECORD}" | logger -t nsup
    COMMAND="${COMMAND} UPDATE ${DBNAME}.records SET content = \"$IP\" WHERE name = \"${RECORD}\" AND type = 'A';"
    COMMAND="${COMMAND} UPDATE ${DBNAME}.records SET content = \"$SOA\" WHERE name = \"${RECORD}\" AND type = 'SOA';"
  done
  mysql -u${DBUSER} -p${DBPASS} ${DBNAME} -e "$COMMAND"
  echo "${IP}" > /tmp/nsup.IP
  echo "IP updates done" | logger -t nsup
fi