Add WiFi Connection
Description
Adds a new Wi-Fi connection with the specified SSID, password, and connection name. Also attempts to activate the connection.
Function Signature:
def add_wifi_connection(ssid: str, password: str, connection_name: str, priority=1) -> None:
Parameters
- ssid: The SSID (name) of the Wi-Fi network.
- password: The password for the Wi-Fi network.
- connection_name: The name you want to assign to the Wi-Fi connection.
- priority (default=1): The autoconnect priority for the connection. Higher values indicate higher priority.
Returns
- None
Example Usage
add_wifi_connection('MyWiFi', 'password123', 'HomeConnection', priority=10)
Notes
This function uses nmcli
to add a Wi-Fi connection and modify its settings. It first adds the connection, then modifies it with WPA2-PSK encryption, and finally attempts to bring it up (activate it).
- The
subprocess.run()
method is used to executenmcli
commands, which are part of NetworkManager. - If the connection is successfully added and activated, a success message will be printed. If not, it will indicate that it was added but unable to connect.
- If there's an error while running the commands, it will print the error message indicating what went wrong.
Error Handling
- If the
nmcli
commands fail (e.g., due to incorrect input or missing permissions), asubprocess.CalledProcessError
will be caught and the error message will be printed.