iptables-parser/iptables_parser.c
2022-12-16 16:27:38 +07:00

126 lines
3.2 KiB
C

#include <stdio.h>
#include <string.h>
int main()
{
int h,i,j,k,l,v,w,x,y,ctr,dtr;
char str_in[200];
char newString[200][200];
char newParam[100][100];
char str_final[50];
char *filename = "/var/log/iptables.log";
FILE *fp = fopen(filename, "r");
if (fp == NULL)
{
printf("Error: could not open file %s", filename);
return 1;
}
// reading line by line, max 256 bytes
const unsigned MAX_LENGTH = 256;
char buffer[MAX_LENGTH];
h = 0;
while (fgets(buffer, MAX_LENGTH, fp)) {
h = h + 1;
}
printf(" %d\n ", h);
i = 0;
fclose(fp);
fp = fopen(filename, "r");
while (fgets(buffer, MAX_LENGTH, fp)) {
// printf("- %d -", i);
if ( i > h-10 ) {
j=0; ctr=0;
printf("\n%d -> %s \n", i, buffer);
printf("[%d]\t[ Param ]\t\t[ Value ] \n", i);
for (x=0; x <= (strlen(buffer)); x++)
{
if(buffer[x]==' '|| buffer[x]=='\0' || buffer[x]=='\n'){
newString[ctr][j]='\0';
ctr++;
j=0;
}
else {
newString[ctr][j]=buffer[x];
j++;
}
}
for (v=0; v <= ctr ; v++) {
// printf("[ %d ]----> %s \n", v, newString[v]);
strcpy(str_in, newString[v]);
// printf("\n[%d]----> %s \n", i, str_in);
k=0; dtr=0;
memset(newParam, 0, sizeof newParam);
for (w=0; w <= (strlen(str_in)) ; w++) {
if(str_in[w]=='='){
newParam[dtr][k]='\0';
dtr++;
k=0;
}
// else if(str_in[w]!='\n' && str_in[w]!=' ') {
else {
newParam[dtr][k]=str_in[w];
k++;
}
}
strcpy(str_final, newParam[0]);
// if ( newParam[0] && newParam[0][0] != "\n" ) {
if ( *str_final == EOF ) {
// if strcmp(&str_final, '\n') {
continue;
}
printf("[%d]\t[ %s ]\t\t[ %s ] \n", v, str_final, newParam[1]);
// printf("[ Value %d]----> %s \n", i, newParam[1]);
// for (l=0; l <= dtr ; l++) {
// printf("[ + %d]----> %s \n", w, newParam[l]);
// }
}
// j=0; ctr=0;
// for (v=0; v <= (strlen(newString)); v++) {
// if(newString[v]=='='){
// newParam[ctr][j]='\0';
// ctr++;
// j=0;
// }
// else {
// newParam[ctr][j]=newString[v];
// j++;
// }
// }
}
i = i +1 ;
// printf("\n==================================================================================\n");
}
// close the file
fclose(fp);
return 0;
}