bin/pvl.dns-hosts
changeset 265 5f2807999222
parent 264 ce29be46f705
equal deleted inserted replaced
264:ce29be46f705 265:5f2807999222
    60 
    60 
    61     # output
    61     # output
    62     parser.add_option('--output-hosts',         metavar='FILE',
    62     parser.add_option('--output-hosts',         metavar='FILE',
    63             help="Output hosts file")
    63             help="Output hosts file")
    64 
    64 
    65     parser.add_option('--output-ip',            metavar='PREFIX',
    65     parser.add_option('--output-prefix',        metavar='PREFIX',
    66             help="Output hosts by ip prefix")
    66             help="Select hosts by ip prefix")
    67 
    67 
    68     # defaults
    68     # defaults
    69     parser.set_defaults(
    69     parser.set_defaults(
    70 
    70 
    71     )
    71     )
    89             continue
    89             continue
    90 
    90 
    91         elif rr.type == 'A' :
    91         elif rr.type == 'A' :
    92             ip, = rr.data
    92             ip, = rr.data
    93 
    93 
    94             yield rr.name, 'ip', ip
    94             yield rr.name, 'ip', ipaddr.IPAddress(ip)
    95 
    95 
    96             if rr.comment :
    96             if rr.comment :
    97                 yield rr.name, 'comment', rr.comment
    97                 yield rr.name, 'comment', rr.comment
    98 
    98 
    99         elif rr.type == 'CNAME' :
    99         elif rr.type == 'CNAME' :
   376     for host, field, value in import_hosts :
   376     for host, field, value in import_hosts :
   377         hosts[host][field].append(value)
   377         hosts[host][field].append(value)
   378     
   378     
   379     return hosts.iteritems()
   379     return hosts.iteritems()
   380 
   380 
   381 def select_hosts_ip (options, hosts, network) :
   381 def process_export_hosts (options, hosts) :
       
   382     if options.output_prefix :
       
   383         prefix = ipaddr.IPNetwork(options.output_prefix)
       
   384     else :
       
   385         prefix = None
       
   386 
   382     for host, fields in hosts :
   387     for host, fields in hosts :
   383         ip = fields.get('ip')
   388         ip = fields.get('ip')
   384 
   389         
   385         if not ip :
   390         # sort by IP
   386             continue
   391         if ip :
   387 
   392             sort = ip = ip[0]
   388         ip = ipaddr.IPAddress(ip[0])
   393         else :
   389 
   394             # fake, to sort correctly
   390         if ip in network :
   395             sort = ipaddr.IPAddress(0)
   391             yield ip, host, fields
   396         
   392 
   397         # select
       
   398         if prefix:
       
   399             if not (ip and ip in prefix) :
       
   400                 continue
       
   401 
       
   402         yield sort, host, fields
   393 
   403 
   394 def export_hosts (options, hosts) :
   404 def export_hosts (options, hosts) :
   395     """
   405     """
   396         Export hosts to file.
   406         Export hosts to file.
   397     """
   407     """
   398 
   408 
   399     file = pvl.args.apply_file(options.output_hosts, 'w', options.output_charset)
   409     file = pvl.args.apply_file(options.output_hosts, 'w', options.output_charset)
   400 
   410 
   401     if options.output_ip :
   411     # filter + sort
   402         prefix = ipaddr.IPNetwork(options.output_ip)
   412     hosts = [(host, fields) for sort, host, fields in sorted(process_export_hosts(options, hosts))]
   403     
       
   404         # filter + sort
       
   405         hosts = [(host, fields) for ip, host, fields in sorted(select_hosts_ip(options, hosts, prefix))]
       
   406 
   413 
   407     for host, fields in hosts :
   414     for host, fields in hosts :
   408         for comment in fields.get('comment', ()) :
   415         for comment in fields.get('comment', ()) :
   409             print >>file, u"# {comment}".format(comment=comment)
   416             print >>file, u"# {comment}".format(comment=comment)
   410 
   417