import subprocess import time from threading import Thread # 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} SSID") # Main Script if __name__ == "__main__": setup_adapter() # Set up the network adapter print("Adapter setup complete") current_ssid = "" # Keep track of the current connected SSID while True: print("Checking network activity...") networks = scan_for_networks() adhoc_networks = [network for network in networks if network.startswith("PSP")] print("Ad hoc networks:", adhoc_networks) if not is_packet_stream_active(): print("Packet stream inactive. Switching SSID if available...") if adhoc_networks: new_ssid = adhoc_networks[0] # Choose the first available "psp" network if new_ssid != current_ssid: switch_to_new_ssid(new_ssid) current_ssid = new_ssid else: print("Already connected to the same SSID") else: print("No available 'psp' networks") else: print("Packet stream active") # Wait for a short period before checking again time.sleep(5) # Adjust the delay as needed