Score:1

nmcli: Proper way of scanning WiFi networks

ru flag

I am writing a script where I need to switch WiFi networks. How should I scan if the WiFi I want to connect is available or not?

I tried the following:

  1. nmcli dev wifi list

    Issue: It lists previously scanned networks, which includes available and non-available networks.

  2. nmcli dev wifi rescan; sleep 10; nmcli dev wifi list;

    Issue 1: It just starts the scan and exits while the scan is running in the background. I could not find how to detect if the scan is finished or not. So I used added waiting time of 10 seconds.

    Issue 2: Even after the scan is finished (After 10 seconds), it is still listing the recently turned off WiFi networks.

  3. nmcli radio wifi off ; sleep 1; nmcli radio wifi on; sleep 4; nmcli dev wifi list

    This one is the worst unexpected. It still shows the WiFi networks which are turned off.

Running the commands as root does not make any difference.

What is wrong with the nmcli? What am I doing wrong?

System information:

OS:
    Ubuntu 20.04 Desktop.

Driver: 
    Kernel driver in use: rtl8821ce

Laptop:
    hp 15 dy1751ms

WiFi
    product:      RTL8821CE 802.11ac PCIe Wireless Network Adapter
    vendor:       Realtek Semiconductor Co., Ltd.
    physical id:  0
    bus info:     pci@0000:01:00.0
    logical name: wlo1
    serial: xx:xx:xx:xx:xx:xx
    width: 64 bits
    clock: 33MHz
    capabilities: bus_master cap_list ethernet physical wireless
    configuration: broadcast=yes driver=rtl8821ce ip=192.168.10.12 latency=0 multicast=yes wireless=IEEE 802.11bgn
    resources: irq:137 ioport:3000(size=256) memory:8050000-8050ffff
chili555 avatar
cn flag
Please try: `nmcli device wifi rescan && nmcli device wifi list` Welcome to Ask Ubuntu.
Ajaib Singh avatar
ru flag
It still shows the unavailable networks.
darth_epoxy avatar
nl flag
For reference http://manpages.ubuntu.com/manpages/xenial/man1/nmcli.1.html
Ajaib Singh avatar
ru flag
@darth_epoxy I have already checked the man pages for this. The question is not about how to use the command.
gb flag
Do you need `nmcli`? Did you try something like `sudo iwlist $INTERFACE scanning | grep ESSID`? And what about `nmcli device wifi list --rescan yes`? Here the unavailable networks are not shown (after a few seconds though).
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.