用Python解析pcap数据包中的五元组信息
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
try:
import scapy.all as scapy
except ImportError:
import scapy
try:
# This import works from the project directory
import scapy_http.http
except ImportError:
# If you installed this package via pip, you just need to execute this
from scapy.layers import http
packets = scapy.rdpcap('test.pcap')
f =open("result.txt","w+")
# print >> f,name
for p in packets:
try:
dic = {}
dic["Protocol"] = p[1].proto
dic["Destination"] = p[1].dst
dic["Source"] = p[1].src
dic["Sport"] = p[1].sport
dic["Dport"] = p[1].dport
print >> f, dic
# print >> f,p[1].proto, p[1].dst, p[1].src, p[2].sport, p[2].dport
except AttributeError:
continue
# f.close()
p.show()