Skip to content

Commit dc4e534

Browse files
Uncommitted changes before Checkout at 03/01/2025 21:42 [Changes]
Signed-off-by: Shahm Najeeb <Nirt_12023@outlook.com>
1 parent e70036a commit dc4e534

File tree

1 file changed

+29
-25
lines changed

1 file changed

+29
-25
lines changed

CODE/packet_sniffer.py

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,15 @@
1313
if __name__ == "__main__":
1414
log = Log({"log_level": DEBUG})
1515

16-
try:
17-
# Read configuration from config.ini
18-
config = ConfigParser()
19-
config.read('config.ini')
20-
config = config['PacketSniffer Settings']
16+
# Read configuration from config.ini
17+
config = ConfigParser()
18+
config.read('config.ini')
19+
config = config['PacketSniffer Settings']
2120

22-
# Global configuration
23-
conf.verb = 0 # Turn off verbosity for clean output
24-
packet_data = [] # List to store packet information
25-
G = nx.Graph() # Initialize a graph
26-
except Exception as e:
27-
log.error(f"Error reading configuration: {e}")
28-
exit(1)
21+
# Global configuration
22+
conf.verb = 0 # Turn off verbosity for clean output
23+
packet_data = [] # List to store packet information
24+
G = nx.Graph() # Initialize a graph
2925

3026

3127
# Function to process and log packet details
@@ -241,6 +237,14 @@ def packet_sniffer():
241237
Starts packet sniffing on the specified interface.
242238
Handles exceptions related to invalid interface names and attempts to correct them.
243239
"""
240+
241+
def correct_interface_name(interface_name: str) -> str:
242+
corrections = {
243+
"WiFi": "Wi-Fi",
244+
"Wi-Fi": "WiFi"
245+
}
246+
return corrections.get(interface_name, interface_name)
247+
244248
interface = config['interface']
245249
packet_count = int(config['packet_count'])
246250
timeout = int(config['timeout'])
@@ -256,18 +260,16 @@ def packet_sniffer():
256260
log.error("Error reading configuration: Improper values for packet count or timeout")
257261
exit(1)
258262

259-
try:
260-
start_sniffing(interface, packet_count, timeout)
261-
except Exception as err:
262-
log.error(f"Invalid interface '{interface}'. Please check the configuration: {err}")
263-
if interface == "WiFi" or interface == "Wi-Fi":
264-
log.warning("Attempting to correct the interface name...")
265-
interface = "Wi-Fi" if interface == "WiFi" else "WiFi"
266-
log.info(f"Interface name auto-corrected to '{interface}', retrying packet sniffing...")
267-
try:
268-
start_sniffing(interface, packet_count, timeout)
269-
except Exception as err:
270-
log.error(f"Error sniffing packets on auto-corrected interface '{interface}': {err}")
263+
for attempt in range(2): # Try original and corrected name
264+
try:
265+
start_sniffing(interface, packet_count, timeout)
266+
break
267+
except Exception as err:
268+
if attempt == 0 and interface in ("WiFi", "Wi-Fi"):
269+
log.warning(f"Retrying with corrected interface name...")
270+
interface = correct_interface_name(interface)
271+
else:
272+
log.error(f"Failed to sniff packets: {err}")
271273

272274

273275
# Entry point of the script
@@ -276,4 +278,6 @@ def packet_sniffer():
276278
packet_sniffer()
277279
except Exception as e:
278280
log.error(e)
279-
exit(1)
281+
finally:
282+
if G:
283+
plt.close()

0 commit comments

Comments
 (0)