python script for RaspberryPi to connect wifi automatically

后端 未结 3 1736
清酒与你
清酒与你 2021-02-03 13:01

I want to operate a WiFi dongle with RaspberryPi, (it\'s like a CPU without built-in WiFi). I need to write a python script which automatically scan for WiFi networks and a conn

3条回答
  •  伪装坚强ぢ
    2021-02-03 13:41

    wifi is a python library for scanning and connecting to wifi networks on linux. You can use it to scan and connect to wireless networks.

    It doesn't have any built-in support for connecting automatically to a network, but you could easily write a script to do that. Here's an example of a basic idea for how to do this.

    #!/usr/bin/python
    from __future__ import print_function
    
    from wifi import Cell, Scheme
    
    # get all cells from the air
    ssids = [cell.ssid for cell in Cell.all('wlan0')]
    
    schemes = list(Scheme.all())
    
    for scheme in schemes:
        ssid = scheme.options.get('wpa-ssid', scheme.options.get('wireless-essid'))
        if ssid in ssids:
            print('Connecting to %s' % ssid)
            scheme.activate()
            break
    

    I just wrote it and it seems to work. Just so you know, I wrote the wifi library. If you want me to add this feature to that library, I could.

提交回复
热议问题