简体   繁体   English

Scapy 提取 IP 地址,不重复

[英]Scapy extract IP address with no repetitions

I am trying to extract the destination IP address then save this in a dictionary but i only want it once我正在尝试提取目标 IP 地址,然后将其保存在字典中,但我只想要一次

Input输入

from scapy.all import *

pkts = rdpcap('example.pcap')

test = ""
for pkt in pkts:
    temp = pkt.sprintf("%IP.dst%",)
    test = test + temp

print(test)


Currently my output is like this目前我的输出是这样的

??,????,????,????,??0.0.0.0,255.255.255.255192.168.1.1,192.168.1.2380.0.0.0,255.255.255.255192.168.1.1,192.168.1.238192.168.1.1,192.168.1.2380.0.0.0,255.255.255.255192.168.1.1,192.168.1.238??,????,????,????,????,??192.168.1.238,192.168.1.1192.168.1.1,192.168.1.238192.168.1.238,89.30.121.15089.30.121.150,192.168.1.238192.168.1.238,89.30.121.150192.168.1.238,89.30.121.15089.30.121.150,192.168.1.238192.168.1.238,89.30.121.150192.16

What I want is the output to look like this and I want the destination IP address only with no repeats我想要的是输出看起来像这样,我只想要目标 IP 地址没有重复

89.30.121.150
198.50.110.244
89.30.121.14
89.30.121.23

What I do get in the output is a massive list of IP addresses instead I only want the destination IP address but only ONCE(no repetitions) not for each packet我在输出中得到的是大量 IP 地址列表,而不是我只想要目标 IP 地址,但只需要一次(无重复)而不是每个数据包

I have also tried this but this freezes?我也试过这个,但是这冻结了?

def print_summary(pkt):
    if IP in pkt:
        ip_dst=pkt[IP].dst
    print(ip_dst)

sniff(offline=pkts, filter="ip",prn=print_summary)

Can anyone think of a quicker solution to extract IP addresses from larger PCAP files using Scapy谁能想到使用 Scapy 从较大的 PCAP 文件中提取 IP 地址的更快解决方案

If you want to put in dictionary you should use dictionary.如果你想放入字典,你应该使用字典。

You getting repeated value because you are not saving it into python dict.你得到重复的值,因为你没有将它保存到 python dict 中。

Here is one way to keep src-->dest ip without repeating by modifying your code:这是一种通过修改代码来保持src-->dest ip 而不重复的方法:

from scapy.all import *

pkts = rdpcap('example.pcap')

dic = {}
for pkt in pkts:
    temp = pkt.sprintf("%IP.dst%")
    dic[temp] = 1

for ip in dic.keys():
    print(ip)

output:输出:

192.168.1.1
192.37.115.0
192.168.1.2
212.242.33.35
192.168.1.251
147.137.21.94
147.137.21.122
147.234.1.253

One of the fastest method is:最快的方法之一是:

from scapy.all import *

IP.payload_guess = []

ips = set(p[IP].dst for p in PcapReader('example.pcap') if IP in p)

for ip in ips:
    print(ip)

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

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