title: Processing (binary?) file in python and linux shell
body: <p>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.</p>
  
  <p>The script basically opens a file using the python <strong>open()</strong> command, then iterates over every line <em>(for line in file)</em> and looks for the key word <em>reservedip</em>.
  If the keyword is found, the line is broken up into fields using the shlex <strong>split()</strong> command.</p>
  
  <p>However, when I run this script with the default dump files of the microsoft DHCP server, I get no results. <em>also note that I was unable to use Linux's grep command to search in the file</em></p>
  
  <p>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.</p>
  
  <p>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.</p>
  
  <p>I hope somebody can help me with this.</p>
  
  <p>As per request, here is an example of what the script does (at least the looping part) and the DHCP server output:</p>
  
  <p><strong>Script</strong></p>
  
  <pre><code># 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]]
  </code></pre>
  
  <p>*note: addr_to_int is a function I wrote that converts a dotted IPv4 address to an integer*</p>
  
  <p><strong>DHCP dump</strong></p>
  
  <p>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:</p>
  
  <blockquote>
    <p>Dhcp Server \\servername.company.local Scope 172.16.104.0 Add reservedip 172.16.104.207 003386dd00gg "hostname.company.local" "Host Description" "BOTH"</p>
  </blockquote>
  
  <p>Thanks in advance,
  Pascal</p>