I have recently written a script in python that processes a Microsoft Windows DHCP server dump file and generates an XML file of the current reservations using spreadsheet XML formatting.
The script basically opens a file using the python open() command, then iterates over every line (for line in file) and looks for the key word reservedip. If the keyword is found, the line is broken up into fields using the shlex split() command.
However, when I run this script with the default dump files of the microsoft DHCP server, I get no results. also note that I was unable to use Linux's grep command to search in the file
I then tried to open the file in gedit and save it as a unix text file. After this was done, I got results and was able to grep within the file. This method however defeats the whole point of writing a script to automate my work.
I have been searching on google but had no luck in finding what I am looking for. I also tried to open the file in binary mode, but this was also no help.
I hope somebody can help me with this.
As per request, here is an example of what the script does (at least the looping part) and the DHCP server output:
Script
# Setup an empty dictionary to store the extracted records
records = {}
# Open dhcp dump file
f = open(dhcp.txt, "r")
# Iterate file line by line
for line in f:
# Only use line with the word "reservedip" in it
if "reservedip" in line:
# Split line into fields by spaces (excluding quoted substrings)
field = shlex.split(line)
# Add new entry for each record using the 32bit IP address int as it's key
records[addr_to_int(field[7])] = [field[7], field[8], field[9], field[10]]
*note: addr_to_int is a function I wrote that converts a dotted IPv4 address to an integer*
DHCP dump
Unfortunately I cannot include the real DHCP server dump due to company policy. But the lines I'm trying to get out of the file look like this:
Dhcp Server \\servername.company.local Scope 172.16.104.0 Add reservedip 172.16.104.207 003386dd00gg "hostname.company.local" "Host Description" "BOTH"
Thanks in advance, Pascal