title: How do I fill in unused ip addresses in a python sublist?
body: <p>I have created a python script to try and make my life as a system administrator a lot easier.
  The point of this script is to convert a Microsoft DHCP server dump file into a sorted CSV file.</p>
  
  <p>I will include the code here and am thankfull for all kinds of improvements.</p>
  
  <p><strong>My problem</strong></p>
  
  <p>My script creates a list of lists (one for each dhcp reservation). For example:</p>
  
  <pre><code>[
    # [DHCP SERVER, IP ADDRESS, MAC ADDRESS, HOSTNAME, DESCRIPTION]
    [server1,172.16.0.120,31872fcefa33,wks120.domain.net,Description of client]
    [server1,172.16.0.125,4791ca3d7279,wks125.domain.net,Description of client]
    [server1,172.16.0.132,6035a71c930c,wks132.domain.net,Description of client]
    ...
  ]
  </code></pre>
  
  <p>The unused ip addresses are not listed. But I would like my script to automatically add sublists for all the unused IP addresses in between and give them a comment saying "Unregistered" or something.</p>
  
  <p>I have no clue how to even begin searching google on how to complete this task, so any help would be appreciated :)</p>
  
  <p><strong>The script</strong></p>
  
  <pre><code>#!/usr/bin/python
  import sys, shlex
  from operator import itemgetter
  
  # Function: get_dhcp_reservations
  #
  # Extracts a list of ip reservations from a Microsoft DHCP server dump file
  # then it stores the processed reservations them in a nested list
  
  def get_dhcp_reservations(dmpFile):
  
    # Setup empty records list
    records = []
  
    # Open dump file for reading
    dmpFile = open(dmpFile,"r")
  
    # Iterate dump file line by line
    for line in dmpFile:
  
      # Only user lines with the word "reservedip" in it
      if "reservedip" in line:
  
        # Split the line into fields excluding quoted substrings
        field = shlex.split(line)
  
        # Create a list of only the required fields
        result = [field[2][1:9], field[7], field[8], field[9], field[10]]
  
        # Append each new record as a nested list
        records.append(result)
  
    # Return the rendered data
    return records
  
  
  # Function: sort_reservations_by_ip
  #
  # Sorts all records by the IPv4 address field
  
  def sort_reservations_by_ip(records):
  
    # Temporarily convert dotted IPv4 address to tuples for sorting
    for record in records:
      record[1] = ip2tuple(record[1])
  
    # Sort sublists by IP address
    records.sort(key=itemgetter(1)) 
  
    # Convert tuples back to dotted IPv4 addresses
    for record in records:
      record[1] = tuple2ip(record[1])
  
    return records
  
  
  # Function: ip2tuple
  #
  # Split ip address into a tuple of 4 integers (for sorting)
  
  def ip2tuple(address):
    return tuple(int(part) for part in address.split('.'))
  
  
  # Function: tuple2ip
  #
  # Converts the tuple of 4 integers back to an dotted IPv4 address
  
  def tuple2ip(address):
  
    result = ""
  
    for octet in address:
      result += str(octet)+"."
  
    return result[0:-1]
  
  
  # Get DHCP reservations
  records = get_dhcp_reservations(sys.argv[1])
  
  # Sort reservations by IP address
  records = sort_reservations_by_ip(records)
  
  # Print column headings
  print "DHCP Server,Reserved IP,MAC Address,Hostname,Description"
  
  # Print in specified format records
  for record in records:
    print record[0]+","+record[1]+",\""+record[2]+"\","+record[3]+","+record[4]
  </code></pre>
  
  <p>NOTE: I have also tried IPv4 sorting by using the python <a href="http://docs.python.org/2/library/socket.html#socket.inet_ntoa" rel="nofollow">socket.inet_ntoa</a> as suggested in other topics on this site but was not successful to get it working.</p>
  
  <p><strong>Dump File Example</strong></p>
  
  <p>Per request, here is some of the dump file</p>
  
  <pre><code>[Ommited content]
  
  # ======================================================================
  #  Start Add ReservedIp to the Scope : 172.16.0.0, Server : server1.domain.net            
  # ======================================================================
  
  
      Dhcp Server \\server1.domain.net Scope 172.16.0.0 Add reservedip 172.16.0.76 0800278882ae "wks126devlin.domain.net" "Viana (VM)" "BOTH"
      Dhcp Server \\server1.domain.net Scope 172.16.0.0 Add reservedip 172.16.0.118 001e37322202 "WKS18.domain.net" "Kristof (linux)" "BOTH"
      Dhcp Server \\server1.domain.net Scope 172.16.0.0 Add reservedip 172.16.0.132 000d607205a5 "WKS32.domain.net" "Lab PC" "BOTH"
      Dhcp Server \\server1.domain.net Scope 172.16.0.0 Add reservedip 172.16.0.156 338925b532ca "wks56.domain.net" "Test PC" "BOTH"
      Dhcp Server \\server1.domain.net Scope 172.16.0.0 Add reservedip 172.16.0.155 001422a7d474 "WKS55.domain.net" "Liesbeth" "BOTH"
      Dhcp Server \\server1.domain.net Scope 172.16.0.0 Add reservedip 172.16.0.15 0800266cfe31 "xpsystst.domain.net" "Pascal (VM)" "BOTH"
  
  [Ommited content]
  </code></pre>