Python – Simple DNS Lookup Tool

# Author: Kerry Cordero
# Version: 1.0.0
# Description: This script prompts for the IP/FQDN, Record Type, DNS Server to use for lookups, and the Tool to use (NSLOOKUP, PowerShell, or DIG).

import subprocess

def perform_lookup(ip_or_fqdn, record_type, dns_server, lookup_tool):
    if lookup_tool.lower() == 'nslookup':
        process = subprocess.Popen(['nslookup', '-type='+record_type, ip_or_fqdn, dns_server], stdout=subprocess.PIPE)
        output = process.communicate()[0]
        print(output.decode('utf-8'))

    elif lookup_tool.lower() == 'powershell':
        command = f'Resolve-DnsName -Name {ip_or_fqdn} -Type {record_type} -Server {dns_server}'
        process = subprocess.Popen(["powershell", command], stdout=subprocess.PIPE)
        output = process.communicate()[0]
        print(output.decode('utf-8'))

    elif lookup_tool.lower() == 'dig':
        process = subprocess.Popen(['dig', '+short', '-t', record_type, ip_or_fqdn, '@'+dns_server], stdout=subprocess.PIPE)
        output = process.communicate()[0]
        print(output.decode('utf-8'))

    else:
        print(f'Unsupported lookup tool: {lookup_tool}')

def main():
    ip_or_fqdn = input('Enter IP or FQDN: ')
    record_type = input('Enter Record Type: ')
    dns_server = input('Enter the DNS Server to Use for these lookups: ')
    lookup_tool = input('Enter the lookup tool to use (NSLOOKUP, PowerShell, or DIG): ')

    perform_lookup(ip_or_fqdn, record_type, dns_server, lookup_tool)

if __name__ == "__main__":
    main()

Make it pretty:

!Simple DNS tool that prompts for the IP/FQDN, Record Type, DNS Server to use for lookups, and the Tool to use (NSLOOKUP, PowerShell, or DIG).
!By: Kerry Cordero

import subprocess
from termcolor import colored
from pyfiglet import figlet_format

def perform_lookup(ip_or_fqdn, record_type, dns_server, lookup_tool):
    output = None
    if lookup_tool.lower() == 'nslookup':
        process = subprocess.Popen(['nslookup', '-type='+record_type, ip_or_fqdn, dns_server], stdout=subprocess.PIPE)
        output = process.communicate()[0]

    elif lookup_tool.lower() == 'powershell':
        command = f'Resolve-DnsName -Name {ip_or_fqdn} -Type {record_type} -Server {dns_server}'
        process = subprocess.Popen(["powershell", command], stdout=subprocess.PIPE)
        output = process.communicate()[0]

    elif lookup_tool.lower() == 'dig':
        process = subprocess.Popen(['dig', '+short', '-t', record_type, ip_or_fqdn, '@'+dns_server], stdout=subprocess.PIPE)
        output = process.communicate()[0]

    else:
        print(colored(f'Unsupported lookup tool: {lookup_tool}', 'red'))
        return

    print(colored(output.decode('utf-8'), 'green'))

def main():
    print(colored(figlet_format('DNS Lookup Tool', font='slant'), 'cyan'))
    ip_or_fqdn = input('Enter IP or FQDN: ')
    record_type = input('Enter Record Type: ')
    dns_server = input('Enter the DNS Server to Use for these lookups: ')
    lookup_tool = input('Enter the lookup tool to use (NSLOOKUP, PowerShell, or DIG): ')
    print(colored("=======================================", 'yellow'))

    perform_lookup(ip_or_fqdn, record_type, dns_server, lookup_tool)

if __name__ == "__main__":
    main()