在 Python 中使用 Scapy 的数据包计数
Packet Count Using Scapy in Python
我正在使用以下方法从特定 IP 地址捕获 ICMP 数据包:
import scapy.all from *
sniff(filter="icmp and src host 192.168.100.3 and dst host 192.168.100.2", prn=lambda x: x.sprintf("%IP.proto% packets from %IP.src% to %IP.dst%"))
我得到以下输出:
icmp packets from 192.168.100.3 to 192.168.100.2
icmp packets from 192.168.100.3 to 192.168.100.2
icmp packets from 192.168.100.3 to 192.168.100.2
icmp packets from 192.168.100.3 to 192.168.100.2
icmp packets from 192.168.100.3 to 192.168.100.2
...
但是我只想显示抓包总数
实时即
Total icmp packets from 192.168.100.3 to 192.168.100.2 is <total_packets>
上面的这一行也应该定期更新,即我的输出中始终只需要这一行。请指导。
首先,你需要修改这一行:
sniff(filter="icmp and src host 192.168.100.3 and dst host 192.168.100.2", prn=lambda x: x.sprintf("%IP.proto% packets from %IP.src% to %IP.dst%"))
lambda x: x.sprintf("%IP.proto% packets from %IP.src% to %IP.dst%"
函数针对每个请求运行
所以像这样:
import os
counter = 0
def deal_with_packets(x):
global counter
os.system("cls")
ips = x.sprintf("Total icmp packets from %IP.src% to %IP.dst%")
counter += 1
print(ips + "is" + str(counter))
sniff(filter="icmp and src host 192.168.100.3 and dst host 192.168.100.2", prn=lambda x: deal_with_packets(x))
在 Windows 上使用 os.system("cls")
,在 Linux 和 macOS
上使用 os.system("clear")
我正在使用以下方法从特定 IP 地址捕获 ICMP 数据包:
import scapy.all from *
sniff(filter="icmp and src host 192.168.100.3 and dst host 192.168.100.2", prn=lambda x: x.sprintf("%IP.proto% packets from %IP.src% to %IP.dst%"))
我得到以下输出:
icmp packets from 192.168.100.3 to 192.168.100.2
icmp packets from 192.168.100.3 to 192.168.100.2
icmp packets from 192.168.100.3 to 192.168.100.2
icmp packets from 192.168.100.3 to 192.168.100.2
icmp packets from 192.168.100.3 to 192.168.100.2
...
但是我只想显示抓包总数 实时即
Total icmp packets from 192.168.100.3 to 192.168.100.2 is <total_packets>
上面的这一行也应该定期更新,即我的输出中始终只需要这一行。请指导。
首先,你需要修改这一行:
sniff(filter="icmp and src host 192.168.100.3 and dst host 192.168.100.2", prn=lambda x: x.sprintf("%IP.proto% packets from %IP.src% to %IP.dst%"))
lambda x: x.sprintf("%IP.proto% packets from %IP.src% to %IP.dst%"
函数针对每个请求运行
所以像这样:
import os
counter = 0
def deal_with_packets(x):
global counter
os.system("cls")
ips = x.sprintf("Total icmp packets from %IP.src% to %IP.dst%")
counter += 1
print(ips + "is" + str(counter))
sniff(filter="icmp and src host 192.168.100.3 and dst host 192.168.100.2", prn=lambda x: deal_with_packets(x))
在 Windows 上使用 os.system("cls")
,在 Linux 和 macOS
os.system("clear")