Sep-19-2020, 06:01 PM
(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