'How to support dig's @<ip> syntax in python argparse
The linux dig utility allows you to specify the target DNS server with '@ip' syntax, like dig @8.8.8.8 A www.example.com. How can I can support that same syntax support in Python's argparse module? (Installing dnsutils in my container was adding 50MB to my image size, so I'm trying to put together a small and simple resolver with dnspython that mimics my most commonly used dig features.)
#!/usr/local/bin/python
import dns.resolver
import argparse
parser = argparse.ArgumentParser(description='Small and incomplete python replacement for dig.')
#parser = argparse.ArgumentParser(prefix_chars='-@', description='Small and incomplete python replacement for dig.')
parser.add_argument('query_type', nargs='?', default='A', help='The type of record to resolve')
parser.add_argument('url', type=str,
help='The URL to resolve')
parser.add_argument('@', dest=dns,
help='The DNS server to query.')
if __name__ == "__main__":
resolver = dns.resolver.Resolver()
args = parser.parse_args()
if args.dns:
print(f'DNS is {args.dns}')
resolver.nameservers=[args.dns]
if args.query_type:
print(f'query_type is {args.query_type}')
if args.url:
print(f'url is {args.url}')
records = resolver.resolve('_imaps._tcp.gmail.com', args.query_type)
for record in records:
print(record)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
