Aug 272012
 

I recently had some issues with the WiFi on one of my computers. The WiFi network it normally connects to was down and it wouldn’t connect to another network. This also happened on my Raspberry Pi recently, so I did something similar to get it to connect.
Using wpa_cli, I got the network to connect to make sure that all the configuration parameters were correct. I then created the configuration file that I needed for automatic connection.

To connect manually, you need to first start up wpa_cli. This assumes that wpa_supplicant is already running, which it should be in most cases. If it’s not, you will see an error when you start wpa_cli.

Otherwise, once you’re in wpa_cli, it will show a prompt similar to this

Selected interface 'wlan0'

Interactive mode

>

Using the scan and scan_results command, you can scan for the networks and see some basic information

> scan
OK
<2>CTRL-EVENT-SCAN-RESULTS
> scan_results
bssid / frequency / signal level / flags / ssid
00:55:ab:25:ac:5a 2462 -71 [WPA2-PSK-CCMP][ESS] WLAN-Network
00:11:99:51:ba:f0 2437 -64 [WPA2-PSK-CCMP][ESS] WLAN-Network2
>

In the above scan results, you can see the 2 networks that the computer can pick up, the frequencies / channel that they are broadcasting on, the signal strength, the security type, and network name. Both of these networks are using WPA2 with a Pre-Shared Key.

To connect to one of them, we need to first create a network.

> add_network

This will output a number, which is the network ID.

> add_network
0

This will create an empty network ready for us to configure it.
Next up, we need to set the SSID and PSK for the network.

set_network 0 ssid "WLAN-Network"
set_network 0 psk "SupahSecretPassphrase"

Once these parameters are entered, wpa_cli should show the client trying to connect straight away.
When it tries to connect, it should show something like this

> <2>Trying to authenticate with 00:55:ab:25:ac:5a (SSID='WLAN-Network' freq=2437 MHz)
> <2>Trying to associate with 00:55:ab:25:ac:5a (SSID='WLAN-Network' freq=2437 MHz)
> <2>Associated with 00:55:ab:25:ac:5a
> <2>WPA: Key negotiation completed with 00:55:ab:25:ac:5a [PTK=CCMP GTK=CCMP]
> <2>CTRL-EVENT-CONNECTED - Connection to 00:55:ab:25:ac:5a completed (reauth) [id=0 id_str=]

Once the wireless has connected, it should automatically get an IP address.
If it doesn’t you can run the dhclient command to force it to get an IP address via DHCP, or edit /etc/network/interfaces to set a static IP address for the wireless network interface.

Hopefully this helps someone out there !

Share

  29 Responses to “How To : Use wpa_cli To Connect To A Wireless Network”

  1. how can I use wpa_cli to configure my computer as an Access Point ?

    • Haven’t tried that before, but unless you’re going to use ad-hoc mode, you’d best use hostapd to turn it into an access point.

  2. Thanks, it was useful, considering that the man for wpa_cli is useless. What I had to do (in addition to the above) is:

    enable_network 0

    and

    reconnect

    No idea if this is mandatory, but the network was staying with flag [DISABLED], before I issued “enable network” and didn’t really try to associate and authenticate before “reconnect”.

    Regards and thanks: Dimitar

  3. Sorry, forgot to mention that I was not working on a Raspberry Pi, but on a EEEPC 701 with Ubuntu core and minimal LXDE, and my quest was to see what a hustle it is to browse and connect to wireless networks without NetworkManager and the associated applet.
    Well, it is a huge hustle, but it works (as i am sending this over the wireless network already).

    • Glad this helped.
      There are other ways to setup the wifi to connect automatically without Network Manager though –
      You can edit /etc/network/interfaces and place 2 lines in there to automatically connect to a network 🙂

  4. Hello SirLagz,

    wpa_cli seems to hang-up, then the USB dongle seems not to respond anymore on lsusb but still there at lsmod:

    http://www.raspberrypi.org/phpBB3/viewtopic.php?f=28&t=18082

    pi@raspberrypi ~ $ wpa_cli
    wpa_cli v1.0
    Copyright (c) 2004-2012, Jouni Malinen and contributors

    This program is free software. You can distribute it and/or modify it
    under the terms of the GNU General Public License version 2.

    Alternatively, this software may be distributed under the terms of the
    BSD license. See README and COPYING for more details.

    Selected interface ‘wlan0’

    Interactive mode

    > scan
    OK
    CTRL-EVENT-SCAN-RESULTS
    CTRL-EVENT-TERMINATING – signal 15 received
    > ‘PING’ command failed.
    Connection to wpa_supplicant lost – trying to reconnect

  5. Thank you, this helps a lot.

  6. Thank you, thank you, thank you for posting this! I’ve been doing research on how to do networking between two devices for a couple of weeks now, and you helped me find a solution.

  7. Thank you for this.

    I am trying to connect to a public wifi network outside my building.
    I have not yet got my wifi adaptor to connect to it and receive an IP address.
    In the scan, the wifi network shows as having ‘ESS’ security flag. There is no password because it just connects to anything then brings up a login web page.
    Well … dealing with the webpage is another issue I know but (one step at a time) would you have any idea why I am not connecting to it and how to remedy?

    I have confirmed my wifi dongle is working OK by connecting to a router in the house

    • Can you paste the output of the following commands
      wpa_cli scan
      *wait a few seconds before you run the next commands
      wpa_cli scan_results

      You’ll need to do this when you’re in range of that network that you’re trying to connect to

  8. […] Y despuĂ©s de reiniciar, la tarjeta es funcional y se puede configurar con wpa_cli: […]

  9. It helped a lot! Now I am connected to the Wifi thanks to wpa_cli in the Musix distro, which comes with no networking tools apart from the basics (ifconfig, wpa, iwlist, dhclient ).

    Thanks

  10. Thank you this helped me tremendously! I have one question when you said, “Using wpa_cli, I got the network to connect to make sure that all the configuration parameters were correct. I then created the configuration file that I needed for automatic connection.” How exactly did you create the configuration file needed for automatic connection. Basically my problem is in the fact that I have to issue these commands on every reboot:

    # wpa_supplicant -B -iath0_wlan0 -c/etc/wpa_supplicant/wpa_supplicant.conf -O/var/run/wpa_supplicant/
    # …Initialized successfully
    # wpa_cli
    > add_network
    0
    > set_network 0 ssid “dlink”
    OK
    > set_network 0 psk “################”
    OK
    > enable_network 0
    OK
    > reconnect

  11. Please tell me how to display in clear text the passwords saved in wpa_supplicant.conf.

  12. thanks-worked for me d-link 130, jessie, rpi-2. be patient. i had to also enter password via icon upper right of screen next to clock

  13. Actually, i do this step. But i can’t connect to the network.
    It say’s authentication failure. Plsss do something with this. Thanks

  14. After CTRL-EVENT_CONNECTED, The AP doesnt give an IP address to the client, even using dhclient. Does anyone know the reason?
    Then I set the IP address from terminal with ifconfig, but when I try to ping AP from client I can’t reach it.

  15. How to use wap_cli
    Help me, please.

  16. After I use scan, why can’t I make it stop?

  17. HI if I entry the erro passwd.How can I konw the connection’s result is failed ?

 Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

(required)

(required)