[英]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.