Menu

(Solved) : Given Starting Code Need Write Python3 Program Send Dns Queries Server Parse Replies Need Q36374708 . . .

Given this starting code below, I need to write a Python3program to send DNS queries to a server and parse the replies.I only need to complete #1 ~ 5 part to complete thecode.

Note that there is substantial boilerplate code provided thatalready parses DNS replies, so you need to focus on simply sendinga correctly-formatted DNS query.

** The program must send DNS queries over UDPto a DNS server using the Python3 programminglanguage and the socket module. Warning:I cannot use python built-in implementation of getaddrinfo(). Also,DNSPython, DNSimple, dnslib cannot be used.**

<dns.py starting code>

#!/usr/bin/env python3

# Python DNS query client

#

# Example usage:

# ./dns.py –type=A –name=www.pacific.edu –server=8.8.8.8

# ./dns.py –type=AAAA –name=www.google.com–server=8.8.8.8

# Should provide equivalent results to:

# dig www.pacific.edu A @8.8.8.8 +noedns

# dig www.google.com AAAA @8.8.8.8 +noedns

# (note that the +noedns option is used to disable thepseduo-OPT

# header that dig adds. Our Python DNS client does not need

# to produce that optional, more modern header)

from dns_tools import dns # Custom module for boilerplate code

import argparse

import ctypes

import random

import socket

import struct

import sys

def main():

# Setup configuration

parser = argparse.ArgumentParser(description=’DNS client forECPE 170′)

parser.add_argument(‘–type’, action=’store’, dest=’qtype’,

required=True, help=’Query Type (A or AAAA)’)

parser.add_argument(‘–name’, action=’store’, dest=’qname’,

required=True, help=’Query Name’)

parser.add_argument(‘–server’, action=’store’,dest=’server_ip’,

required=True, help=’DNS Server IP’)

args = parser.parse_args()

qtype = args.qtype

qname = args.qname

server_ip = args.server_ip

port = 53

server_address = (server_ip, port)

if qtype not in (“A”, “AAAA”):

print(“Error: Query Type must be ‘A’ (IPv4) or ‘AAAA'(IPv6)”)

sys.exit()

Questions

1) Create UDP socket

2) Generate DNS request message

3) Send request message to server (Tip: Use sendto()function for UDP

4) Receive message from server (Tip:use recvfrom() function for UDP)

5) Close socket

# Decode DNS message and display to screen

dns.decode_dns(raw_bytes)

if __name__ == “__main__”:

sys.exit(main())

Expert Answer


Answer to Given Starting Code Need Write Python3 Program Send Dns Queries Server Parse Replies Need Q36374708 . . .

OR