(I'm intentionally imprecise with the actual steps i've tried because there are hundreds of things - I know because I looked at the size of my BASH history)
CentOS 7, NetworkManager
(Please correct anything i'm wrong about below)
A customer has an environment with multiple APs having the same SSID. I have to write a BASH script that connects a linux box to each AP in turn, executes a command to gather data, then moves on to the next AP, wash, rinse, repeat. The BSSID is the obvious thing to use to connect. The script qua script is working beautifully, but the BSSID-based wifi connection bit is not.
I have tried iw, iwconfig, nmcli, and wpa_supplicant and cannot get it to reliably connect to the BSSID I specify - regardless of what I put in the command the actual BSSID connected to just seems to be random. From what I have gathered, all of these tools, aside from wpa_supplicant, just take the BSSID, associate it with an SSID, then use the SSID in the connection setup, which, if correct, seems kinda stupid (to be blunt).
wpa_supplicant is just too fragile to use this way - it always gets into a state where wpa_cli cannot connect and I have to continually restart it and often restart NetworkManager.
I thought about switching to Python, but the Python modules just seem to be wrappers to the linux commands, so nothing changes.
If anyone has any ideas please pass them along. I cannot believe that this is so difficult, so I must be missing something obvious...
I tried Eduardo's solution below.
I cannot implement this exactly as provided. I'm running the connection attempt in a loop in a shell script where the MAC address / BSSID is a variable (SSID and password/psk are static).
The command i'm using to connect is:
nmcli connection up ifname wlp4s0 ap $AP passwd-file passwdfile
where passwdfile contains the psk. This is in a loop through the list of $AP.
It still connects to a random AP from the list. (Probably the one that at that instant had the best signal), and more confusing (to me) is that
iw event shows a different connected BSSID than
nmcli -f SSID,BSSID,ACTIVE dev wifi list | grep yes
There must be something really obvious i'm not doing correctly (this is still mostly new to me).