Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get a list of available wireless networks on Linux?

Tags:

linux

wireless

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.

like image 795
richq Avatar asked Dec 30 '08 13:12

richq


People also ask

How do I see available networks in Linux?

netstat command – It is used to display network connections, routing tables, interface statistics, masquerade connections, and multicast memberships. ifconfig command – It is used to display or configure a network interface. nmcli command – A command to show or configure a network interface on Linux.

How can I see all available wireless networks?

After your Surface restarts, sign in to Windows. Go to Start, and select Settings > Network & internet > Wi-Fi > Show available networks, and see whether your wireless network name appears in the list of available networks.

Where are Wi-Fi networks stored Linux?

If you look in to the /etc/NetworkManager/system-connections directory, you'll find a file stored for each network you had connected to in the past. You can view the content of the desired network connection's file and you'll find the password under wifi-security section and in the line starting with psk.


1 Answers

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");     exit(2);   }    /* Perform the scan */   if (iw_scan(sock, "wlan0", range.we_version_compiled, &head) < 0) {     printf("Error during iw_scan. Aborting.\n");     exit(2);   }    /* Traverse the results */   result = head.result;   while (NULL != result) {     printf("%s\n", result->b.essid);     result = result->next;   }    exit(0); } 

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!

like image 70
David Matlack Avatar answered Oct 04 '22 12:10

David Matlack