One can connect to a wifi network through wpa_supplicant
's command line interface, wpa_cli
. wpa_cli
has 2 modes of operation : interactive, and normal.
In normal mode, one must pass the desired command through wpa_cli
's arguments.
For exemple, to connect to a network, one must execute the following commands (as root) :
wpa_cli add_network
> 0 # this is the new network's ID
wpa_cli set_network 0 ssid '"SSID_HERE"'
wpa_cli set_network 0 psk '"PASSPHRASE_HERE"'
wpa_cli enable_network 0
All of these operations can be scripted. But wpa_cli
requires, for set_network ssid
and set_network psk
, that both the SSID and passphrase are escaped by a single quote and a double quote, like this : '"ssid"'
.
However, this escape format is hard to respect in a script when you want to replace the ssid by a variable's value, and wpa_cli keeps returning a FAIL exit status, instead of OK. The following patterns have been tried :
ssid '"$ssid"' # doesn't work, sets SSID to "$ssid" instead of $ssid's value
ssid \'"$ssid"\' # doesn't work
Hence, my question is : How to put the ssid in a variable in a script, and use it as a parameter to give wpa_cli a SSID/passphrase ?
ssid "'$ssid'"
should work.FAIL
too. This is very weird, though, because this should be equivalent to my solution...'"' $ssid '"'
instead of"'" $ssid "'"
... Will edit my answer