在控制台中执行以下代码时:
int ret = system("iptables -t filter -L");
Run Code Online (Sandbox Code Playgroud)
ret将获得价值1,控制台中将显示一系列规则.问题是我还想获得程序中的规则列表.我正在使用以下解决方案:
int ret = system("iptables -t filter -L >> filter-table.txt");
/* read filter-table.txt file to get the list */
Run Code Online (Sandbox Code Playgroud)
还有其他方法可以获得清单吗?
正如@Charles Duffy和@Kevin所提到的,system()不是你想要的功能. popen()更合适.以下应该有效.请注意,如果您使用的是gcc与编译-std=c99标志,你需要添加#define _POSIX_C_SOURCE 2之前#include <stdio.h>
#include <stdio.h>
#include <error.h>
#define PATH_MAX 1024
int main(void)
{
FILE *fp;
int status;
char path[PATH_MAX];
fp = popen("iptables -t filter -L", "r");
if (fp == NULL)
{
perror("popen");
return -1;
}
while (fgets(path, PATH_MAX, fp) != NULL)
{
printf("%s", path);
/* do something you want with the return data */
}
status = pclose(fp);
if (status == -1)
{
perror("pclose");
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
269 次 |
| 最近记录: |