Skip to content

Instantly share code, notes, and snippets.

@mrngm
Last active December 10, 2015 16:09
Show Gist options
  • Save mrngm/4459236 to your computer and use it in GitHub Desktop.
Save mrngm/4459236 to your computer and use it in GitHub Desktop.
Fetches current DHCPACKs from syslog and formats it into a BIND-friendly format. This script takes into account the current leases already placed in DNS. (Use case: replace computer hostnames with (first|last|nick)names for easy recognition). Room for update: read the dhcpd.leases file.
#!/bin/bash
date
grep dhcpd /var/log/syslog | grep hostname | grep -i dhcpack | grep 'xx.yy' | grep -v 'xx.yy.zz.1' | sed -e 's/\(.*\)DHCPACK on \(.*\) to \(.*\)\( \(.*\)\|\) via vlanUU$/\2 \3 \4/' | grep -v 'hostname' | sort -u > currdhcpleases.txt
rm forwarddns.zone
rm reversedns.zone
while read IPADDR MAC HOST
do
GREP=`grep $IPADDR /etc/bind/somezonefile.zone`
RET=$?
KNOWNHOST=`echo $GREP | awk '{print $1};'`
STRIPPEDHOST=`echo $HOST | sed -e 's/(\(.*\))/\1/'`
STRIPPEDIP=`echo $IPADDR | cut -d '.' -f 4`
if [[ "$RET" -eq "0" ]]; then
# add forward dns
echo -n "$KNOWNHOST" >> forwarddns.zone
echo -ne "\t\t" >> forwarddns.zone
NUMCHARS=`echo $KNOWNHOST | wc -c`
if [[ "$NUMCHARS" -lt "9" ]]; then
echo -ne "\t" >> forwarddns.zone
fi
echo "IN A $IPADDR ; $MAC" >> forwarddns.zone
# add reverse dns
echo -e "$STRIPPEDIP\t\tIN PTR $KNOWNHOST.somezone." >> reversedns.zone
else
# add unknown forward dns
echo -e "$STRIPPEDHOST-new\t\tIN A $IPADDR ; $MAC" >> forwarddns.zone
# add unknown reverse dns
echo -e "$STRIPPEDIP\t\tIN PTR $STRIPPEDHOST-new.somezone." >> reversedns.zone
fi
done <currdhcpleases.txt
#cat reversedns.zone | sort -u
echo "---"
cat forwarddns.zone | sort -uk4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment