簡體   English   中英

在Python中使用Scapy解析文件

[英]Parsing a file using Scapy in Python

我想使用Scapy在Python中解析文件,因為我不需要與Scapy一起使用的命令提供的所有信息。

import logging
logging.getLogger("scapy.runtime").setLevel(logging.ERROR)
from scapy.all import *
import sys
from pprint import pprint

with open('myLogFile', 'r+') as f:
        sys.stdout = f
        print(sniff(filter="udp and port 5060",iface="eth0", prn=lambda x: x.show()))

如果打開名為“ myLogFile”的文件,則會得到以下內容:

結果截圖

我的願望是只獲取IP標題的IP“ src”和“ dest”以及每個電車的負載,但我不知道該怎么做,我是Python的新手。

有一個簡單的解決方案,如果f是您要寫入的文件對象:

sniff(filter="udp and port 5060", iface="eth0",
      prn=lambda x: f.write(x.sprintf("%IP.src% %IP.dst% %UDP.payload%\n")))

假設您不想格式化有效負載。 如果它包含二進制數據,則可能要使用Python的repr()進行轉義:

sniff(filter="udp and port 5060", iface="eth0",
      prn=lambda x: f.write(x.sprintf("%IP.src% %IP.dst% ") + repr(str(x[UDP].payload)) + '\n'))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM