Code for Crafting Dummy Packets with Scapy Using Python Tutorial


View on Github

network_latency_measure.py

server_ips = ["192.168.27.1", "192.168.17.129", "192.168.17.128"]

from scapy.all import IP, ICMP, sr1
import time

def check_latency(ip):
    packet = IP(dst=ip) / ICMP()
    start_time = time.time()
    response = sr1(packet, timeout=2, verbose=0)
    end_time = time.time()
    
    if response:
        latency = (end_time - start_time) * 1000  # Convert to milliseconds
        print(f"[+] Latency to {ip}: {latency:.2f} ms")
    else:
        print(f"[-] No response from {ip} (possible packet loss)")

for server_ip in server_ips:
    check_latency(server_ip)

   

packet_craft.py

# Uncomment them and run according to the tutorial
#from scapy.all import IP, TCP, send, UDP

# # Step 1: Creating a simple IP packet
# packet = IP(dst="192.168.1.1")  # Setting the destination IP
# packet = IP(dst="192.168.1.1") / TCP(dport=80, sport=12345, flags="S")
# print(packet.show())  # Display packet details
# send(packet)


############
# from scapy.all import ICMP

# # Creating an ICMP Echo request packet
# icmp_packet = IP(dst="192.168.1.1") / ICMP()
# send(icmp_packet)


############
# from scapy.all import UDP

# # Creating a UDP packet
# udp_packet = IP(dst="192.168.1.1") / UDP(dport=53, sport=12345)
# send(udp_packet)



###########
# blocked_packet = IP(dst="192.168.1.1") / TCP(dport=80, flags="S")
# send(blocked_packet)

# allowed_packet = IP(dst="192.168.1.1") / UDP(dport=53)
# send(allowed_packet)