2017-01-01 142 views
0

在獲取數據包的DNS信息時,我在使用pyshark時遇到了一些問題。我使用python 3打印DNS信息時發生Pyshark屬性錯誤

我的代碼如下所示:

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) 

和錯誤看起來是這樣的:

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() 

我甚至不打印有關數據包的任何信息,只錯誤。

回答

0

您可以試試把它包/除非忽略所有的非DNS數據包

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