import subprocess import time import threading # Function to set up network adapter def setup_adapter(): subprocess.run(["ifconfig", "wlan0", "create", "wlandev", "rtwn0", "wlanmode", "adhoc"]) subprocess.run(["ifconfig", "wlan0", "inet", "192.168.0.1", "netmask", "255.255.255.0"]) subprocess.run(["ifconfig", "wlan0", "up", "scan"]) print("Adapter set up in adhoc and promiscuous mode") # Function to scan for networks def scan_for_networks(): print("Scanning for networks...") result = subprocess.run(["ifconfig", "wlan0", "scan"], capture_output=True, text=True) output = result.stdout networks = [] lines = output.split('\n') for line in lines: if "PSP_" in line: ssid = line.strip()[00:38].split()[0] # Extract SSID up to 27 characters networks.append(ssid) print("Available networks:", networks) return networks # Function to check if packet stream is active def is_packet_stream_active(): tshark_command = ["tshark", "-i", "wlan0", "-Y", "wlan.sa == HonKaiMac", "-c", "1", "-T", "fields", "-e", "frame.time"] result = subprocess.run(tshark_command, capture_output=True, text=True) return bool(result.stdout.strip()) # Check if there's output # Function to switch to a new SSID def switch_to_new_ssid(new_ssid): subprocess.run(["ifconfig", "wlan0", "ssid", new_ssid]) # Change the SSID of the existing interface print(f"Switched to {new_ssid}") # Main Script if __name__ == "__main__": setup_adapter() # Set up the network adapter last_stream_activity_time = time.time() # Initialize last stream activity time last_connected_ssid = "" # Initialize last connected SSID # Start packet stream monitoring in a separate thread packet_stream_thread = threading.Thread(target=packet_stream_monitor) packet_stream_thread.daemon = True # Set as a daemon thread to exit when main thread exits packet_stream_thread.start() while True: if not is_packet_stream_active(): inactive_duration = time.time() - last_stream_activity_time if inactive_duration >= 5: # Check if stream has been inactive for more than 5 seconds print("Packet stream stopped for more than 5 seconds. Scanning and switching SSID if available...") available_ssids = scan_for_networks() psp_ssids = [ssid for ssid in available_ssids if ssid.startswith("PSP")] if psp_ssids: new_ssid = psp_ssids[0] # Choose the first available "PSP" network if new_ssid != last_connected_ssid: switch_to_new_ssid(new_ssid) last_connected_ssid = new_ssid else: print("No available 'PSP' networks") # Sleep for a short period before checking again time.sleep(1) # Sleep for 1 second before checking packet stream again