FireDragon
Posts: 5
Joined: Sat Mar 25, 2017 11:25 pm

Changing Wifi networks from the command line interface

Sat Apr 01, 2017 3:55 pm

Is there a way of changing wireless networks from the CLI on Raspbian?. I just want to switch between networks that have previously been configured in:

Code: Select all

/etc/wpa_supplicant/wpa_supplicant.conf

User avatar
DougieLawson
Posts: 42768
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK

Re: Changing Wifi networks from the command line interface

Sat Apr 01, 2017 4:06 pm

If you put more than one network block in your supplicant file your RPi will automagically connect to the strongest. You can even prioritise the connections.

Code: Select all

network={
ssid="wifssidnumberone"
Priority=35
psk="secretpassword"
}
network={
ssid="numbertwoSSID"
priority=70
psk="pswdsecret"
}
higher priority wins the race.
Languages using left-hand whitespace for syntax are ridiculous

DMs sent on https://twitter.com/DougieLawson or LinkedIn will be answered next month.
Fake doctors - are all on my foes list.

The use of crystal balls and mind reading is prohibited.

PublicMakings
Posts: 2
Joined: Fri Mar 30, 2018 1:25 pm

Re: Changing Wifi networks from the command line interface

Fri Mar 30, 2018 1:55 pm

Hello, I know this is an old thread, but I have been looking for an answer to this question, and I think there is more to be explored. I understand what DougieLawson has provided in terms of the supplicant file and prioritization of network access. But I am curious about switching networks from active user/programatic input, as opposed to the the more passive seeming computer prioritized picking.

Why I am curious about this question --
  • I am creating an interactive installation;
  • The components(pi zeros & one pi 3B) are talking to each other across a closed local network from a wireless router;
  • I want one component(pi 3b) to switch occasionally to da internet on a different router and grab some data;
  • then switch back to the closed network.
I am new to working with digital tools, pi's, the command line & the python, so perhaps I am misunderstanding my network needs and there is a different way to go about solving my needs.

Another way to phrase the question is: Is there a command line equivalent 'switch' to the GUI switching of WIFI networks?

PublicMakings
Posts: 2
Joined: Fri Mar 30, 2018 1:25 pm

Re: Changing Wifi networks from the command line interface

Fri Mar 30, 2018 7:45 pm

Okay... after further searching I found some solutions:
  • There is this script from the raspberry pi forums, which I believe answers the question of switching networks from the command line.
  • And there is the conmanctl package for command line managing network connections, which seems like an appropriate tool.

User avatar
DougieLawson
Posts: 42768
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK

Re: Changing Wifi networks from the command line interface

Fri Mar 30, 2018 8:27 pm

Unless you have two APs with differing SSIDs that are both part of your network you don't need this.

The GUI has a network picker as part of the Raspbian taskbar. Use that, it's easier.
Languages using left-hand whitespace for syntax are ridiculous

DMs sent on https://twitter.com/DougieLawson or LinkedIn will be answered next month.
Fake doctors - are all on my foes list.

The use of crystal balls and mind reading is prohibited.

Takaklas
Posts: 4
Joined: Tue Oct 09, 2018 4:59 pm

Re: Changing Wifi networks from the command line interface

Wed Oct 10, 2018 4:32 pm

Old post but I have not found the answer to this question anywhere else so I decided to post here my solution:

There is a tool named wpa_cli = WPA command line client (https://www.systutorials.com/docs/linux/man/8-wpa_cli/) which is easy to use and solves this problem. You can search all over the internet for usage tutorials, however changing between known wifi's (present in wpa_supplicant.conf) you can just just type wpa_cli which will open its own command line interface and then type these commands in order:

1) interface + your preferred interface , wlan0 is the raspi's onboard default so type interface wlan0
(this step is actually needed in my case , in which my rpi 3B+ defaults to an interface called 'p2p-dev-wlan0' for dunno reason, as presented here https://raspberrypi.stackexchange.com/q ... ed-network)
or you can replace this step by calling wpa_cli -i wlan0 or your preffered interface (useful for commands without opening wpa_cli own's CLI)

2) list_networks , find the respective id of the network you want to connect and then type

3) select_network + the id of the network from previous step eg. select_network 1. Beware, you need to type network id, not name!

