标签:fp sum 端口 argv C语言 CentOS7 result printf sizeof
之前我在这篇文章里介绍了使用脚本方便地检测特定端口流量的方法。但是实际使用时发现脚本太过臃肿,执行效率不高,遂萌生出将其写为C程序的想法。下面提供我自己写的代码,以供参考。
注:本程序在CentOS7x64
下编译通过。
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
int daemon(int nochdir, int noclose);
int main(int argc, char *argv[], char *envp[])
{
daemon(1, 1);
//load parameters
char s_outputdir[100];
char s_result[20];
char s_command[strlen("iftop -nNtP -s 60 2>/dev/null | grep 65536 | cut -d b -f4 | sed 's/ //g' | grep B | sed -e '/Cumulative/d'") + 1];
double d_sum = 5; //borrow it to make a count below in checking whether process exist.
sprintf(s_command, "ps aux | grep %s | sed -e '/grep/d'", argv[0]);
//printf("\n%s\n",s_command);
FILE *fp = NULL;
fp = popen(s_command, "r");
while(d_sum --)fgets(s_outputdir, sizeof(s_result) - 1, fp); //borrow it to store some data temply.
memset(s_result, 0, sizeof(s_result));
fgets(s_result, sizeof(s_result) - 1, fp);
fclose(fp);
if(s_result[0])
{
printf("\nprocess already exists!\n");
return 1;
}
//printf("%d", argc); //when you enter 2 paras, it should be 3 now.
if(argc != 4 && argc != 2)
{
printf("\nwrong parameter numbers!\n");
return 1;
}
//printf("%s\n", argv[1]);
if(!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help"))
{
printf("\nUsage:[command dir] <second> <port number> <file name>\n");
return 0;
}
if(atoi(argv[1]) > 60 || atoi(argv[1]) <= 0)
{
printf("\nsecond larger than 1 min or less than 1 sec.!\n");
return 1;
}
if(atoi(argv[2]) > 65536 || atoi(argv[2]) <= 0)
{
printf("\nport number should be smaller than 65536 and larger than 0!\n");
return 1;
}
strcpy(s_outputdir, argv[3]);
//printf("%s %s\n", s_second, s_portnumber);
//prepare for the shell cmd.
sprintf(s_command,"iftop -nNtP -s %s 2>/dev/null | grep %s | cut -d b -f4 | sed 's/ //g' | grep B | sed -e '/Cumulative/d'", argv[1], argv[2]);
//printf("%s\n", s_command);
//do iftop
while(1){ //make a daemon.
fp = NULL;
fp = popen(s_command, "r");
if(!fp)
{
printf("\ndoing shell error!\n");
return 1;
}
memset(s_result, 0, sizeof(s_result));
d_sum = 0;
//load result and make a sum.
while(fgets(s_result, sizeof(s_result) - 1, fp))
{
//printf("%s %d\n", s_result, strlen(s_result)); //when result is 1.76MB, strlen is 7.
if(s_result[0] == 'B' || s_result[0]=='K' || s_result[0] == 'M')continue;
else if(s_result[strlen(s_result) - 3] == 'M'){d_sum += atof(s_result);continue;}
else if(s_result[strlen(s_result) - 3] == 'K'){d_sum += atof(s_result)/1000;continue;}
else {d_sum += atof(s_result)/1000000;continue;}
}
pclose(fp);
//printf("\nbefore open the file:%f\n",d_sum);
//output sum to a file.
fp = NULL;
fp = fopen(s_outputdir, "r");
//printf("opened the file with ro.\n");
memset(s_result, 0, sizeof(s_result));
if(!fp)printf("\nno file exist! trying to create one.\n");
else
{
fgets(s_result, sizeof(s_result) - 1, fp); //we borrow s_result to load the old sum.
fclose(fp);
//printf("closed the file in ro.\n");
}
fp = NULL;
//printf("opened the file in wo\n");
fp = fopen(s_outputdir, "w");
//printf("\nold:%fMB\n", d_sum);
d_sum+=atof(s_result);
//printf("\nnew:%fMB\n", d_sum);
fprintf(fp,"%fMB\n", d_sum);
fclose(fp);
//printf("closed the file.\n");
}
return 0;
}
标签:fp,sum,端口,argv,C语言,CentOS7,result,printf,sizeof 来源: https://blog.csdn.net/u011570312/article/details/96986573
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。