def test_ns_records():
    actual_ns_records = dnsq.ns_records(resolver, domain=SEARCH_DOMAIN)
    expected_ns_records = [
        'ns1.' + SEARCH_DOMAIN + '.',
        'ns2.' + SEARCH_DOMAIN + '.',
    ]
    assert actual_ns_records == expected_ns_records
Exemple #2
0
def test_ns_records_with_default_arguments_will_invoke_resolver_dot_query_and_return_a_non_empty_list_of_nameservers():
    domain = 'foo-domain.'
    mocked_results = mock_NS_Answer(domain=domain, message=MOCKED_DNS_NS_MESSAGE)

    resolver = mock.MagicMock(spec=dns.resolver.Resolver)
    resolver.query.return_value = mocked_results
    actual_ns_records = dnsq.ns_records(resolver=resolver, domain=domain)
    expected_ns_records = [x.to_text() for x in mocked_results]
    expected_ns_records.sort()

    assert actual_ns_records == expected_ns_records
    resolver.query.assert_called_once_with(domain, 'NS')
Exemple #3
0
def test_ns_type_nameserver_domain_long_arguments():
    assert_cli(
        'dnsq --type ns --nameserver {nameserver} --domain {domain} --timeout {timeout}'
        .format(nameserver=DEFAULT_NAMESERVER,
                domain=DEFAULT_DOMAIN,
                timeout=DEFAULT_TIMEOUT),
        expected='ns1.foo-domain.com. ns2.foo-domain.com.',
        rc=0,
        stdout=True,
        stderr=True)

    resolver = dnsq.create_resolver(DEFAULT_DOMAIN, DEFAULT_NAMESERVER)
    assert dnsq.ns_records(resolver=resolver, domain=DEFAULT_DOMAIN)
Exemple #4
0
def execute(argv=None):
    """Execute the command line with argv

    Args:
        Argv `list` or `None` - When `None` reads sys.argv[1:]

    """
    parser = create_parser()
    options = parser.parse_args(argv)
    resolver = None

    if options.verbose == 1:
        dnsq.LOGGER.setLevel(logging.INFO)
    elif options.verbose >= 2:
        dnsq.LOGGER.setLevel(logging.DEBUG)

    if options.domain and options.nameserver:
        resolver = dnsq.create_resolver(search=options.domain,
                                        nameservers=options.nameserver,
                                        lifetime=options.timeout)
        options.resolver = resolver

    if options.query:
        regex = re.compile(r'{!s}'.format(options.query))
        dnsq.LOGGER.info(
            'Searching zone transfer for the following query: "{}"'.format(
                regex.pattern))
        err_msg = 'The query option requires the zone transfer capability for domain={} nameserver={}'.format(
            options.domain, options.nameserver)
        assert dnsq.supports_zone_transfer(domain=options.domain,
                                           nameserver=options.nameserver,
                                           lifetime=options.timeout), err_msg

        results = []
        for line in dnsq.zone_transfer(domain=options.domain,
                                       nameserver=options.nameserver,
                                       lifetime=options.timeout):
            for item in line:
                if regex.search(item):
                    hostname, rec_type, alias = (line[0], line[-2], line[-1])
                    if options.sort_by == 'ip':
                        result = ' '.join([alias, rec_type, hostname])
                    elif options.sort_by == 'hostname' or options.sort_by is None:
                        result = ' '.join([hostname, rec_type, alias])
                    results.append(result)

        # sort the results
        if options.sort_by is None or options.sort_by == 'hostname':
            sorted_results = '\n'.join(sorted(results))
            print(sorted_results)
        elif options.sort_by == 'ip':
            sorted_results = '\n'.join(dnsq.sort_ips(results))
            print(sorted_results)

        sys.exit(0)

    if options.supports_axfr:
        result = dnsq.supports_zone_transfer(domain=options.domain,
                                             nameserver=options.nameserver,
                                             lifetime=options.timeout)
        if result:
            sys.exit(0)
        sys.exit(1)

    if options.type == 'ns':
        recs = dnsq.ns_records(resolver, domain=options.domain)
        rec_str = ' '.join(recs)
        print(rec_str)
        sys.exit(0)
    elif options.type == 'soa':
        recs = dnsq.soa_records(resolver, domain=options.domain)
        print('\n'.join([' '.join(rec) for rec in recs]))
        sys.exit(0)
    elif options.type == 'axfr' or options.type == 'zone-transfer':
        err_msg = ('ERR: The domain: "{domain}" via nameserver: "{nameserver}"'
                   'does not support AXFR (zone-transfers)\n').format(
                       domain=options.domain, nameserver=options.nameserver)

        if dnsq.supports_zone_transfer(domain=options.domain,
                                       nameserver=options.nameserver,
                                       lifetime=options.timeout) is False:
            sys.stderr.write(err_msg)
            sys.exit(1)
        for line in dnsq.zone_transfer(domain=options.domain,
                                       nameserver=options.nameserver,
                                       lifetime=options.timeout):
            result = ' '.join(line)
            print(result)
        sys.exit(0)