-
Notifications
You must be signed in to change notification settings - Fork 0
/
execmd.c
187 lines (145 loc) · 4.72 KB
/
execmd.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#include <stdio.h>
#define BUS_RET_SUCCESS (0)
#define BUS_RET_FAILED (-1)
// 描述:execmd函数执行命令,并将结果存储到result字符串数组中
// 参数:cmd表示要执行的命令
// result是执行的结果存储的字符串数组
// 函数执行成功返回1,失败返回0
int api_exeCmdtoString(char *vpCmd, char *vpResult, int vLen)
{
char buffer[128] = {0};
int len = 0;
FILE* pipe = popen(vpCmd, "r");
if (NULL == pipe)
{
return BUS_RET_FAILED;
}
while(!feof(pipe))
{
if(NULL != fgets(buffer, sizeof(buffer), pipe))
{
len += strlen(buffer);
if(len >= vLen)
{
printf("len=%d >= vLen=%d.\n", len, vLen);
return BUS_RET_FAILED;
}
strcat(vpResult, buffer);
}
}
pclose(pipe);
return BUS_RET_SUCCESS;
}
int api_extractStatisticsFromCmd(const char *vpCmd, unsigned long *vpUsStats, unsigned long *vpDsStats)
{
char usStr[64] = {0};
char dsStr[64] = {0};
//" RX bytes:884786511 (843.7 MiB) TX bytes:413554232 (394.3 MiB)"
if(EOF == sscanf(vpCmd,
"%*[]+ %*[^ ] %*[^ ] %[^ ] %*[^ ] %*[^ ] %*[^ ] %[^ ] %*[^ ] %*[^\n]",
dsStr,
usStr))
{
printf("read from \"%s\" error.\n", vpCmd);
return BUS_RET_FAILED;
}
if(EOF == sscanf(dsStr,
"%*[^:]:%d[^\n]",
vpDsStats))
{
printf("read from \"%s\" error.\n", dsStr);
return BUS_RET_FAILED;
}
if(EOF == sscanf(usStr,
"%*[^:]:%d[^\n]",
vpUsStats))
{
printf("read from \"%s\" error.\n", usStr);
return BUS_RET_FAILED;
}
printf("dsStr=%s, usStr=%s.\n", dsStr, usStr);
return BUS_RET_SUCCESS;
}
static int api_getIfconfigStatsbyIfName(char *vpWanIfName, unsigned long *vpUsStats, unsigned long *vpDsStats)
{
char result[128] = {0};
char cmd[128] = {0};
snprintf(cmd, sizeof(cmd), "ifconfig %s |grep bytes", vpWanIfName);
printf("cmd=%s\n", cmd);
if(BUS_RET_SUCCESS != api_exeCmdtoString(cmd, result, sizeof(result)))
{
printf("call api_exeCmdtoString failed!\n");
return BUS_RET_FAILED;
}
if(BUS_RET_SUCCESS != api_extractStatisticsFromCmd(result, vpUsStats, vpDsStats))
{
printf("call api_extractStatisticsFromCmd failed!\n");
return BUS_RET_FAILED;
}
return BUS_RET_SUCCESS;
}
static int api_getWanIfName(int vIndex, char *vpWanIfName, int vlen)
{
strncpy(vpWanIfName, "eth3", vlen-1);
printf("find the %d wan ifname is %s.\n", vIndex, vpWanIfName);
return BUS_RET_SUCCESS;
}
static int api_getWanIfStatsInt(int vWanIndex, unsigned long *vpUsStats, unsigned long *vpDsStats)
{
char wanIfName[32] = {0};
int ret = BUS_RET_SUCCESS;
ret = api_getWanIfName(vWanIndex, wanIfName, sizeof(wanIfName));
if(BUS_RET_SUCCESS != ret)
{
printf("call api_getWanIfName failed!\n");
return BUS_RET_FAILED;
}
ret = api_getIfconfigStatsbyIfName(wanIfName, vpUsStats, vpDsStats);
if(BUS_RET_SUCCESS != ret)
{
printf("call api_getIfconfigStatsbyIfName failed!\n");
return BUS_RET_FAILED;
}
return BUS_RET_SUCCESS;
}
typedef struct {
char UsStats[32];
char DsStats[32];
} api_WanIfStats_t;
static int api_getWanIfStats(int vWanIndex, api_WanIfStats_t *vpWanIfStats)
{
unsigned long ulUsStats = 0;
unsigned long ulDsStats = 0;
int ret = BUS_RET_SUCCESS;
ret = api_getWanIfStatsInt(vWanIndex, &ulUsStats, &ulDsStats);
if(BUS_RET_SUCCESS != ret)
{
printf("call api_getWanIfStatsInt failed!\n");
return BUS_RET_FAILED;
}
snprintf(vpWanIfStats->UsStats, sizeof(vpWanIfStats->UsStats), "%lu", ulUsStats);
snprintf(vpWanIfStats->DsStats, sizeof(vpWanIfStats->DsStats), "%lu", ulDsStats);
return BUS_RET_SUCCESS;
}
int main()
{
unsigned long long int us = 0, ds = 0;
int ret = 0;
ret = api_getIfconfigStatsbyIfName("eth3", &us, &ds);
if(0 != ret)
{
printf("call api_getIfconfigStatsbyIfName failed!\n");
return -1;
}
printf("us=%d, ds=%d.\n", us, ds);
api_WanIfStats_t wanIfStats;
memset(&wanIfStats, 0, sizeof(wanIfStats));
ret = api_getWanIfStats(3, &wanIfStats);
if(BUS_RET_SUCCESS != ret)
{
printf("call api_getWanIfStats failed!\n");
return BUS_RET_FAILED;
}
printf("wanIfStats:us=%s, ds=%s.\n", wanIfStats.UsStats, wanIfStats.DsStats);
return 0;
}