在c中使用iwconfig解析ssid

时间:2012-10-28 22:46:06

标签: c linux parsing iwconfig

我正在为DWM(ubuntu linux)构建一个栏,显示无线网络详细信息,例如ssid。

这是我的代码:

#include <stdio.h>
#include <stdlib.h>


int main( int argc, char *argv[] )
{

  FILE *fp;
  int status;
  char path[1035];

  /* Open the command for reading. */
  fp = popen("iwconfig", "r");
  if (fp == NULL) {
    printf("Failed to run command\n" );
    exit;
  }
   char s[500];

  /* Read the output a line at a time - output it. */
  while (fgets(path, sizeof(path)-1, fp) != NULL) {
    sprintf(s,"%s%s",s, path);
  }
    //printf("%s",s);
  /* close */
  pclose(fp);


    char delimiter[1] = "s";
    char *ptr;

    ptr = strtok(s, delimiter);

        printf("SSID: %s\n", ptr);


  return 0;
}

我越来越不知所措,不知道该怎么办。 我不认为,这是获得ssid的好方法......:/ 建议?

1 个答案:

答案 0 :(得分:1)

我宁愿使用内核的直接信息(例如netdevice(7))而不是调用子进程。

也许此标题可以提供帮助:http://lxr.free-electrons.com/source/include/linux/wireless.h

修改:如果您仍想使用popen,那么您只需添加| grep Essid:

$ /sbin/ifconfig 2>/dev/null | grep ESSID | cut -d: -f2
"pink-panter"