-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cc177ef
commit 7df39dd
Showing
10 changed files
with
893 additions
and
139 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,202 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 14, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"Available interfaces:\n", | ||
"\\Device\\NPF_{02741F9E-884C-4A62-BE45-423AFA520097}\n", | ||
"\\Device\\NPF_{9AC1E72B-7F72-49E7-850C-4F18896F94CA}\n", | ||
"\\Device\\NPF_{0D10BCB1-6E31-4BB9-AA69-682E92525E9B}\n", | ||
"\\Device\\NPF_{88214AF4-0292-4167-A00F-E0990C869E72}\n", | ||
"\\Device\\NPF_{3C9FD1E2-417E-4DA9-9612-8AC735F01823}\n", | ||
"\\Device\\NPF_{A5707401-5579-44A5-AD24-63A3DCCEF346}\n", | ||
"\\Device\\NPF_{684E3EFC-41CB-4FF3-B971-7466DB7D2532}\n", | ||
"\\Device\\NPF_{C8A62220-B9A0-4ACF-A8D2-E9EC414D3743}\n", | ||
"\\Device\\NPF_{E960F3D2-72F5-4DF1-8282-340C0EEB528A}\n", | ||
"\\Device\\NPF_{37217669-42DA-4657-A55B-0D995D328250}\n", | ||
"\\Device\\NPF_Loopback\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"from scapy.all import Ether, sendp, get_if_list\n", | ||
"import time\n", | ||
"\n", | ||
"# List available network interfaces\n", | ||
"interfaces = get_if_list()\n", | ||
"print(\"Available interfaces:\")\n", | ||
"for iface in interfaces:\n", | ||
" print(iface)\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 17, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# Define the Ethernet frame\n", | ||
"eth_frame = Ether(dst=\"12:34:ff:ff:ff:ff\", type=0x8001)\n", | ||
"\n", | ||
"# Send the packet on specified interface (replace 'YOUR_INTERFACE' with the actual interface name from the list above)\n", | ||
"#sendp(eth_frame, iface='Ethernet 6', verbose=False)\n", | ||
"\n", | ||
"\n", | ||
"start_time = time.time()\n", | ||
"for _ in range(1000000): # Adjust the range based on the desired duration\n", | ||
" sendp(eth_frame, iface='Ethernet 6', verbose=False)\n", | ||
" if time.time() - start_time >= 30: # Run for 10 seconds\n", | ||
" break" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"from scapy.all import *\n", | ||
"import time\n", | ||
"\n", | ||
"# Packet configuration\n", | ||
"packet = IP(dst=\"target.ip\")/UDP(dport=12345)\n", | ||
"\n", | ||
"# Start sending packets\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 55, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"import serial\n", | ||
"import numpy as np\n", | ||
"import time\n", | ||
"import re\n", | ||
"\n", | ||
"def send_command_and_read(serial_port, frame_count, delay_time):\n", | ||
" # Build the command to send to the serial port\n", | ||
" command = f\"{frame_count} {delay_time}\"\n", | ||
" serial_port.write(command.encode('utf-8')) # Send command to the serial port\n", | ||
" #time.sleep(delay_time / 1_000_000) # Convert microseconds to seconds for the delay\n", | ||
"\n", | ||
"\n", | ||
" # Regular expression to capture frame number and timing\n", | ||
" pattern = re.compile(r'Frame\\[(\\d+)\\]: (\\d+) us')\n", | ||
"\n", | ||
" # Read data from the serial port\n", | ||
" data = []\n", | ||
" last_time = time.time() # Store the time of the last read\n", | ||
" while True:\n", | ||
" if serial_port.in_waiting > 0:\n", | ||
" response = serial_port.readline().decode('utf-8').strip() # Read a line from the serial port\n", | ||
" match = pattern.match(response) # Match the response against the regex pattern\n", | ||
" if match:\n", | ||
" frame_number = int(match.group(1)) # Extract the frame number\n", | ||
" timing_value = int(match.group(2)) # Extract the timing in microseconds\n", | ||
" data.append((frame_number, timing_value)) # Append as a tuple\n", | ||
" last_time = time.time() # Update last read time\n", | ||
" # Check if 1 second has passed since the last read\n", | ||
" if time.time() - last_time > 2:\n", | ||
" break # Exit the loop if no new messages for 1 second\n", | ||
"\n", | ||
" return np.array(data) # Convert the list of tuples into a numpy array\n", | ||
"\n", | ||
"def run():\n", | ||
" # User input in the specified format\n", | ||
" #user_input = input(\"Enter frameCount and delayTimeMicroseconds: \")\n", | ||
" #frame_count, delay_time = map(int, user_input.split())\n", | ||
" frame_count, delay_time = (5000, 50)\n", | ||
" # Set up the serial port (adjust COM port and baud rate as needed)\n", | ||
" with serial.Serial('COM6', 115200, timeout=1) as ser:\n", | ||
" time.sleep(2) # Allow time for the serial connection to initialize\n", | ||
" data_array = send_command_and_read(ser, frame_count, delay_time)\n", | ||
" return data_array\n", | ||
"\n", | ||
"data = run()" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 56, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"array([[ 0, 53],\n", | ||
" [ 1, 52],\n", | ||
" [ 2, 53],\n", | ||
" ...,\n", | ||
" [1349, 52],\n", | ||
" [1350, 52],\n", | ||
" [1351, 53]])" | ||
] | ||
}, | ||
"execution_count": 56, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"data" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 59, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"3900000.0000000005" | ||
] | ||
}, | ||
"execution_count": 59, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"US_TO_MS = (10**-6)\n", | ||
"78/(US_TO_MS*20)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "ethernet", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.12.7" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import time | ||
import serial | ||
import subprocess | ||
import glob | ||
import os | ||
from scapy.all import rdpcap, Ether | ||
|
||
# Configuration | ||
serial_port = "COM6" # Update to your Windows serial port | ||
baud_rate = 115200 | ||
command = "10 1000\n" # Example command to send | ||
filter_ethertype = 0x8001 # Updated to the specified custom EtherType | ||
capture_interface = "6" # Update to the desired Npcap interface | ||
|
||
# Connect to Serial Port | ||
def send_command_to_serial(port, baudrate, cmd): | ||
with serial.Serial(port, baudrate, timeout=1) as ser: | ||
print(f"Sending command: {cmd.strip()}") | ||
ser.write(cmd.encode()) | ||
time.sleep(2) # Wait for the command to be processed | ||
|
||
# Capture traffic using tshark | ||
def capture_traffic(output_file, interface): | ||
print(f"Capturing traffic on interface {interface} to {output_file}...") | ||
# Use tshark to capture packets; this will also overwrite the existing file | ||
tshark_cmd = f"tshark -i {interface} -w {output_file} -F pcap" # -F specifies the output format | ||
process = subprocess.Popen(tshark_cmd, shell=False) | ||
return process | ||
|
||
# Get the most recent PCAP file created | ||
def get_latest_pcap_file(directory): | ||
list_of_files = glob.glob(os.path.join(directory, '*.pcap')) | ||
if not list_of_files: | ||
return None | ||
latest_file = max(list_of_files, key=os.path.getmtime) | ||
return latest_file | ||
|
||
# Read PCAP file and filter packets with the required EtherType | ||
def read_pcap_and_filter(pcap_file, ether_type): | ||
packets_info = [] | ||
packets = rdpcap(pcap_file) | ||
|
||
for index, packet in enumerate(packets): | ||
if Ether in packet and packet[Ether].type == ether_type: | ||
timestamp = packet.time | ||
packets_info.append((index, timestamp)) | ||
|
||
return packets_info | ||
|
||
def main(): | ||
# Start capturing packets | ||
capture_file_pattern = "capture.pcap" # Name of the PCAP file | ||
tshark_process = capture_traffic(capture_file_pattern, capture_interface) | ||
|
||
try: | ||
# Allow a moment for capture process to initialize | ||
time.sleep(2) | ||
|
||
# Send command to the Ethernet device | ||
send_command_to_serial(serial_port, baud_rate, command) | ||
|
||
# Monitor for a while to ensure all packets are captured | ||
print("Capture started. Waiting for packets...") | ||
time.sleep(10) # Adjust timing to ensure enough time for packet arrival | ||
|
||
finally: | ||
# Get the PID of the running process to kill it using taskkill | ||
pid = tshark_process.pid | ||
print(f"Terminating tshark process with PID: {pid}") | ||
|
||
# Forcibly stop the packet capture using taskkill | ||
try: | ||
subprocess.call(f"taskkill /F /PID {pid}", shell=True) | ||
except Exception as e: | ||
print(f"An error occurred while trying to kill the process: {e}") | ||
|
||
# Get the latest PCAP file | ||
pcap_file = get_latest_pcap_file(os.getcwd()) | ||
if pcap_file: | ||
print(f"Latest PCAP file: {pcap_file}") | ||
|
||
# Read and filter the PCAP file | ||
packets_data = read_pcap_and_filter(pcap_file, filter_ethertype) | ||
|
||
# Print the results | ||
print("Captured packets:") | ||
for packet_number, timestamp in packets_data: | ||
print(f"Packet Number: {packet_number}, Timestamp: {timestamp:.6f}") # Display the timestamp with microseconds | ||
|
||
else: | ||
print("No PCAP files found.") | ||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.