in deepracer_systems_pkg/deepracer_systems_pkg/network_monitor_module/wifi_config_utils.py [0:0]
def connect_wifi(ssid, password):
"""Attempts to connect to the specified WiFi network.
Args:
ssid (str): WiFI network name to connect to.
password (str): WiFi password.
Returns:
tuple: Success flag and list of the command response lines.
"""
cmd = ["nmcli",
"device"]
# Add SSID to connect to.
cmd.extend(["wifi", "connect", ssid])
# Add password if specified.
if password != "":
cmd.extend(["password", password])
try:
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = p.communicate()
returncode = p.returncode
except Exception as ex:
out = ""
err = f"Error while executing nmcli: {ex}"
returncode = -1
finally:
out_response = clean_nmcli_response(out)
err_response = clean_nmcli_response(err)
response = out_response + err_response
success = (returncode == 0)
result = list()
for line in response:
if line == "":
continue
result.append(line)
if "error" in line.lower():
success = False
if success:
result.insert(0, str(f"Successfully connected to \"{ssid}\":"))
else:
result.insert(0, str(f"({returncode}) Failed to connect to \"{ssid}\":"))
return success, result