# Python WiFi Scanner

This utility scans for available WiFi networks using the `pywifi` library. It retrieves the SSID (network name), signal strength (RSSI), and security protocols of nearby access points.

**Modules Used:**
*   `pywifi`: To interface with the wireless network card.
*   `time`: To wait for the scan to complete.
*   [[programming/python/modules/argparse-module|argparse]]: To handle command-line arguments.

## Installation

```bash
pip install pywifi
# On Windows, you might also need comtypes
pip install comtypes
```

## The Code

Save this as `wifi_scanner.py`.

```python
import pywifi
from pywifi import const
import time
import argparse

def scan_wifi():
    wifi = pywifi.PyWiFi()
    
    # Get the first wireless interface
    if len(wifi.interfaces()) == 0:
        print("No WiFi interface found!")
        return

    iface = wifi.interfaces()[0]
    
    print(f"Using interface: {iface.name()}")
    
    # Trigger scan
    print("Scanning for networks (this takes a few seconds)...")
    iface.scan()
    
    # Wait for scan to complete (usually takes 2-5 seconds)
    time.sleep(5)
    
    # Get results
    results = iface.scan_results()
    
    if not results:
        print("No networks found.")
        return

    print(f"\nFound {len(results)} networks:")
    print(f"{'SSID':<30} {'Signal':<10} {'Security'}")
    print("-" * 60)
    
    # Use a set to avoid duplicates (same SSID might appear multiple times)
    seen_ssids = set()
    
    for network in results:
        ssid = network.ssid
        # Skip empty SSIDs or duplicates
        if ssid in seen_ssids or not ssid:
            continue
            
        seen_ssids.add(ssid)
        
        # Signal strength (RSSI)
        signal = network.signal
        
        # Basic check for security (AKM - Authentication Key Management)
        # Note: pywifi returns a list of AKM types
        is_open = const.AKM_TYPE_NONE in network.akm
        security = "Open" if is_open else "Protected"
        
        print(f"{ssid:<30} {signal:<10} {security}")

if __name__ == "__main__":
    # Run the scanner
    scan_wifi()
```

## Usage

```bash
python wifi_scanner.py
```

**Note:** On Linux, you might need `sudo` privileges to perform a scan depending on your system configuration.

[[programming/python/python]]