bin/check-dhcp-hosts
author Tero Marttila <terom@paivola.fi>
Wed, 21 Mar 2012 18:43:56 +0200
changeset 588 21b33b9090d0
permissions -rwxr-xr-x
check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
588
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
     1
#!/usr/bin/env python
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
     2
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
     3
"""
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
     4
    Go through a dhcp conf file looking for fixed-address stanzas, and make sure that they are valid.
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
     5
"""
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
     6
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
     7
__version__ = '0.0.1-dev'
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
     8
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
     9
import optparse
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    10
import codecs
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    11
import logging
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    12
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    13
import socket
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    14
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    15
log = logging.getLogger('main')
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    16
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    17
# command-line options, global state
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    18
options = None
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    19
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    20
def parse_options (argv) :
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    21
    """
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    22
        Parse command-line arguments.
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    23
    """
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    24
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    25
    prog = argv[0]
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    26
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    27
    parser = optparse.OptionParser(
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    28
            prog        = prog,
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    29
            usage       = '%prog: [options]',
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    30
            version     = __version__,
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    31
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    32
            # module docstring
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    33
            description = __doc__,
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    34
    )
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    35
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    36
    # logging
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    37
    general = optparse.OptionGroup(parser, "General Options")
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    38
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    39
    general.add_option('-q', '--quiet',     dest='loglevel', action='store_const', const=logging.ERROR, help="Less output")
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    40
    general.add_option('-v', '--verbose',   dest='loglevel', action='store_const', const=logging.INFO,  help="More output")
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    41
    general.add_option('-D', '--debug',     dest='loglevel', action='store_const', const=logging.DEBUG, help="Even more output")
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    42
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    43
    parser.add_option_group(general)
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    44
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    45
    # input/output
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    46
    parser.add_option('-c', '--input-charset',  metavar='CHARSET',  default='utf-8', 
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    47
            help="Encoding used for input files")
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    48
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    49
    # 
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    50
    parser.add_option('--doctest',              action='store_true',
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    51
            help="Run module doctests")
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    52
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    53
    # defaults
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    54
    parser.set_defaults(
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    55
        loglevel            = logging.WARN,
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    56
    )
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    57
    
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    58
    # parse
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    59
    options, args = parser.parse_args(argv[1:])
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    60
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    61
    # configure
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    62
    logging.basicConfig(
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    63
        format  = prog + ': %(name)s: %(levelname)s %(funcName)s : %(message)s',
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    64
        level   = options.loglevel,
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    65
    )
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    66
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    67
    return options, args
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    68
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    69
def parse_fixedaddrs (file) :
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    70
    """
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    71
        Go through lines in given .conf file, looking for fixed-address stanzas.
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    72
    """
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    73
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    74
    filename = file.name
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    75
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    76
    for lineno, line in enumerate(file) :
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    77
        # comments?
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    78
        if '#' in line :
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    79
            line, comment = line.split('#', 1)
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    80
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    81
        else :
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    82
            comment = None
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    83
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    84
        # whitespace
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    85
        line = line.strip()
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    86
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    87
        if not line :
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    88
            # empty
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    89
            continue
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    90
       
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    91
        # grep
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    92
        if 'fixed-address' in line :
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    93
            # great parsing :)
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    94
            fixedaddr = line.replace('fixed-address', '').replace(';', '').strip()
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    95
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    96
            log.debug("%s:%d: %s: %s", filename, lineno, fixedaddr, line)
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    97
        
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    98
            yield lineno, fixedaddr
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    99
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   100
def resolve_addr (addr, af=socket.AF_INET, socktype=socket.SOCK_STREAM) :
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   101
    """
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   102
        Resolve given address for given AF_INET, returning a list of resolved addresses.
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   103
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   104
        Raises an Exception if failed.
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   105
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   106
        >>> resolve_addr('127.0.0.1')
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   107
        ['127.0.0.1']
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   108
    """
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   109
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   110
    if not addr :
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   111
        raise Exception("Empty addr: %r", addr)
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   112
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   113
    # resolve
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   114
    result = socket.getaddrinfo(addr, None, af, socktype)
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   115
   
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   116
    #log.debug("%s: %s", addr, result)
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   117
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   118
    # addresses
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   119
    addrs = list(sorted(set(sockaddr[0] for family, socktype, proto, canonname, sockaddr in result)))
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   120
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   121
    return addrs
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   122
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   123
def check_file_hosts (file) :
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   124
    """
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   125
        Check all fixed-address parameters in given file.
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   126
    """
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   127
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   128
    filename = file.name
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   129
    fail = 0
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   130
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   131
    for lineno, addr in parse_fixedaddrs(file) :
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   132
        # lookup
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   133
        try :
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   134
            resolved = resolve_addr(addr)
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   135
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   136
        except Exception as e:
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   137
            log.warning("%s:%d: failed to resolve: %s: %s", filename, lineno, addr, e)
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   138
            fail += 1
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   139
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   140
        else :
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   141
            log.debug("%s:%d: %s: %r", filename, lineno, addr, resolved)
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   142
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   143
    return fail
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   144
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   145
def open_file (path, mode, charset) :
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   146
    """
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   147
        Open unicode-enabled file from path, with - using stdio.
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   148
    """
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   149
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   150
    if path == '-' :
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   151
        # use stdin/out based on mode
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   152
        stream, func = {
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   153
            'r':    (sys.stdin, codecs.getreader),
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   154
            'w':    (sys.stdout, codecs.getwriter),
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   155
        }[mode[0]]
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   156
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   157
        # wrap
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   158
        return func(charset)(stream)
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   159
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   160
    else :
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   161
        # open
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   162
        return codecs.open(path, mode, charset)
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   163
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   164
def main (argv) :
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   165
    global options
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   166
    
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   167
    options, args = parse_options(argv)
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   168
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   169
    if options.doctest :
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   170
        import doctest
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   171
        fail, total = doctest.testmod()
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   172
        return fail
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   173
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   174
    if args :
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   175
        # open files
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   176
        input_files = [open_file(path, 'r', options.input_charset) for path in args]
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   177
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   178
    else :
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   179
        # default to stdout
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   180
        input_files = [open_file('-', 'r', options.input_charset)]
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   181
   
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   182
    # process zone data
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   183
    for file in input_files :
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   184
        log.info("Reading zone: %s", file)
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   185
    
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   186
        fail = check_file_hosts(file)
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   187
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   188
        if fail :
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   189
            log.warn("DHCP hosts check failed: %d", fail)
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   190
            return 2
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   191
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   192
        else :
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   193
            log.info("DHCP hosts check OK")
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   194
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   195
    return 0
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   196
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   197
if __name__ == '__main__':
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   198
    import sys
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   199
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   200
    sys.exit(main(sys.argv))
21b33b9090d0 check-dhcp-hosts: parse dhcp conf for fixed-address stanzas, and ensure that they resolve..
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   201