adamz88
adamz88

Reputation: 319

How to remove a search domain by name from the resolver in dnspython?

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.

Upvotes: 1

Views: 635

Answers (1)

kimbo
kimbo

Reputation: 2693

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).

Upvotes: 0

Related Questions