首页 > 解决方案 > Pyshark 将数据导出到 CSV

问题描述

大家好,我正在使用 pyshark 捕获数据并导出数据,所以我有一个名为“final_traces.pcap”的网络捕获,我可以检查文件,甚至可以在捕获的帮助下打印出数据名为“PyShark”的库,但我无法以csv格式导出数据。我附上下面的代码片段。

>>> import pyshark
>>> cap = pyshark.FileCapture('./final_traces.pcap')
>>> print cap [0]
Packet (Length: 62)
Layer ETH:
    Destination: 00:0d:48:31:c4:fe
    .... ..0. .... .... .... .... = LG bit: Globally unique address (factory default)
    Address: 00:0d:48:31:c4:fe
    .... ...0 .... .... .... .... = IG bit: Individual address (unicast)
    Type: IPv4 (0x0800)
    Source: 40:8d:5c:f5:50:9f
    .... ..0. .... .... .... .... = LG bit: Globally unique address (factory default)
    Address: 40:8d:5c:f5:50:9f
    .... ...0 .... .... .... .... = IG bit: Individual address (unicast)
Layer IP:
    Source GeoIP: Unknown
    Destination GeoIP Longitude: 4.899500
    Destination GeoIP Latitude: 52.382401
    ..0. .... = More fragments: Not set
    Time to live: 128
    0100 .... = Version: 4
    .1.. .... = Don't fragment: Set
    Flags: 0x02 (Don't Fragment)
    Differentiated Services Field: 0x00 (DSCP: CS0, ECN: Not-ECT)
    Identification: 0x7a48 (31304)
    Header checksum: 0xee23 [validation disabled]
    .... ..00 = Explicit Congestion Notification: Not ECN-Capable Transport (0)
    .... 0101 = Header Length: 20 bytes (5)
    Destination: 139.63.226.59
    Destination GeoIP Country: Netherlands
    0000 00.. = Differentiated Services Codepoint: Default (0)
    Fragment offset: 0
    0... .... = Reserved bit: Not set
    Total Length: 48
    Source: 192.168.100.56
    Header checksum status: Unverified
    Destination GeoIP AS Number: AS1103 SURFnet bv
    Protocol: TCP (6)
    Destination GeoIP: Netherlands, AS1103 SURFnet bv, Netherlands, AS1103 SURFnet bv, 52.382401, 4.899500
    Destination GeoIP Country: Netherlands
    Destination GeoIP AS Number: AS1103 SURFnet bv
Layer TCP:
    Calculated window size: 8192
    000. .... .... = Reserved: Not set
    .... ..0. .... = Urgent: Not set
    Acknowledgment number: 0
    TCP Option - No-Operation (NOP)
    Stream index: 0
    Checksum Status: Unverified
    Sequence number: 0    (relative sequence number)
    MSS Value: 1460
    TCP Segment Len: 0
    Connection establish request (SYN): server port 445
    Length: 4
    0111 .... = Header Length: 28 bytes (7)
    Destination Port: 445
    .... .... 0... = Push: Not set
    ...0 .... .... = Nonce: Not set
    .... ...0 .... = Acknowledgment: Not set
    TCP Option - Maximum segment size: 1460 bytes
    Expert Info (Chat/Sequence): Connection establish request (SYN): server port 445
    TCP Flags: \xc2\xb7\xc2\xb7\xc2\xb7\xc2\xb7\xc2\xb7\xc2\xb7\xc2\xb7\xc2\xb7\xc2\xb7\xc2\xb7S\xc2\xb7
    .... .... ...0 = Fin: Not set
    Kind: Maximum Segment Size (2)
    TCP Option - SACK permitted
    Window size value: 8192
    Group: Sequence
    Severity level: Chat
    Options: (8 bytes), Maximum segment size, No-Operation (NOP), No-Operation (NOP), SACK permitted
    Flags: 0x002 (SYN)
    .... .0.. .... = ECN-Echo: Not set
    Source Port: 38770
    Checksum: 0x4aaa [unverified]
    Urgent pointer: 0
    .... .... ..1. = Syn: Set
    .... 0... .... = Congestion Window Reduced (CWR): Not set
    .... .... .0.. = Reset: Not set
    TCP Option - No-Operation (NOP)
    Length: 2
    Kind: No-Operation (1)
    Kind: No-Operation (1)
    Kind: SACK Permitted (4)

我需要有关如何将某些数据以 CSV 格式保存的帮助假设我想将 Lat 和 Long 放入 CSV。

标签: pythoncsvwiresharkpyshark

解决方案


我不确定你会用 python 做什么,但你可以使用cat foo.txt | grep -i "lat" | paste -d " " >> xyz.logbash。


推荐阅读