怎么用脚本获取无线网络列表

wen 实用脚本 1

本文目录导读:

怎么用脚本获取无线网络列表

  1. 1. Windows (PowerShell)
  2. 2. Linux (Bash)
  3. 3. macOS (Bash/Zsh)
  4. 4. Python (跨平台)
  5. 5. 批处理 (Windows CMD)
  6. 注意事项

Windows (PowerShell)

# 方法1:使用 netsh 命令(最稳定)
$wifiList = netsh wlan show networks | Select-String "SSID" | ForEach-Object {
    $_ -replace "^\s+SSID\s+\d+\s+:\s+", ""
}
$wifiList
# 方法2:使用 Get-WifiNetwork 模块(需要安装)
# Install-Module -Name WifiProfileManager
Get-WifiNetwork | Select-Object SSID, SignalStrength, Authentication
# 方法3:使用 netsh 输出详细格式
netsh wlan show networks mode=bssid

Linux (Bash)

#!/bin/bash
# 方法1:使用 nmcli(NetworkManager)
nmcli -f SSID,SIGNAL,SECURITY dev wifi list
# 方法2:使用 iwlist(需要sudo)
sudo iwlist scan 2>/dev/null | grep -E "ESSID|Signal level|Quality"
# 方法3:使用 iw(较新工具)
iw dev wlan0 scan | grep -E "SSID|signal|freq"
# 方法4:使用 wpa_cli(如果使用wpa_supplicant)
wpa_cli scan_results

macOS (Bash/Zsh)

#!/bin/bash
# 方法1:使用 airports 命令
/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport -s
# 方法2:使用 networksetup
networksetup -listallhardwareports | grep -A1 "Wi-Fi" | grep "Device"
# 方法3:使用 system_profiler
system_profiler SPAirPortDataType | grep "SSID"

Python (跨平台)

import subprocess
import re
import platform
def get_wifi_list():
    system = platform.system()
    wifi_list = []
    if system == "Windows":
        result = subprocess.run(["netsh", "wlan", "show", "networks"], 
                              capture_output=True, text=True)
        for line in result.stdout.split('\n'):
            if "SSID" in line and ":" in line:
                ssid = line.split(":")[1].strip()
                wifi_list.append(ssid)
    elif system == "Linux":
        result = subprocess.run(["nmcli", "-f", "SSID", "dev", "wifi", "list"],
                              capture_output=True, text=True)
        for line in result.stdout.split('\n')[1:]:
            if line.strip():
                wifi_list.append(line.strip())
    elif system == "Darwin":  # macOS
        airport = "/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport"
        result = subprocess.run([airport, "-s"], capture_output=True, text=True)
        for line in result.stdout.split('\n')[1:]:
            if line.strip():
                parts = line.split()
                if parts:
                    wifi_list.append(parts[0])
    return wifi_list
# 使用
networks = get_wifi_list()
for network in networks:
    print(network)

批处理 (Windows CMD)

@echo off
netsh wlan show networks | findstr "SSID"

注意事项

  1. 权限问题

    • Linux需要root权限执行扫描
    • Windows可能需要管理员权限
  2. 依赖工具

    • Linux需要安装 wireless-toolsiw
    • 确保系统有WiFi网卡并开启
  3. 格式化输出

    • 可以添加信号强度过滤
    • 支持导出为JSON/CSV格式

建议先测试系统支持的命令,再选择合适的脚本方法。

抱歉,评论功能暂时关闭!