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: 36576
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

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.
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

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: 36576
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

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.
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

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!!!

Return to “Advanced users”