简体   繁体   English

在csv文件python中打印输出

[英]print output in csv file python

My code that is used to print the output of the pcap file IP's IN CSV FILE is working well but the issue is it is storing only 1st packet of the pcap file. 我用于打印pcap文件IP的IN CSV FILE输出的代码运行良好,但问题是它只存储了pcap文件的第一个数据包。 I am not getting where is the actual issue is.. can someone help me to solve this then please. 我没有得到实际问题的位置..有人可以帮助我解决这个问题。

Here is my code: 这是我的代码:

import dpkt
from dpkt.ip import IP
from dpkt.ethernet import Ethernet
import struct
import socket
import csv

def ip_to_str(address):
    return socket.inet_ntoa(address)

f = open('sample.pcap', 'rb')
pcap = dpkt.pcap.Reader(f)
for ts, buf in pcap:
    eth = dpkt.ethernet.Ethernet(buf)
    if eth.type != dpkt.ethernet.ETH_TYPE_IP:
        continue

    ip = eth.data
    do_not_fragment = bool(dpkt.ip.IP_DF)
    more_fragments = bool(dpkt.ip.IP_MF)
    fragment_offset = bool(dpkt.ip.IP_OFFMASK)
    c = csv.writer(open("a.csv", "wb"))

    Source = "%s" % ip_to_str(ip.src)
    Destination = "%s" % ip_to_str(ip.dst)
    Length = "%d" % (ip.len)
    TTL = "%d" % (ip.ttl)
    OFF = ip.off
    TOS = ip.tos
    Protocol = ip.p
    data = (Source, Destination, Length, TTL, TOS, OFF, Protocol)
    c.writerow(data)

Your code is currently opening a csv file inside the loop, so each time creating a new version of "a.csv" and only writing one packet to it. 您的代码当前正在循环中打开一个csv文件,因此每次创建一个新版本的“a.csv”并且只写一个数据包。 Move the file creation statement outside of the loop, and keep writing inside the loop. 将文件创建语句移到循环之外,并继续在循环内写入。

import dpkt
from dpkt.ip import IP
from dpkt.ethernet import Ethernet
import struct
import socket
import csv

def ip_to_str(address):
    return socket.inet_ntoa(address)

f = open('sample.pcap', 'rb')
pcap = dpkt.pcap.Reader(f)
c = csv.writer(open("a.csv", "wb"))  # <=== moved here
for ts, buf in pcap:
    eth = dpkt.ethernet.Ethernet(buf)
    if eth.type != dpkt.ethernet.ETH_TYPE_IP:
        continue

    ip = eth.data
    do_not_fragment = bool(dpkt.ip.IP_DF)
    more_fragments = bool(dpkt.ip.IP_MF)
    fragment_offset = bool(dpkt.ip.IP_OFFMASK)

    Source = "%s" % ip_to_str(ip.src)
    Destination = "%s" % ip_to_str(ip.dst)
    Length = "%d" % (ip.len)
    TTL = "%d" % (ip.ttl)
    OFF = ip.off
    TOS = ip.tos
    Protocol = ip.p
    data = (Source, Destination, Length, TTL, TOS, OFF, Protocol)
    c.writerow(data)

You need to make sure you close the file at the end of your loop; 您需要确保在循环结束时关闭文件; and as mentioned indent your code properly: 并且正如提到的那样正确缩进代码:

import struct
import socket
import csv

import dpkt

from dpkt.ip import IP
from dpkt.ethernet import Ethernet

def ip_to_str(address):
    return socket.inet_ntoa(address)

with open('sample.pcap', 'rb') as f:
    pcap = dpkt.pcap.Reader(f)

results = []
for ts, buf in pcap:
    eth = dpkt.ethernet.Ethernet(buf)
    if eth.type != dpkt.ethernet.ETH_TYPE_IP:
        continue

    ip = eth.data
    do_not_fragment = bool(dpkt.ip.IP_DF)
    more_fragments = bool(dpkt.ip.IP_MF)
    fragment_offset = bool(dpkt.ip.IP_OFFMASK)

    data = [ip_to_str(ip.src),
            ip_to_str(ip.dst),
            ip.len,
            ip.ttl,
            ip.off,
            ip.tos,
            ip.p]
    results.append(data)

with open('output.csv', 'wb') as f:
    writer = csv.writer(f, delimiter=',', quotechar='"')
    writer.writerows(results)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM