Skip to content

Instantly share code, notes, and snippets.

@albertofwb
Created April 22, 2023 10:05
Show Gist options
  • Save albertofwb/f597d1205f28ec2c07560a47f9491db9 to your computer and use it in GitHub Desktop.
Save albertofwb/f597d1205f28ec2c07560a47f9491db9 to your computer and use it in GitHub Desktop.
implement dig command with python
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
import sys
import dns.resolver
def check_dns(domain_name: str, dns_server: str):
# Create a DNS resolver instance and set the nameserver to the DNS server you want to query
resolver = dns.resolver.Resolver(configure=False)
resolver.nameservers = [dns_server]
# Use the resolver to perform a DNS lookup for the domain's IP address
response = resolver.query(domain_name)
# Print out the IP addresses associated with the domain name
for answer in response:
print(f'{dns_server} reply: {domain_name} is {answer}.')
def main():
if len(sys.argv) < 3:
print("Usage: script.py domain @dns_server")
return
domain_name = sys.argv[1]
dns_server = sys.argv[2]
if dns_server.startswith('@'):
dns_server = dns_server[1:]
check_dns(domain_name, dns_server)
else:
print("Error: DNS server argument should start with '@'")
return
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment