Linux:如何在没有root权限的情况下获取无线网络SSID?

5

有没有办法在无需root权限的情况下获取当前无线网络的SSID?

iwconfig可以告诉我ESSID,但只有在以root身份运行时才能使用。


如果您以普通用户身份运行它会发生什么?您使用哪个Linux发行版?另外,您可能会发现 [unix.se] 更适合此类问题的网站。 - Lev Levitsky
1个回答

6
如果您查看 (无线工具)的源代码,您将看到以下内容:
iwconfig.c:639: if(iw_get_ext(skfd, ifname, SIOCGIWESSID, &wrq) < 0)

这行代码负责获取ESSID(wireless.h)。我认为只有root用户才有权限(开箱即用),因此调用ioctl的函数(定义在中,来自包)将返回EPERM操作不允许)。
/*------------------------------------------------------------------*/
/*
 * Wrapper to extract some Wireless Parameter out of the driver
 */
static inline int
iw_get_ext(int                  skfd,           /* Socket to the kernel */
           const char *         ifname,         /* Device name */
           int                  request,        /* WE ID */
           struct iwreq *       pwrq)           /* Fixed part of the request */
{
  /* Set device name */
  strncpy(pwrq->ifr_name, ifname, IFNAMSIZ);
  /* Do the request */
  return(ioctl(skfd, request, pwrq));
}

你有两种解决方案:
  1. 使用 setuid 允许用户使用 iwconfig 命令:

    sudo chmod u+s /sbin/iwconfig

  2. 你也可以尝试使用 CAP_NET_ADMIN 权限来为特定用户授予一些特定的权限。以下是关于 CAP_NET_ADMIN 的一些链接:

http://packetlife.net/blog/2010/mar/19/sniffing-wireshark-non-root-user/

http://peternixon.net/news/2012/01/28/configure-tcpdump-work-non-root-user-opensuse-using-file-system-capabilities/

http://www.lids.org/lids-howto/node48.html

http://lwn.net/Articles/430462/

最后,你可以使用 strace 跟踪所有系统调用,并确认 ioctl 调用是导致此问题的原因:

作为 root 用户,执行以下操作:

#strace /sbin/iwconfig your_interface_name > strace_iwconfig_root.log

同普通用户一样:

$strace /sbin/iwconfig your_interface_name > strace_iwconfig_normal.log

并且比较结果。

太好了!使用chmod u+s或者使用CAP_NET_ADMIN和CAP_NET_ADMIN做一些事情都非常有效。非常感谢! - npcode
如果系统上安装了 network-manager,则可以解决这个问题。然后,您可以(根据版本)使用 nm-toolnmctl。我的地理位置库有示例代码,请参见:https://github.com/privatwolke/geolocation - Stephan Klein
$ nmcli dev 怎么样? - AAAfarmclub

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接