def dumpRR(u): name, type, klass, ttl, rdlength = u.getRRheader() typename = dnstype.typestr(type) print 'name=%s, type=%d(%s), class=%d(%s), ttl=%d' \ % (name, type, typename, klass, dnsclass.classstr(klass), ttl) mname = 'get%sdata' % typename if hasattr(u, mname): print ' formatted rdata:', getattr(u, mname)() else: print ' binary rdata:', u.getbytes(rdlength)
def test(): import sys import getopt import socket protocol = 'udp' server = 'meermin.cwi.nl' # XXX adapt this to your local port = 53 opcode = dnsopcode.QUERY rd = 0 qtype = dnstype.MX qname = 'cwi.nl' try: opts, args = getopt.getopt(sys.argv[1:], 'Trs:tu') if len(args) > 2: raise getopt.error, 'too many arguments' except getopt.error, msg: print msg print 'Usage: python dnslib.py', print '[-T] [-r] [-s server] [-t] [-u]', print '[qtype [qname]]' print '-T: run testpacker() and exit' print '-r: recursion desired (default not)' print '-s server: use server (default %s)' % server print '-t: use TCP protocol' print '-u: use UDP protocol (default)' print 'qtype: query type (default %s)' % \ dnstype.typestr(qtype) print 'qname: query name (default %s)' % qname print 'Recognized qtype values:' qtypes = dnstype.typemap.keys() qtypes.sort() n = 0 for qtype in qtypes: n = n + 1 if n >= 8: n = 1 print print '%s = %d' % (dnstype.typemap[qtype], qtype), print sys.exit(2)
def test(): import sys import getopt import socket protocol = 'udp' server = 'cnri.reston.va.us' # XXX adapt this to your local port = 53 opcode = dnsopcode.QUERY rd = 0 qtype = dnstype.MX qname = 'cwi.nl' try: opts, args = getopt.getopt(sys.argv[1:], 'Trs:tu') if len(args) > 2: raise getopt.error, 'too many arguments' except getopt.error, msg: print msg print 'Usage: python dnslib.py', print '[-T] [-r] [-s server] [-t] [-u]', print '[qtype [qname]]' print '-T: run testpacker() and exit' print '-r: recursion desired (default not)' print '-s server: use server (default %s)' % server print '-t: use TCP protocol' print '-u: use UDP protocol (default)' print 'qtype: query type (default %s)' % \ dnstype.typestr(qtype) print 'qname: query name (default %s)' % qname print 'Recognized qtype values:' qtypes = dnstype.typemap.keys() qtypes.sort() n = 0 for qtype in qtypes: n = n+1 if n >= 8: n = 1; print print '%s = %d' % (dnstype.typemap[qtype], qtype), print sys.exit(2)
def dumpQ(u): qname, qtype, qclass = u.getQuestion() print 'qname=%s, qtype=%d(%s), qclass=%d(%s)' \ % (qname, qtype, dnstype.typestr(qtype), qclass, dnsclass.classstr(qclass))
if o == '-t': protocol = 'tcp' if o == '-u': protocol = 'udp' if o == '-s': server = a if o == '-r': rd = 1 if args[0:]: try: qtype = eval(string.upper(args[0]), dnstype.__dict__) except (NameError, SyntaxError): print 'bad query type:', `args[0]` sys.exit(2) if args[1:]: qname = args[1] if qtype == dnstype.AXFR: print 'Query type AXFR, protocol forced to TCP' protocol = 'tcp' print 'QTYPE %d(%s)' % (qtype, dnstype.typestr(qtype)) m = Mpacker() m.addHeader(0, 0, opcode, 0, 0, rd, 0, 0, 0, 1, 0, 0, 0) m.addQuestion(qname, qtype, dnsclass.IN) request = m.getbuf() if protocol == 'udp': s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) s.connect((server, port)) s.send(request) reply = s.recv(1024) else: s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect((server, port)) s.send(pack16bit(len(request)) + request)