Python Forum
python 3 dns lookup private domain - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: python 3 dns lookup private domain (/thread-29783.html)



python 3 dns lookup private domain - didact - Sep-19-2020

Hi everyone, I'm new to Python and new to this site so I apologize if this question has been asked and answered before.

I'm trying to do a DNS lookup to get the IP of a device on our corporate network - not a device in the public domain.
Our devices have general names like Site#-deviceType-number. When I ssh to them, I'm assuming there is a DNS lookup done, and I'm guided to the correct IP address in the background.

I've written a program that logs into our cisco and ciena equipment and runs some commands - but it only works if I supply the IP address - not the device name. I'd like to write some code that does a quick lookup and then proceeds with the rest of my program.

So far I've tried working with resolver.Resolver() and Nslookup but I'm just not having any luck.
The closest thing I have to a success is this (success meaning it didn't give me any errors, but it just returns empty lists):

from nslookup import Nslookup

device_name = "my_device_name"
dns_query = Nslookup(dns_servers=["dns.ip.add.rss"]) # the actual ip address obviously
ip_address = dns_query.dns_lookup(device_name)
print(ip_address.response_full, ip_address.answer)[/font]



RE: python 3 dns lookup private domain - bowlofred - Sep-19-2020

(Sep-19-2020, 01:52 PM)didact Wrote: When I ssh to them, I'm assuming there is a DNS lookup done, and I'm guided to the correct IP address in the background.

While that may be true, ssh has other things it can do. You can create host aliases that would prevent the need for a DNS lookup. You could associate an IP with a name in the config. You could validate if the DNS is working on the local host with dig or nslookup just to make sure it's valid in the first place.

Quote:I've written a program that logs into our cisco and ciena equipment and runs some commands - but it only works if I supply the IP address - not the device name. I'd like to write some code that does a quick lookup and then proceeds with the rest of my program.

Your other equipment may have different DNS configurations than your workstation. So resolution may happen differently.

Try this to use the local resolver on your machine:
import socket

def name2ip(hostname):
    try:
        ip = socket.gethostbyname(hostname)
        return ip
    except socket.gaierror:
        return None


for name in ["www.google.com", "www.python.org", "no.such.name"]:
    print(f"{name} resolves to {name2ip(name)}")
Output:
www.google.com resolves to 216.58.195.68 www.python.org resolves to 151.101.40.223 no.such.name resolves to None