close

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

#define ROUTE_INFO_PATTERN "Destination Gateway Genmask Flags Metric Ref Use Iface"
#define WIFI_DEV_NAME "wlan0"
#define WIFI_HWADDR_NAME "HWaddr"
#define BUF1_LEN 256
#define WIFI_HWADDR_LEN 17
#define BUF2_LEN 512
#define BUF3_LEN 1024
#define DEFAULT_DEST_IP "0.0.0.0"
#define NETMASK_PATTERN "Mask:"

int main()
{
FILE * fp = NULL;
char * head = NULL;
char * tail = NULL;
char * GeteWay = NULL;
char * NetMask = NULL;
char * RSSI = NULL;
char cmd[BUF1_LEN];
char buf[BUF1_LEN];
bool isStart = false;
char * nop = NULL;

memset(cmd, 0x00, sizeof(cmd));

// 1. 取得Gateway
sprintf(cmd, "/sbin/route -n");
fp = popen(cmd, "r");
if (fp == NULL)
{
fprintf(stderr, "%s(%d): popen() failed \n", __FUNCTION__, __LINE__);
goto end;
}

while ( !feof(fp) )
{
memset(buf, 0x00, sizeof(buf));
nop = fgets(buf, sizeof(buf), fp);
head = buf;

if(nop && isStart)
{
tail = NULL;
tail = strstr(head, DEFAULT_DEST_IP);
if(tail)
{
// 找到預設Destination IP後, 第二欄即為GeteWay
head += strlen(DEFAULT_DEST_IP) + 1;
GeteWay = strtok(head, " ");
if(GeteWay == NULL)
{
fprintf(stderr, "%s(%d): Get GeteWay failed \n", __FUNCTION__, __LINE__);
break;
}
tail = GeteWay;
tail += strlen(GeteWay);
*tail = '\0';
printf("Get Gateway = %s, ", GeteWay);
printf("length = %d\n", strlen(GeteWay));
break;
}
}
else
{
tail = strstr(head, ROUTE_INFO_PATTERN);
if (tail && tail == head) { isStart = true; }
}
}
//2. 取得Netmask
fp = NULL;
head = NULL;
tail = NULL;
nop = NULL;
isStart = false;

memset(cmd, 0x00, sizeof(cmd));

sprintf(cmd, "/sbin/ifconfig");
fp = popen(cmd, "r");
if (fp == NULL)
{
fprintf(stderr, "%s(%d): popen() failed \n", __FUNCTION__, __LINE__);
goto end;
}

while ( !feof(fp) )
{
memset(buf, 0x00, sizeof(buf));
nop = fgets(buf, sizeof(buf), fp);
head = buf;

if(nop && isStart)
{
tail = NULL;
//在找出目前的interface(wlan0/eth1)後, 找出Mask所在的那一行(以inet addr起頭)
tail = strstr(head, NETMASK_PATTERN);

if(tail == NULL)
{ break; }

// 找到inet addr後, 第二欄即為NetMask
tail += strlen(NETMASK_PATTERN);
NetMask = tail;
if( NetMask == NULL )
{
fprintf(stderr, "%s(%d): Get NetMask failed \n", __FUNCTION__, __LINE__);
break;
}
tail += strlen(NetMask) - 1;
*tail = '\0';
printf("Get NetMask = %s, ", NetMask);
printf("length = %d\n", strlen(NetMask));
break;
}
else
{
tail = strstr(head, WIFI_DEV_NAME);
if (tail && tail == head) { isStart = true; }
}
}
//2. 取得RSSI
fp = NULL;
head = NULL;
tail = NULL;
nop = NULL;
isStart = false;

memset(cmd, 0x00, sizeof(cmd));

sprintf(cmd, "/sbin/iwconfig");
fp = popen(cmd, "r");
if (fp == NULL)
{
fprintf(stderr, "%s(%d): popen() failed \n", __FUNCTION__, __LINE__);
goto end;
}

while ( !feof(fp) )
{
memset(buf, 0x00, sizeof(buf));
nop = fgets(buf, sizeof(buf), fp);
head = buf;

if(nop && isStart)
{
tail = NULL;
//在找出目前的interface(wlan0/eth1)後, 找出Mask所在的那一行(以inet addr起頭)
tail = strstr(head, " Signal level:");

if(tail == NULL)
{ break; }

// 找到inet addr後, 第二欄即為NetMask
head += strlen(" Signal level:");
if( head == NULL )
{
fprintf(stderr, "%s(%d): Get NetMask failed \n", __FUNCTION__, __LINE__);
break;
}
RSSI = head;
tail = strstr(head, " ");
*tail = '\0';
printf("Get RSSI = %s, ", RSSI);
printf("length = %d\n", strlen(RSSI));
break;
}
else
{
tail = strstr(head, WIFI_DEV_NAME);
if (tail && tail == head) { isStart = true; }
}
}
end:
return 0;
}

arrow
arrow
    文章標籤
    Linux wlan
    全站熱搜

    lynn770707 發表在 痞客邦 留言(0) 人氣()