Re: just a better script for those with other setup

Home Page Forums Network Management ZeroShell allow DNS dynamic updates from DHCP server Re: just a better script for those with other setup

#46314

mashuser
Member

This allows you to use other networks than 192.168.0 like 172.16.0.0/16
The assumption is you have reverse zones for each set of /24

In my case I got:
172.16.1.0/24
172.16.2.0/23
172.16.252.0/24
172.16.253.0/24
172.16.254.0/24

172.16.2.0/23 has two reverse zones 2.16.172.in-addr.arpa and 3.16.172.in-addr.arpa so that the hosts in those addresses do not collide with each other.


#!/bin/bash
#############################################################################
#This program is free software; you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation; either version 2 of the License, or
#(at your option) any later version.
#
#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#GNU General Public License for more details.
#
#You should have received a copy of the GNU General Public License
#along with this program; if not, write to the Free Software
#Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
#Write by Alessandro Cartatone
#Porting in bash of perl script dhcp2ldap by Travis Groth for use in Zeroshell distribution
#modified by Benj Dag to work with a complicated setup
#############################################################################


lease_file="/Database/var/register/system/dhcp/dhcpd.leases"
log_file="/Database/dhcp2ldap2.log" # insert a path to write log file
domain="example.com"
reverse=".in-addr.arpa" #your reverse domain
FORWARD_BASE=",dc=example,dc=com,ou=DNS,dc=example,dc=com"
REVERSE_BASE=",ou=DNS,dc=example,dc=com"
USER="cn=Manager,dc=example,dc=com" #default user manager of zeroshell ldap - verify in /etc/openldap/slapd.conf
PASSWORD="password" #admin password - verify in /etc/openldap/slapd.conf
UPDATE_TIME=30 # In Seconds
AUTO_VERIFY=0
check_count=0
result=0

usage()
{

echo "dhcp2ldap.sh : Dynamic DNS Updates for the Bind9 LDAP backend
Copyright 2012 Alessandro Cartatone under the GNU GPL based on dhcp2ldapd Copyright 2005 Travis Groth
To demonize: nohup /dhcp2ldap.sh 0/dev/null 2>&1 &
Please edit the config variables before running!"

}


parse()
{
lease_found=0
counter=0




while read line
do


if [ "$(echo "$line" | awk '/lease/ {print $1}')" = "lease" ]
then
ip="$(echo "$line" | awk '/lease/ {print $2}')"
lease_found=1
fi

if [ $lease_found ]
then
if [ "$(echo "$line" | awk '/client-hostname/ {print $1}')" = "client-hostname" ]
then
hostname="$(echo "$line" | awk '/client-hostname/ {print $2}')"
hostname="$(echo "$hostname" | sed "s/"//g")"
hostname="$(echo "$hostname" | sed "s/;//g")"
hostnamearray[counter]=$hostname
iparray[counter]=$ip
lease_found=0
counter=$counter+1
fi

if [ "$(echo "$line" | awk '/lease/ {print $1}')" = "}" ]
then
lease_found=0
fi

fi

done < $lease_file

}


addldapforward()
{
#add forward zone
rm -f /tmp/addhost.ldif 2>&1 >/dev/null
echo "dn: relativeDomainName="$2$FORWARD_BASE > /tmp/addhost.ldif
echo "objectClass: top" >> /tmp/addhost.ldif
echo "objectClass: dNSZone" >> /tmp/addhost.ldif
echo "relativeDomainName: "$2 >> /tmp/addhost.ldif
echo "dNSTTL: 7200" >> /tmp/addhost.ldif
echo "zoneName: "$domain >> /tmp/addhost.ldif
echo "aRecord: "$1 >> /tmp/addhost.ldif
ldapadd -x -D $USER -w $PASSWORD -f /tmp/addhost.ldif 2>&1 >/dev/null
}

addldapreverse()
{
#add reverse zone
rm -f /tmp/addhost.ldif 2>&1 >/dev/null
echo "dn: relativeDomainName="$(echo "$1" | sed -r 's/^([0-9]{1,3}).([0-9]{1,3}).([0-9]{1,3}).([0-9]{1,3})$/4,dc=3,dc=2,dc=1,dc=in-addr,dc=arpa/g')$REVERSE_BASE > /tmp/addhost.ldif
echo "objectClass: top" >> /tmp/addhost.ldif
echo "objectClass: dNSZone" >> /tmp/addhost.ldif
echo "relativeDomainName: "$(echo "$1" | cut -d'.' -f4) >> /tmp/addhost.ldif
echo "dNSTTL: 7200" >> /tmp/addhost.ldif
echo "zoneName: "$(echo "$1" | sed -r 's/^([0-9]{1,3}).([0-9]{1,3}).([0-9]{1,3}).([0-9]{1,3})$/3.2.1.in-addr.arpa/g') >> /tmp/addhost.ldif
echo "pTRRecord: "$2"."$domain"." >> /tmp/addhost.ldif
cat /tmp/addhost.ldif
ldapadd -x -D $USER -w $PASSWORD -f /tmp/addhost.ldif 2>&1 >/dev/null
}

deleteldapforward()
{

ldapdelete -x -D $USER -w $PASSWORD "relativeDomainName="$1$FORWARD_BASE 2>&1 >/dev/null

}


deleteldapreverse()
{

ldapdelete -x -D $USER -w $PASSWORD "relativeDomainName="$(echo "$1" | sed -r 's/^([0-9]{1,3}).([0-9]{1,3}).([0-9]{1,3}).([0-9]{1,3})$/4,dc=3,dc=2,dc=1,dc=in-addr,dc=arpa/g')$REVERSE_BASE 2>&1 >/dev/null

}


do_stuff()
{
count=0
readd=0
notfound="found:"
for i in "${iparray[@]}"
do
lookup="$(host "${hostnamearray[count]}"".""$domain")"
lookup="$(echo "$lookup" | awk '{print $4}')"
if [ "$lookup" = "${iparray[count]}" ]
then
count=$count+1
continue
fi

#when it's "found:" no record in ldap
if "$lookup" != "$notfound" && "$lookup" != "${iparray[count]}"
then
#delete record in ldap
echo "$(date '+%F %T')" "delete record in ldap" $lookup "${iparray[count]}" ${hostnamearray[count]} >> "$log_file"
deleteldapforward ${hostnamearray[count]}
deleteldapreverse "${iparray[count]}"
readd=1
fi

if | $readd
then
echo "$(date '+%F %T')" "add record in ldap" "${iparray[count]}" ${hostnamearray[count]} >> "$log_file"
addldapforward "${iparray[count]}" ${hostnamearray[count]}
addldapreverse "${iparray[count]}" ${hostnamearray[count]}
fi


count=$count+1
done


}

changed()
{
result=0
curstat="$(stat -c %y "$lease_file")"

if [ $AUTO_VERIFY ]
then
check_count=$check_count+1
fi

if [ "$oldstat" != "$curstat" ] || [ $check_count = $AUTO_VERIFY -a $AUTO_VERIFY ]
then
oldstat="$curstat"
check_count=0
result=1
else
result=0
fi


}



if [ "$1" = "-h" ]
then
usage
exit
else

while :
do
changed
if [ $result -eq 1 ]
then
parse
do_stuff
fi
sleep $UPDATE_TIME
done

fi

[/code]