Source

PcapAnalyzeTest / statistics.py

import glob
import os

pcapList = glob.glob("tmp/*.pcap")
loopCount = 0

for pcapFile in pcapList:
    tcpPortDict = dict()
    udpPortDict = dict()
    tcpPorts = os.popen("tshark -nn -r %s -T fields -e tcp.dstport (tcp)" %
            (pcapFile)).read().strip().split("\n")
    for port in tcpPorts:
        tcpPortDict.setdefault(int(port),0)
        tcpPortDict[int(port)] += 1
    udpPorts = os.popen("tshark -nn -r %s -T fields -e udp.dstport (udp)" %
            (pcapFile)).read().strip().split("\n")
    for port in udpPorts:
        udpPortDict.setdefault(int(port),0)
        udpPortDict[int(port)] += 1

    w = open("%d.tcp.txt" % loopCount,"w")
    for key,value in tcpPortDict.iteritems():
        w.write("%d,%d\n" % (key,value))
    w.close()

    w = open("%d.udp.txt" % loopCount,"w")
    for key,value in udpPortDict.iteritems():
        w.write("%d,%d\n" % (key,value))
    w.close()
    loopCount += 1