Pyshark Attribute Error while printing DNS info

2k views Asked by At

I'm having some trouble with pyshark when acquiring DNS info of a packet. I am using python 3.

My code looks like this:

    import pyshark
    cap = pyshark.LiveCapture(interface="en1")
    cap.sniff(timeout=5)

    def print_dns_info(pkt):
        if pkt.dns.qry_name:
            print 'DNS Request from %s: %s' % (pkt.ip.src, pkt.dns.qry_name)
        elif pkt.dns.resp_name:
            print 'DNS Response from %s: %s' % (pkt.ip.src, pkt.dns.resp_name)
    cap.apply_on_packets(print_dns_info, timeout=100)

And the error looks like this:

Traceback (most recent call last):
  File "<pyshell#6>", line 1, in <module>
    cap.apply_on_packets(print_dns_info, timeout=100)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/pyshark/capture/capture.py", line 201, in apply_on_packets
    return self.eventloop.run_until_complete(coro)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/trollius/base_events.py", line 300, in run_until_complete
    return future.result()
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/trollius/futures.py", line 287, in result
    raise self._exception
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/trollius/tasks.py", line 255, in _step
    result = next(coro)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/trollius/tasks.py", line 424, in wait_for
    raise Return(fut.result())
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/trollius/futures.py", line 287, in result
    raise self._exception
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/trollius/tasks.py", line 251, in _step
    result = coro.throw(exc)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/pyshark/capture/capture.py", line 215, in packets_from_tshark
    packet_count=packet_count))
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/trollius/tasks.py", line 253, in _step
    result = coro.send(value)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/pyshark/capture/capture.py", line 238, in _go_through_packets_from_fd
packet_callback(packet)
  File "<pyshell#5>", line 2, in print_dns_info
    if pkt.dns.qry_name:
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/pyshark/packet/packet.py", line 110, in __getattr__
    raise AttributeError()

I doesn't even print any info about the packets, only the error.

1

There are 1 answers

0
arist On BEST ANSWER

you can wrap it in a try/except to ignore all the non-DNS packets

    try:
        if pkt.dns.qry_name:
            print 'DNS Request from %s: %s' % (pkt.ip.src, pkt.dns.qry_name)
    except AttributeError as e:
        #ignore packets that aren't DNS Request
        pass
    try:
        if pkt.dns.resp_name:
            print 'DNS Response from %s: %s' % (pkt.ip.src, pkt.dns.resp_name)
    except AttributeError as e:
        #ignore packets that aren't DNS Response
        pass