CERN

LDAP-extraction

I’m working on extracting hostnames, MAC-address and IP-address from the current/old LDAP-database, which I’ll use to generate new configuration-files for DHCP/BIND/RADIUS in the near future. I probably did it the “hard way” (I could have used ‘ldapsearch’ or whatever, but yeah).

I made a complete LDAP-dump with ‘slapcat’. Then I extracted the DHCP-relevant parts;

cat ldap.ldif | grep -A18 "ou=dhcp" > ldap-dhcp.ldif

And to get all the hostnames;

cat ldap-dhcp.ldif | grep -i "ou=dhcp" | cut -d',' -f1 | sed -e 's/dn\: cn\=//' | grep -viE "(dhcp|192.168.0.0|group1|pool1)" > hostnames.txt

And to get only valid addresses, I made a little bash-script to extract it;

while read hostname; do
	ip=`cat ldap-dhcp.ldif | grep -A18 "cn\=$hostname\," | grep "fixed\-address" | sed -e 's/dhcpStatements\:\ fixed\-address\ //'`
	mac=`cat ldap-dhcp.ldif | grep -A18 "cn\=$hostname\," | grep "dhcpHWAddress" | sed -e 's/dhcpHWAddress\:\:\ //' | sed -e 's/dhcpHWAddress\:\ ethernet\ //'`
 
	if [ ! -z "$ip" ]; then 
		# $ip has a value
 
		echo "$hostname # $mac # $ip" >> complete-address-list.txt
 
		if [ "$mac" != "00:00:00:00:00:00" ]; then
			# only print entries that has a valid MAC-address
 
			echo "$hostname # $mac # $ip" >> only-valid-mac-addresses.txt
		fi
	fi
done < hostnames.txt

This left me with all the hosts that had an IP-address, and a non-all-zero MAC-address, which I then had to sort manually.