如何在 Linux 上获取可用无线网络的列表?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/400240/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 16:49:38  来源:igfitidea点击:

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

linuxwireless

提问by richq

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.

我想获得可用无线网络的列表。理想情况下,这将通过一些 C 调用进行,但我不介意是否必须使用系统调用来混淆它。如果所需的 C 调用或程序不需要一些奇特的 3rd 方包,那就更好了。

The internet seems to suggest I use sudo iwlist <interface> scanwhich 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.

互联网似乎建议我使用sudo iwlist <interface> scanwhich 似乎可以从命令行完成任务,但我宁愿不需要 root 权限。我只想看看基础,不想改变任何东西。

采纳答案by Judge Maygarden

The Wireless Toolspackage -- of which iwlistis a part -- also contains a Wireless Tools Helper Library. You need to include iwlib.hand link with libiw.a(i.e. add -liw). Then look up the documentation for the iw_set_extfunction. The SIOCSIWSCANparameter 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_networksmethod). Alternatively, you can also download the Wireless Tools source codeand take a look at how the iwlib iw_set_ext function is also used for scanning in iwlist.c.

无线工具包-它的iwlist是其中的一部分-还包含一个无线工具帮助程序库。您需要包含iwlib.h并与libiw.a链接(即添加 -liw)。然后查找iw_set_ext函数的文档。该SIOCSIWSCAN参数将是最有用的。有关如何使用此接口的示例,请查看 KDE 库中的 KWifiManager 源代码(请参阅:Interface_wireless_wirelessextensions::get_available_networks方法)。或者,您也可以下载Wireless Tools 源代码,并查看 iwlib iw_set_ext 函数如何也用于 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.

至于权限,我想该进程需要以 root 身份运行才能执行扫描。我很想知道是否也可以这样做。

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

由于您使用的是 Ubuntu 8.04,因此应该使用libiw-dev软件包。

回答by David Matlack

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

在命令行中进行扫描非常容易。手册页是您的朋友(查看iwconfigiwlist)。但是使用 C 接口有点困难,所以我将专注于这一点。

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 .cfiles. 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.hand iwlib.cfor this question.

首先,正如其他人所说,一定要下载无线工具源代码。编程接口的所有文档都在.c文件中。据我所知,该 api 没有网络文档。但是,源代码很容易通读。对于这个问题,您几乎只需要iwlib.hiwlib.c

While you can use iw_set_extand iw_get_ext, the libiwimplements a basic scanning function iw_scan, from which you can extract most of the information that you need.

虽然您可以使用iw_set_extand iw_get_ext,但libiw实现了一个基本的扫描功能iw_scan,您可以从中提取大部分您需要的信息。

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

这是一个简单的程序,用于获取所有可用无线网络的 ESSID。编译-liw并运行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.

免责声明:这只是一个演示程序。某些结果可能没有 essid。此外,这假设您的无线接口是“wlan0”。你明白了。

Read the iwlibsource code!

阅读iwlib源代码!

回答by guy

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

您可以使用nmcli不需要root权限或WIFI接口名称的。

nmcli -t -f ssid dev wifi