Python Forum
python 3 dns lookup private domain
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
python 3 dns lookup private domain
#1
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]
Reply
#2
(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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Peaks in time domain frohr 0 473 Jul-06-2023, 02:57 AM
Last Post: frohr
  Lint and private var names PatM 1 685 Dec-15-2022, 05:08 PM
Last Post: deanhystad
  2-dataframe, datetime lookup problem Mark17 0 1,229 Jan-27-2022, 01:02 AM
Last Post: Mark17
  Python VLookup? Lookup Table? Nu2Python 3 2,407 Oct-25-2021, 08:47 PM
Last Post: Nu2Python
  Unable to import Private Repo using setup.py Bob786 1 1,748 Sep-02-2021, 04:19 PM
Last Post: snippsat
  Can I replace IF statements with a key lookup ? jehoshua 3 2,491 Mar-05-2021, 10:24 PM
Last Post: jehoshua
  [split] Помощь по приватным ключам/Private key help sairam17519 0 1,604 Sep-07-2020, 12:55 PM
Last Post: sairam17519
  Download file from Private GitHub rep vinuvt 0 1,968 Jul-27-2020, 11:38 AM
Last Post: vinuvt
  Partial key lookup in dictionary GaryNR 1 3,440 Jul-16-2020, 06:55 PM
Last Post: Gribouillis
  Private package distribution abomination disadvantages research andreir 2 2,156 May-07-2020, 12:32 AM
Last Post: andreir

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020