(20241204) Minor changes.

This commit is contained in:
2024-12-04 10:03:20 +05:30
parent e608ddd7be
commit fe8efddce8
+8 -25
View File
@@ -30,24 +30,20 @@
# To make sibling directories accessible for imports: # To make sibling directories accessible for imports:
# ---
import sys import sys
sys.path.append(".") sys.path.append(".")
sys.path.append("..") sys.path.append("..")
# Utils: # Utils:
# --- from utils_v2.string import json
from utils import json_utils from utils_v2.string import regex
from utils import time_utils from utils_v2.date_time import date_time
from utils import regex_utils
# For networking: # For networking:
# ---
import socket import socket
from scapy.all import * from scapy.all import *
# For running the script from the terminal: # For running the script from the terminal:
# ---
import argparse import argparse
@@ -81,24 +77,21 @@ import argparse
def get_name_and_addr(destination): def get_name_and_addr(destination):
# If the user provided the IP address: # If the user provided the IP address:
# ---
if ( if (
regex_utils.match(destination, regex_utils.REGEX_IPV4) or regex.match(destination, regex.REGEX_IPV4) or
regex_utils.match(destination, regex_utils.REGEX_IPV6) regex.match(destination, regex.REGEX_IPV6)
): ):
try: destination_name = socket.gethostbyaddr(destination)[0] try: destination_name = socket.gethostbyaddr(destination)[0]
except Exception as exception: destination_name = "*" except Exception as exception: destination_name = "*"
destination_ip = destination destination_ip = destination
# If the provided destination was the domain name: # If the provided destination was the domain name:
# ---
else: else:
destination_name = destination destination_name = destination
try: destination_ip = socket.gethostbyname(destination) try: destination_ip = socket.gethostbyname(destination)
except Exception as exception: destination_ip = "*" except Exception as exception: destination_ip = "*"
# Done here: # Done here:
# ---
return destination_name, destination_ip return destination_name, destination_ip
@@ -113,17 +106,14 @@ def tracert(
): ):
# Initialize the variables: # Initialize the variables:
# ---
destination_name, destination_ip = get_name_and_addr(destination) destination_name, destination_ip = get_name_and_addr(destination)
full_trace = [] full_trace = []
ttl = 1 ttl = 1
# Keep noting hops till the limit is reached: # Keep noting hops till the limit is reached:
# ---
while ttl <= max_hops: while ttl <= max_hops:
# Create a JSON for this stage: # Create a JSON for this stage:
# ---
this_hop = { this_hop = {
"destAddr": destination_ip, "destAddr": destination_ip,
"destName": destination_name, "destName": destination_name,
@@ -135,22 +125,18 @@ def tracert(
} }
# Create the IP and UDP headers and combine them: # Create the IP and UDP headers and combine them:
# ---
ip_packet = IP(dst = destination, ttl = ttl) ip_packet = IP(dst = destination, ttl = ttl)
udp_packet = UDP(dport = port) udp_packet = UDP(dport = port)
trace_packet = ip_packet / udp_packet trace_packet = ip_packet / udp_packet
# Send the packet and receive a reply and note down the timestamp: # Send the packet and receive a reply and note down the timestamp:
# ---
reply = sr1(trace_packet, timeout = timeout, verbose = 0) reply = sr1(trace_packet, timeout = timeout, verbose = 0)
this_hop["ts"] = time_utils.get_current_utc_datetime(as_string = True) this_hop["ts"] = date_time.get_current_utc_date_time(as_string = True)
# No response: # No response:
# ---
if reply is None: this_hop["hopAddr"] = this_hop["hopName"] = "*" if reply is None: this_hop["hopAddr"] = this_hop["hopName"] = "*"
# If some response was received, we note the values and break out if this was the destination hop: # If some response was received, we note the values and break out if this was the destination hop:
# ---
else: else:
this_hop["hopName"], this_hop["hopAddr"] = get_name_and_addr(f"{reply.src}") this_hop["hopName"], this_hop["hopAddr"] = get_name_and_addr(f"{reply.src}")
if reply.type == 3: if reply.type == 3:
@@ -159,12 +145,10 @@ def tracert(
break break
# Carry on to the next hop: # Carry on to the next hop:
# ---
full_trace.append(this_hop) full_trace.append(this_hop)
ttl += 1 ttl += 1
# Done here: # Done here:
# ---
return full_trace return full_trace
@@ -203,7 +187,7 @@ if __name__ == "__main__":
"--port", "--port",
type = int, type = int,
default = 33434, default = 33434,
help = "Timeout for each packet in seconds (default: 33434)." help = "The port to probe at the destination (default: 33434)."
) )
args = parser.parse_args() args = parser.parse_args()
@@ -214,5 +198,4 @@ if __name__ == "__main__":
timeout = args.timeout, timeout = args.timeout,
port = args.port port = args.port
) )
print("TRACE:") print("TRACE:", json.to_string(trace))
print(json_utils.to_json_string(trace))