Add functions to get and set the current channel for the network interface

This commit is contained in:
Yaro Kasear 2025-04-30 15:03:42 -05:00
parent 2586d5fcca
commit d0d7dd473f

View file

@ -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)")