nmcli is script unfriendly

Bug #2045186 reported by psl
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
network-manager (Ubuntu)
New
Undecided
Unassigned

Bug Description

Ubuntu 20.04, Linux Mint 20.3, amd64
network-manager 1.22.10-1ubuntu2.3

```
$ nmcli -v
nmcli tool, version 1.22.10
```

I wanted to script nmcli to make configuration of WiFi of IoT device easy but it is not an easy task. I assume this is a bug in nmcli design, not specific to Ubuntu...

The problem. IoT devices with WiFi has to be configured in some way. WiFi parameters could be hardcoded in the code or there could be a way to configure WiFi in other way, like that device is switched to AP mode and user can connect and configure the device. ESP32 devices have WiFi Manager library for WiFi provisioning https://dronebotworkshop.com/wifimanager/

A nice example of such device is NerdMiner based on ESP32, https://github.com/BitMaker-hub/NerdMiner_v2

NerdMiner in WiFi provisioning mode creates WiFi AP with SSID NerdMinerAP and WiFi password is MineYourCoins. I found it really boring to configure this device manually and I tried to script it. It is boring to wait until user notebook founds WiFi "NerdMinerAP, it can take a minute or two, so I tried to use nmcli to automate the process...

Issues:

1) I know I want to connect to AP NerdMinerAP but until notebook founds it, I cannot do that.
There is no way to force nmcli to wait for NerdMinerAP:

```
$ nmcli --wait 120 dev wifi connect NerdMinerAP; echo $?
Error: No network with SSID 'NerdMinerAP' found.
10

$ # NerdMinerAP is visible, nmcli can connect
$ nmcli --wait 120 dev wifi connect NerdMinerAP; echo $?
Device 'wlo1' successfully activated with '7d333cdb-87e0-409b-9c98-9ef63b5c331c'.
0
```

2) nmcli dev wifi scan returns always "Success" and error code 10.
NerdMinerAP is not available:

```
$ nmcli dev wifi list | grep NerdMinerAP; nmcli dev wifi list bssid NerdMinerAP; echo $?
Success
10
```

NerdMinerAP is visible, error code is 10, again :-(:

```
$ nmcli dev wifi list | grep NerdMinerAP; nmcli dev wifi list bssid NerdMinerAP; echo $?
        86:FC:E6:11:22:33 NerdMinerAP Infra 1 135 Mbit/s 90 ▂▄▆█ WPA2
Success
10
```

---

It is possible to script that action but it is not as easy as it could be. This is a working solution:

```
#!/bin/sh

BSSID="NerdMinerAP"
[ -n "$1" ] && BSSID="$1"

# wait until provisioning AP is visible..
while sleep 2; do
  echo "INFO: Waiting for WiFi $BSSID..."
  R="$(nmcli dev wifi list 2>/dev/null | grep $BSSID)"
  [ -n "$R" ] && break
  #nmcli device wifi rescan ssid "$BSSID" 2>/dev/null
done

# We have found it, connect to it!
echo "INFO: WiFi $BSSID detected, connecting..."
nmcli dev wifi connect "$BSSID"

# show connection details
nmcli connection show --active
nmcli dev wifi show # show QR code!! password is shown...
```

---

Other working solution, try to connect again and again...

```
#!/bin/sh

BSSID="NerdMinerAP"
[ -n "$1" ] && BSSID="$1"

while sleep 2; do
  echo "INFO: Waiting for WiFi $BSSID..."
  nmcli --wait 20 dev wifi connect "$BSSID" 2>/dev/null && break
  nmcli device wifi rescan ssid "$BSSID" 2>/dev/null
done
echo "INFO: Connected to WiFi $BSSID"

nmcli connection show --active
nmcli dev wifi show # show QR code!! password is shown...
```

This one works too but time from time the script ends with an error:

```
INFO: Waiting for WiFi NerdMinerAP...
Error: Connection activation failed: (0) No reason given.
```

psl (slansky)
description: updated
psl (slansky)
description: updated
psl (slansky)
description: updated
description: updated
psl (slansky)
description: updated
description: updated
psl (slansky)
description: updated
description: updated
description: updated
psl (slansky)
description: updated
description: updated
description: updated
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.