Package netaddr :: Module misc |
[hide private]
[frames] | no frames]

# Source Code for Module netaddr.misc

     1  #!/usr/bin/env python
     2  #-----------------------------------------------------------------------------
     3  #   Copyright (c) 2008-2009, David P. D. Moss. All rights reserved.
     4  #
     5  #   Released under the BSD license. See the LICENSE file for details.
     6  #-----------------------------------------------------------------------------
     7  """
     8  Miscellaneous functions and classes. Do not rely on anything in here as it is
     9  liable to change, move or be deleted with each release.
    10  """
    11  import pprint
    12
    13  from netaddr import CIDR, Wildcard
    14
    15  #-----------------------------------------------------------------------------
    16 -def ipv4_cidr_prefixes():
    17      """
    18      Returns a recordset (list of dicts) of host/network breakdown for IPv4
    19      using all of the various CIDR prefixes.
    20      """
    21      table = []
    22      prefix = 32
    23      while prefix >= 0:
    24          cidr = CIDR('0.0.0.0/%d' % prefix)
    25          table.append(dict(prefix=str(cidr), hosts=cidr.size(),
    26              networks=2 ** cidr.prefixlen))
    27          prefix -= 1
    28      return table
    29
    30  #-----------------------------------------------------------------------------
    31 -def ipv6_cidr_prefixes():
    32      """
    33      Returns a recordset (list of dicts) of host/network breakdown for IPv6
    34      using all of the various CIDR prefixes.
    35      """
    36      table = []
    37      prefix = 128
    38      while prefix >= 0:
    39          cidr = CIDR('::/%d' % prefix)
    40          table.append(dict(prefix=str(cidr), hosts=cidr.size(),
    41              networks=2 ** cidr.prefixlen))
    42          prefix -= 1
    43      return table
    44
    45  #-----------------------------------------------------------------------------
    46 -def print_ipv4_cidr_prefixes():
    47      """
    48      Prints a table to stdout of host/network breakdown for IPv4 using CIDR
    49      notation.
    50      """
    51      print '%-10s %-15s %-15s' % ('Prefix', 'Hosts', 'Networks')
    52      print '-'*10, '-'*15, '-'*15
    53      for record in ipv4_cidr_prefixes():
    54          print '%(prefix)-10s %(hosts)15s %(networks)15s' % record
    55
    56  #-----------------------------------------------------------------------------
    57 -def print_ipv6_cidr_prefixes():
    58      """
    59      Prints a table to stdout of host/network breakdown for IPv6 using CIDR
    60      notation.
    61      """
    62      print '%-10s %-40s %-40s' % ('Prefix', 'Hosts', 'Networks')
    63      print '-'*10, '-'*40, '-'*40
    64      for record in ipv6_cidr_prefixes():
    65          print '%(prefix)-10s %(hosts)40s %(networks)40s' % record
    66
    67  #-----------------------------------------------------------------------------
    68  if __name__ == '__main__':
    69      import pprint
    70      #pprint.pprint(ipv4_cidr_prefixes())
    71      print_ipv4_cidr_prefixes()
    72      #pprint.pprint(ipv6_cidr_prefixes())
    73      print_ipv6_cidr_prefixes()
    74