该程序的主要目的是:统计出以MAP开头的行中,不同IP所占的行数
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define buf_size 85
#define ip_size 15
#define ipfile “ipfilter.txt”
char tmp[ip_size]={0};
typedef struct node
{
char ip[ip_size];
int num;
struct node *next;
} IpInfo;
/*=============链表函数===========*/
IpInfo *Create(IpInfo *h)
{
IpInfo *p, *cp;
if((p=(IpInfo *)malloc(sizeof(IpInfo)))==NULL)
{
printf(“memory malloc failed!”);
exit(0);
}
p->next= NULL;
cp= h;
while(cp->next!=NULL)
cp= cp->next;
cp->next= p;
return p;
}
char* SubChar(char *ip)
{
if (strlen(ip) > 23)
{
strncpy(tmp,ip+5,1);
if (strcmp(tmp,”M”)==0)
strncpy(tmp, ip+9, ip_size);
else
strcpy(tmp, “”);
}
return(tmp);
}
void HandleData(IpInfo *root)
{
FILE *fp;
IpInfo *c;
char *buf;
char *oldip, *newip;
c=root;
oldip=(char *)malloc((ip_size+1)*sizeof(char));
newip=(char *)malloc((ip_size+1)*sizeof(char));
if((fp=fopen(ipfile,”r”))==NULL)
{
printf(“file ‘%s’ open error!\n”, ipfile);
exit(0);
}
printf(“Now analysing… \n”);
fgets(buf, buf_size, fp);
strcpy(newip, SubChar(buf));
strcpy(oldip, newip);
while (!feof(fp))
{
fgets(buf, buf_size, fp);
strcpy(newip, SubChar(buf));
if (strcmp(newip, oldip)==0)
c->num++;
else
{
strcpy(oldip, newip);
c = Create(root);
strcpy(c->ip, newip);
c->num =1;
}
}
free(newip);
free(oldip);
fclose(fp);
}
void ShowAbnormalIp(IpInfo *h, int limit)
{
IpInfo *p;
p= h;
while((p->next!= NULL)||(p->num != 0))
{
if (p->num > limit)
printf(“Abnormal IP is:%s \n”,p->ip);
if (p->next != NULL)
p= p->next;
else
break;
}
}
void main(int argc, char *argv[])
{
IpInfo *root; //头指针
int limit;
if (argc >1)
if ((limit=atoi(argv[1]))<0) limit= 2000;
root=(IpInfo*)malloc(sizeof(IpInfo));
strcpy(root->ip, “.”);
root->num=0;
root->next=NULL;
HandleData(root); //分析文件
ShowAbnormalIp(root, limit); //显示结果
}
ipfilter.txt文件格式
1
1 List of active MAP/Redirect filters:
1 List of active sessions:
1 MAP 172.172.0.207 1025 <- -> 61.132.92.51 1025 [58.60.13.8 8080]
1 MAP 172.172.0.207 1025 <- -> 61.132.92.45 1025 [58.60.13.8 8080]
1 MAP 172.172.0.207 1025 <- -> 61.132.92.55 1025 [58.60.13.8 8080]
1 MAP 172.172.0.207 1025 <- -> 61.132.92.33 1025 [58.60.13.8 8080]
1 MAP 172.172.0.207 1025 <- -> 61.132.92.45 1025 [58.60.13.8 8080]
…
1 MAP 172.172.0.202 1025 <- -> 61.132.92.51 1025 [58.60.13.8 8080]
1 MAP 172.172.0.202 1025 <- -> 61.132.92.52 1025 [58.60.13.8 8080]
1 MAP 172.172.0.203 1025 <- -> 61.132.92.53 1025 [58.60.13.8 8080]
1 MAP 172.172.0.203 1025 <- -> 61.132.92.54 1025 [58.60.13.8 8080]
1 RDR 192.168.66.8 80 <- -> 61.132.92.51 80 [222.71.195.216 1081]
1 map xl0 172.172.0.0/16 -> 61.132.92.51/32
1 map xl0 192.168.0.0/16 -> 61.132.92.51/32
1 rdr xl0 61.132.92.51/32 port 3389 -> 192.168.66.8 port 3389 tcp
1 rdr xl0 61.132.92.51/32 port 5001 -> 172.172.7.8 port 5001 tcp
1 rdr xl0 61.132.92.51/32 port 80 -> 192.168.66.8 port 80 tcp
>> 本文固定链接: http://www.vcgood.com/archives/1715
帮你改了一下。改的不好,望见谅,还请高手出来指点。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define buf_size 85
//#define ip_size 15 // 不要忘了字符串结束标记 or xxx.xxx.xxx/xx
#define ip_size 20
#define ipfile “ipfilter.txt”
char tmp[ip_size]={0};
typedef struct node
{
char ip[ip_size];
int num;
struct node *next;
} IpInfo;
/*=============链表函数===========*/
IpInfo *Create(IpInfo *h)
{
IpInfo *p, *cp;
if((p=(IpInfo *)malloc(sizeof(IpInfo)))==NULL)
{
printf(“memory malloc failed!”);
exit(0);
}
p->next= NULL;
cp= h;
while(cp->next!=NULL)
cp= cp->next;
cp->next= p;
return p;
}
char* SubChar(char *ip)
{
char c;
int i=0;
if (strlen(ip) > 23)
{
strncpy(tmp,ip+5,1);
tmp[1]=’\0′;
if (strcmp(tmp,”M”)==0){
while(*(ip+i+9)!= ‘ ‘)
{
tmp[i]= *(ip+i+9);
i++;
}
}
else
tmp[0]=’\0′;
}
// printf(“&&&&tmp=%s***\n”,tmp);
return(tmp);
}
void HandleData(IpInfo *root)
{
FILE *fp;
IpInfo *c;
char *buf;
char *oldip, *newip;
c=root;
oldip=(char *)malloc((ip_size+1)*sizeof(char));
newip=(char *)malloc((ip_size+1)*sizeof(char));
buf=(char *)malloc((buf_size+1)*sizeof(char));
if((fp=fopen(ipfile,”r”))==NULL)
{
printf(“file ‘%s’ open error!\n”, ipfile);
exit(0);
}
printf(“Now analysing… \n”);
fgets(buf, buf_size, fp);
strcpy(newip, SubChar(buf));
//strcpy(oldip, newip);
oldip[0]=’\0′;
while (!feof(fp))
{
fgets(buf, buf_size, fp);
strcpy(newip, SubChar(buf));
if (!strlen(newip)) continue;
if (strcmp(newip, oldip)==0){
c->num++;
}
else
{
strcpy(oldip, newip);
c = Create(root);
strcpy(c->ip, newip);
c->num =1;
}
}
free(newip);
free(oldip);
free(buf);
fclose(fp);
}
void ShowAbnormalIp(IpInfo *h, int limit)
{
IpInfo *p;
p= h;
while((p->next!= NULL)||(p->num != 0))
{
if (p->num > limit)
printf(“Abnormal IP is:%s \n”,p->ip);
if (p->next != NULL)
p= p->next;
else
break;
}
}
int main(int argc, char *argv[])
//void main(int argc, char *argv[]) //gcc 编译的警告。
{
IpInfo *root; //头指针
int limit;
if (argc >1)
if ((limit=atoi(argv[1]))<0) limit= 2000;
root=(IpInfo*)malloc(sizeof(IpInfo));
strcpy(root->ip, “.”);
root->num=0;
root->next=NULL;
HandleData(root); //分析文件
ShowAbnormalIp(root, limit); //显示结果
return 0;
}
感谢孤影恋秋的修改