and you are good to go. A one line command to connect to your preffered network with name example_name is, combining all together:
wpa_cli -i wlan0 select_network $(wpa_cli -i wlan0 list_networks | grep example_name | cut -f 1)
where you replace example_name with the network you want to connect to.
(idea stolen from https://superuser.com/a/759153 )

P.s. Python users beware, How do I run a command with a pipe | in it ?
see --> https://stackoverflow.com/questions/678 ... -in-python

redlinesprint
Posts: 12
Joined: Sat Dec 01, 2018 7:36 pm

Re: Changing Wifi networks from the command line interface

Sat Dec 01, 2018 7:43 pm

So I have a similar situation. I am planning on setting up my pi using an isolated network (hotspot from my phone) which will have just the pi zero w and my windows laptop connected to it. It will be running Unifipi which looks to be based off of the lite version of Stretch. Once the preliminary setup and testing is done, making sure ssh works and that I can connect via the web address provided, I want to be able to change the network to my primary network that all my Unifi devices are connected to so I can manage them and configure pihole.

Would I have to change the wpa_supplicant file after it is all configured or would I be able to do it via command line? Thanks in advanced!

Takaklas
Posts: 4
Joined: Tue Oct 09, 2018 4:59 pm

Re: Changing Wifi networks from the command line interface

Tue Dec 18, 2018 12:52 am

As far as I can understand you want to make a hotspot just to 'tweak' your pi and then disconnect it from this hotspot and connect to your main wifi? If this is the case I have a recommendation for you. It is good practice that you add all networks that you want to connect your pi to the wpa_supplicant.conf file from the begining and assign to each network a priority eg

Code: Select all

network={
    ssid = "wifi_A"
    psk = "passwordOfA"
    priority = 1
}
network={
   ssid = "wifi_B"
   psk = "passwordOfB"
   priority = 2
}
source: https://raspberrypi.stackexchange.com/q ... k-priority

then your pi will automatically connect at boot with the wifi with biggest priority in range. Assign to your phone hotspot the greatest priority and to your main wifi a lower value priority and then when you disactivate your mobile hotspot, your pi will automatically change wifi and connect to your main wifi.Like that you dont have to change wifi manually. Remember although that if you change wifi network manually from your laptop connected to your pi your ssh with automatically close!!!

marciano
Posts: 31
Joined: Mon Oct 23, 2017 10:54 pm

Re: Changing Wifi networks from the command line interface

Wed Feb 12, 2020 3:21 pm

Takaklas wrote:
Wed Oct 10, 2018 4:32 pm

3) select_network + the id of the network from previous step eg. select_network 1. Beware, you need to type network id, not name!
This could be dangerous if you select an SSID which is not available the time you are going to change WiFi connection.
I don't know if there's something like "if not connected then use an available SSID".
I made a test > select_network 2 (in use network is #0)and it kept waiting for a long time. CTRL-C not useful
It was until I powered off the RPi I got the connection was lost
packet_write_wait: Connection to 192.168.1.55 port 22: Broken pipe
I repeated the same operation and I got an "OK" !.
> list_networks then displayed as [CURRENT] that not available SSID (#2) while > iwgetid -r output was SSID #0
Then I tried $ wpa_cli -i wlan0 select_network $(wpa_cli -i wlan0 list_networks | grep 2 | cut -f 1) and again the only solution was to repower RPi
Finally I tried to connect to another working SSID (#4) but $ wpa_cli -i wlan0 select_network $(wpa_cli -i wlan0 list_networks | grep 4 | cut -f 1) but connection freezes, nothing but repower works. Also tried login from another terminal

akaii
Posts: 11
Joined: Sun Jul 05, 2015 5:08 am

Re: Changing Wifi networks from the command line interface

Thu May 28, 2020 9:30 pm

hey
I'm managing to change my ssid connection quickly with the following scripts :

Code: Select all

#!/bin/bash
word=$(wpa_cli -i wlan0 status |grep -w "id")
id_num=${word:3:3}
if [[ $id_num == "1" ]]; then
        wpa_cli -i wlan0 select_network 0
else
        wpa_cli -i wlan0 select_network 1
fi
My network interface /etc/network/interfaces looks like this :

Code: Select all

source-directory /etc/network/interfaces.d

auto lo
iface lo inet loopback

allow-hotplug wlan0
iface wlan0 inet manual
wpa-roam /etc/wpa_supplicant/wpa_supplicant.conf

iface <ssid1> inet static
address 192.168.1.18
netmask 255.255.255.0
gateway 192.168.1.254
dns-nameservers 192.168.1.254

iface <ssid2> inet static
address 10.10.10.18
netmask 255.255.255.0
gateway 10.10.10.254
dns-nameservers 10.10.10.254
And my wpa_supplicant like this /etc/wpa_supplicant/wpa_supplicant.conf :

Code: Select all

ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
country=FR

network={
        ssid="ssid1"
        psk="<my passwd>"
        key_mgmt=WPA-PSK
        priority=15
        id_str=<ssid1>
}
network={
        ssid="ssid2"
        psk="<my passwd>"
        key_mgmt=WPA-PSK
        priority=1
        id_str="<ssid2>"
}

Return to “Advanced users”