You can run shell commands like dig from within a Python script using the subprocess module.
import subprocess
def dig_query(domain):
result = subprocess.run(['dig', domain, '+short'], stdout=subprocess.PIPE)
return result.stdout.decode('utf-8')
domain = 'example.com'
output = dig_query(domain)
print(output)
Rather than using subprocess, you can use Python libraries like dnspython to perform DNS queries directly:
import dns.resolver
def query_dns(domain):
result = dns.resolver.resolve(domain, 'A')
for ipval in result:
print('IP:', ipval.to_text())
query_dns('example.com')