From d0d7dd473f4fd1db5fa6f9befc4448f6e2cf1bd1 Mon Sep 17 00:00:00 2001 From: Yaro Kasear Date: Wed, 30 Apr 2025 15:03:42 -0500 Subject: [PATCH] Add functions to get and set the current channel for the network interface --- listener.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/listener.py b/listener.py index 1e436be..5a2f3c8 100755 --- a/listener.py +++ b/listener.py @@ -6,6 +6,24 @@ from collections import defaultdict from scapy.all import sniff, Dot11, RadioTap from dotenv import load_dotenv from pathlib import Path +import subprocess + +def get_current_channel(interface): + try: + output = subprocess.check_output(["iw", "dev", interface, "info"]).decode() + for line in output.splitlines(): + if "channel" in line.lower(): + return int(line.split()[1]) + except Exception as e: + print(f"[!] Failed to determine current channel for {interface}: {e}") + return None + +def set_monitor_channel(interface, channel): + try: + subprocess.check_call(["iw", "dev", interface, "set", "channel", str(channel)]) + print(f"[+] Set {interface} to channel {channel}") + except subprocess.CalledProcessError as e: + print(f"[!] Failed to set {interface} to channel {channel}: {e}") # --- Settings.env auto-detection logic --- SCRIPT_DIRECTORY = Path(__file__).resolve().parent @@ -82,6 +100,16 @@ def stop_sniff(signum, frame): # === Main === if __name__ == "__main__": + test_interface = os.getenv("INTERFACE", "wlan0") + monitor_interface = os.getenv("LISTEN_INTERFACE", "wlan0") + + current_channel = get_current_channel(test_interface) + if current_channel: + set_monitor_channel(monitor_interface, current_channel) + else: + print("[!] Unable to determine the current channel. Exiting.") + exit(1) + signal.signal(signal.SIGINT, stop_sniff) print(f"[+] Listening on interface {LISTEN_INTERFACE} (press Ctrl+C to stop)")