1

I'm using dnspython to conduct DNS queries. Since my machine is joined to my company's domain, the corporate domain is part of my search domains. However, I NEVER want that domain to be appended when I am doing forward lookups on hostnames.

An approach that I've taken to remove unwanted nameservers by value is the following:

import dns.resolver

my_resolver = dns.resolver.Resolver()
my_resolver.nameservers.remove('172.20.10.1')

Unfortunately, I cannot take the same approach( or I don't know how) with for my_resolver.search because its elements are <class 'dns.name.Name'> instances and not strings.

Since my corporate domain seems to be the last element in my_resolver.search I remove it like so: del my_resolver.search[-1]. But I want to remove it by value, how can I do so, preferably without iterating through my_resolver.search.

adamz88
  • 319
  • 3
  • 12

1 Answers1

0

But I want to remove it by value, how can I do so, preferably without iterating through my_resolver.search

Just create a dns.name.Name from the string you have.

For example:

import dns.name
import dns.resolver

my_resolver = dns.resolver.Resolver()

name_to_remove = 'example.com'
name = dns.name.from_text(name_to_remove)
my_resolver.search.remove(name)

You could also use dns.query.udp() and other related functions instead of dns.resolver.Resolver.query(). Then if you really need search names you can manually add whatever search names you want in a for loop, for example. That's exactly how they do it in the Resolver class (see https://github.com/rthalley/dnspython/blob/57d38840f3cb59b838e49fe65caa6062a0904832/dns/resolver.py#L892).

kimbo
  • 2,513
  • 1
  • 15
  • 24
  • Couldn't one also use the `dns.resolver.Resolver.resolve()` function with search=None (default) or search=False ? https://dnspython.readthedocs.io/en/latest/resolver-class.html#dns.resolver.Resolver.resolve – wesinat0r Feb 03 '22 at 13:19