• Uncategorized

About linux : How-can-I-get-a-list-of-available-wireless-networks-on-Linux

Question Detail

I would like to get a list of the wireless networks available. Ideally this would be via some C call, but I don’t mind if I have to kludge it with a system call. Even better if the required C call or program doesn’t require some exotic 3rd party package.

The internet seems to suggest I use sudo iwlist <interface> scan which does seem to do the trick from the command line, but I’d rather not require root permissions. I only want to see the basics, not change anything.

Question Answer

It’s pretty easy to do a scan in the command line. The man pages are your friend here (check out iwconfig and iwlist). But using the C interface is a little more difficult so I’ll focus on that.

First of all, as other people have mentioned, definitely download out the wireless tools source code. All the documentation for the programming interface is in the .c files. As far as I can tell, there is no web documentation for the api. However, the source code is pretty easy to read through. You pretty much only need iwlib.h and iwlib.c for this question.

While you can use iw_set_ext and iw_get_ext, the libiw implements a basic scanning function iw_scan, from which you can extract most of the information that you need.

Here is a simple program to get the ESSID for all available wireless networks. Compile with -liw and run with sudo.

#include <stdio.h>
#include <time.h>
#include <iwlib.h>

int main(void) {
  wireless_scan_head head;
  wireless_scan *result;
  iwrange range;
  int sock;

  /* Open socket to kernel */
  sock = iw_sockets_open();

  /* Get some metadata to use for scanning */
  if (iw_get_range_info(sock, "wlan0", &range) < 0) {
    printf("Error during iw_get_range_info. Aborting.\n");

  /* Perform the scan */
  if (iw_scan(sock, "wlan0", range.we_version_compiled, &head) < 0) {
    printf("Error during iw_scan. Aborting.\n");

  /* Traverse the results */
  result = head.result;
  while (NULL != result) {
    printf("%s\n", result->b.essid);
    result = result->next;


DISCLAIMER: This is just a demonstration program. It’s possible for some results to not have an essid. In addition, this assumes your wireless interface is “wlan0”. You get the idea.

Read the iwlib source code!

The Wireless Tools package — of which iwlist is a part — also contains a Wireless Tools Helper Library. You need to include iwlib.h and link with libiw.a (i.e. add -liw). Then look up the documentation for the iw_set_ext function. The SIOCSIWSCAN parameter will be of most use. For an example of how to use this interface, take a look at the KWifiManager source in the KDE library (see: Interface_wireless_wirelessextensions::get_available_networks method). Alternatively, you can also download the Wireless Tools source code and take a look at how the iwlib iw_set_ext function is also used for scanning in iwlist.c.

As for privileges, I imagine the process will need to run as root to perform the scan. I’d love to know if this could be done otherwise as well.

Since you are using Ubuntu 8.04 the libiw-dev package should be of use.

You can use nmcli which does not require root permissions or name of WIFI interface.

nmcli -t -f ssid dev wifi

You may also like...

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.