I am capturing the ICMP packets from a specific IP address using:
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%"))
I am getting the following output:
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
...
However, I only want to display the total number of packets captured in real time i.e.
Total icmp packets from 192.168.100.3 to 192.168.100.2 is <total_packets>
This above line should also be updated regularly i.e. I only want this line at all times in my output. Please guide.
First of all, you need to modify this line:
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%"))
The lambda x: x.sprintf("%IP.proto% packets from %IP.src% to %IP.dst%"
function runs for every request
So something like:
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))
Use os.system("cls")
on Windows, os.system("clear")
on Linux and macOS