firion
firion

Reputation: 336

Connecting to wifi in an embedded environment and check for the outcome without NetworkManager

I'm working on a python script on an embedded environment with a minimal linux. I wrote a function to connect to a wifi network and check the connection. NetworkManager is not available. The current implementation is the following:

import subprocess
import socket
import time
    
def connect():
    subprocess.call(['killall', 'wpa_supplicant'])
    wc_path = 'wpa.conf' # generated with wpa_passphrase
    subprocess.call(['ifconfig', 'wlan0', 'up'])
    subprocess.call(['wpa_supplicant', '-c', wc_path, '-i', 'wlan0', '-B'])
    time.sleep(5)
    subprocess.call(['udhcpc', '-i', 'wlan0', '-b'])
    time.sleep(5)
    

    # check connection
    try:
        socket.setdefaulttimeout(3)
        socket.socket(socket.AF_INET, socket.SOCK_STREAM).connect(('8.8.8.8', 53))
        
        connected = True
    except socket.error:
        connected = False

I would like to know if the connection attempt using wpa_supplicant and udhcpc was successful without the need of manually checking the connection. In case of failure, I also would like to know if the credentials were incorrect or there was another problem. How can I do it?

Upvotes: 0

Views: 39

Answers (0)

Related Questions