免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2611 | 回复: 4
打印 上一主题 下一主题

[C] 如何查找和统计这种记录网络包信息的文本文件(txt) [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-06-09 11:34 |只看该作者 |倒序浏览
我现在有一个网络抓包工具产生的日志文件,里面记录了一些网络包的相关信息。

在文件里,每一行都是一个网络包,第一个是开始时间,第二项是IP,第三项是协议名称,第四项是一些信息。

我现在是想知道

1。如何能把这第二项IP和第三项协议名称从这个文本文件里取出来,就是说在我自己的程序里能调出和打印每个包的IP和协议。

2。如何才能统计这个文件一共有多少行。

我对C下面的文件操作和字符串操作不熟,看了一些文档也不太明白。所以希望高手们能给出一下代码。多谢。

以下是文件结构,但是看起来有点乱,不太好看,所以我上传一个ZIP文件里面是这个文本日志,在附件里。

0.000000  10.27.2.181 -> 10.27.255.255 SMB_NETLOGON SAM LOGON request from client
  0.000281    10.27.2.8 -> 10.27.255.255 BROWSER Browser Election Request
  0.076037    10.27.0.1 -> 224.0.0.5    OSPF Hello Packet
  0.097587    10.27.0.5 -> 12.153.204.71 VNC Server protocol version: \001O\002%\000\f\000
  0.252197 12.153.204.71 -> 10.27.0.5    VNC [Malformed Packet]
  0.252302    10.27.0.5 -> 12.153.204.71 TCP 5901 > 50219 [ACK] Seq=29 Ack=11 Win=46 Len=0
  0.353975 75.66.20.135 -> 10.27.0.14   TCP ttcmremotectrl > ssh [ACK] Seq=1 Ack=1 Win=16356 Len=0
  0.354103   10.27.0.14 -> 75.66.20.135 SSH Encrypted response packet len=68
  0.698544    10.27.0.5 -> 12.153.204.71 VNC Client protocol version: \001O\002%\000\f\000
  0.850785 12.153.204.71 -> 10.27.0.5    VNC Security types supported
  0.850885    10.27.0.5 -> 12.153.204.71 TCP 5901 > 50219 [ACK] Seq=57 Ack=21 Win=46 Len=0
  0.941586 75.66.20.135 -> 10.27.0.14   TCP ttcmremotectrl > ssh [ACK] Seq=1 Ack=69 Win=16288 Len=0
  1.123317 IntelCor_56:5b:a1 -> Broadcast    ARP Who has 10.27.2.166?  Tell 10.27.2.12
  1.298375    10.27.0.5 -> 12.153.204.71 VNC Authentication type selected by client
  1.450877 12.153.204.71 -> 10.27.0.5    VNC Authentication type selected by client
  1.450981    10.27.0.5 -> 12.153.204.71 TCP 5901 > 50219 [ACK] Seq=85 Ack=31 Win=46 Len=0
  1.899372    10.27.0.5 -> 12.153.204.71 VNC Authentication type selected by client
  2.050932 12.153.204.71 -> 10.27.0.5    VNC Authentication type selected by client
  2.051020    10.27.0.5 -> 12.153.204.71 TCP 5901 > 50219 [ACK] Seq=113 Ack=41 Win=46 Len=0
  2.258413   10.27.2.82 -> 239.255.255.250 SSDP NOTIFY * HTTP/1.1
  2.258496   10.27.2.82 -> 239.255.255.250 SSDP NOTIFY * HTTP/1.1
  2.258679   10.27.2.82 -> 239.255.255.250 SSDP NOTIFY * HTTP/1.1
  2.258739   10.27.2.82 -> 239.255.255.250 SSDP NOTIFY * HTTP/1.1
  2.258921   10.27.2.82 -> 239.255.255.250 SSDP NOTIFY * HTTP/1.1
  2.258987   10.27.2.82 -> 239.255.255.250 SSDP NOTIFY * HTTP/1.1

logtext.zip

2.75 KB, 下载次数: 51

论坛徽章:
2
2015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:56:11
2 [报告]
发表于 2008-06-09 14:50 |只看该作者

  1. #include <stdio.h>
  2. #include <ctype.h>

  3. #define DEFAULT_PROTO_NAME_LENGTH  256

  4. struct log_line {
  5.         char name[DEFAULT_PROTO_NAME_LENGTH];
  6.         unsigned char source_ip4[4];
  7.         unsigned char target_ip4[4];
  8. };

  9. int get_eof(FILE *fp) {
  10.         int cc;

  11.         if (fp != NULL) {
  12.                 cc = fgetc(fp);
  13.                 if (cc == EOF) {
  14.                         return 0;
  15.                 }
  16.                 ungetc(cc, fp);
  17.         }
  18.         return -1;
  19. }

  20. int get_space(FILE *fp) {
  21.         int cc;

  22.         if (fp != NULL) {
  23.                 cc = fgetc(fp);
  24.                 if (cc != EOF) {
  25.                         if (isspace(cc)) {
  26.                                 while ((cc = fgetc(fp)) != EOF) {
  27.                                         if (!isspace(cc)) {
  28.                                                 ungetc(cc, fp);
  29.                                                 break;
  30.                                         }
  31.                                 }
  32.                                 return 0;
  33.                         }
  34.                         ungetc(cc, fp);
  35.                 }
  36.         }
  37.         return -1;
  38. }

  39. int get_word(FILE *fp, char *buf, int n) {
  40.         int cc;

  41.         if (fp != NULL && buf != NULL && n > 1) {
  42.                 cc = fgetc(fp);
  43.                 if (cc != EOF) {
  44.                         if (!isspace(cc) && cc != '\n') {
  45.                                 *buf++ = cc;
  46.                                 --n;
  47.                                 while ((cc = fgetc(fp)) != EOF) {
  48.                                         if (isspace(cc) || cc == '\n') {
  49.                                                 ungetc(cc, fp);
  50.                                                 break;
  51.                                         }
  52.                                         if (n > 0) {
  53.                                                 *buf++ = cc;
  54.                                                 n--;
  55.                                         }
  56.                                 }
  57.                                 *buf = '\0';
  58.                                 return 0;
  59.                         }
  60.                         ungetc(cc, fp);
  61.                 }
  62.         }
  63.         return -1;
  64. }

  65. int get_line(FILE *fp, char *buf, int n) {
  66.         int cc;

  67.         if (fp != NULL) {
  68.                 cc = fgetc(fp);
  69.                 if (cc != EOF) {
  70.                         if (cc != '\n') {
  71.                                 while ((cc = fgetc(fp)) != EOF) {
  72.                                         if (cc == '\n') {
  73.                                                 break;
  74.                                         }
  75.                                         if (buf != NULL) {
  76.                                                 if (n-- > 0) {
  77.                                                         *buf++ = cc;
  78.                                                 }
  79.                                         }
  80.                                 }
  81.                                 if (buf != NULL) {
  82.                                         *buf = '\0';
  83.                                 }
  84.                                 return 0;
  85.                         }
  86.                         ungetc(cc, fp);
  87.                 }
  88.         }
  89.         return -1;
  90. }

  91. int get_number(FILE *fp, unsigned limit, unsigned *number) {
  92.         int cc;
  93.         unsigned retval;

  94.         if (fp != NULL) {
  95.                 cc = fgetc(fp);
  96.                 if (cc != EOF) {
  97.                         if (isdigit(cc)) {
  98.                                 retval = cc - '0';
  99.                                 if (retval <= limit) {
  100.                                         while ((cc = fgetc(fp)) != EOF) {
  101.                                                 if (!isdigit(cc)) {
  102.                                                         ungetc(cc, fp);
  103.                                                         if (number != NULL) {
  104.                                                                 *number = retval;
  105.                                                         }
  106.                                                         return 0;
  107.                                                 }
  108.                                                 retval = retval * 10 + cc - '0';
  109.                                                 if (retval > limit) {
  110.                                                         break;
  111.                                                 }
  112.                                         }
  113.                                 }
  114.                         }
  115.                         ungetc(cc, fp);
  116.                 }
  117.         }
  118.         return -1;
  119. }

  120. int get_token(FILE *fp, const char *tok) {
  121.         int cc;

  122.         if (fp != NULL && tok != NULL) {
  123.                 while (*tok != '\0') {
  124.                         cc = fgetc(fp);
  125.                         if (cc == EOF) {
  126.                                 return -1;
  127.                         }
  128.                         if (*tok != cc) {
  129.                                 ungetc(cc, fp);
  130.                                 return -1;
  131.                         }
  132.                         tok++;
  133.                 }
  134.                 return 0;
  135.         }
  136.         return -1;
  137. }

  138. int get_ip4(FILE *fp, unsigned char ip4[4]) {
  139.         unsigned number;

  140.         if (get_number(fp, 255, &number) == 0) {
  141.                 ip4[0] = (unsigned char)number;
  142.                 if (get_token(fp, ".") == 0) {
  143.                         if (get_number(fp, 255, &number) == 0) {
  144.                                 ip4[1] = (unsigned char)number;
  145.                                 if (get_token(fp, ".") == 0) {
  146.                                         if (get_number(fp, 255, &number) == 0) {
  147.                                                 ip4[2] = (unsigned char)number;
  148.                                                 if (get_token(fp, ".") == 0) {
  149.                                                         if (get_number(fp, 255, &number) == 0) {
  150.                                                                 ip4[3] = (unsigned char)number;
  151.                                                                 return 0;
  152.                                                         }
  153.                                                 }
  154.                                         }
  155.                                 }
  156.                         }
  157.                 }
  158.         }
  159.         return -1;
  160. }

  161. int read_line(FILE *fp, struct log_line *line) {
  162.         if (fp != NULL && line != NULL) {
  163.                 if (get_number(fp, ~0, NULL) == 0) {
  164.                         if (get_token(fp, ".") == 0) {
  165.                                 if (get_number(fp, ~0, NULL) == 0) {
  166.                                         if (get_space(fp) == 0) {
  167.                                                 if (get_ip4(fp, line->source_ip4) == 0) {
  168.                                                         if (get_space(fp) == 0) {
  169.                                                                 if (get_token(fp, "->") == 0) {
  170.                                                                         if (get_space(fp) == 0) {
  171.                                                                                 if (get_ip4(fp, line->target_ip4) == 0) {
  172.                                                                                         if (get_space(fp) == 0) {
  173.                                                                                                 if (get_word(fp, line->name, DEFAULT_PROTO_NAME_LENGTH) == 0) {
  174.                                                                                                         if (get_line(fp, NULL, 0) == 0) {
  175.                                                                                                                 return 0;
  176.                                                                                                         }
  177.                                                                                                 }
  178.                                                                                         }
  179.                                                                                 }
  180.                                                                         }
  181.                                                                 }
  182.                                                         }
  183.                                                 }
  184.                                         }
  185.                                 }
  186.                         }
  187.                 }
  188.         }
  189.         return -1;
  190. }

  191. int read_logfile(const char *filename) {
  192.         FILE *fp;
  193.         struct log_line line;
  194.         int lineno;

  195.         if (filename != NULL) {
  196.                 fp = fopen(filename, "r");
  197.                 if (fp != NULL) {
  198.                         lineno = 0;
  199.                         while (get_eof(fp) != 0) {
  200.                                 if (read_line(fp, &line) != 0) {
  201.                                         printf("Line No: %d\n", lineno);
  202.                                         printf("BAD LINE!!!\n");
  203.                                         get_line(fp, NULL, 0);
  204.                                 }else {
  205.                                         if (lineno > 0) {
  206.                                                 printf("\n");
  207.                                         }
  208.                                         printf("Line No: %d\n", lineno);
  209.                                         printf("Source IP: %d.%d.%d.%d\n", line.source_ip4[0], line.source_ip4[1], line.source_ip4[2], line.source_ip4[3]);
  210.                                         printf("Target IP: %d.%d.%d.%d\n", line.target_ip4[0], line.target_ip4[1], line.target_ip4[2], line.target_ip4[3]);
  211.                                         printf("Proto Name: %s\n", line.name);
  212.                                 }
  213.                                 lineno++;
  214.                         }
  215.                         return 0;
  216.                 }
  217.         }
  218.         return -1;
  219. }

  220. int main(void) {
  221.         read_logfile("test.log");
  222.         return 0;
  223. }
复制代码

论坛徽章:
0
3 [报告]
发表于 2008-06-09 16:47 |只看该作者
必须用c么?
用用awk试试

  1. #!/usr/bin/gawk -f
  2. {
  3.   print $2,$3;
  4.   line++;
  5. }
  6. END{
  7. printf "Line is %d\n",line;
  8. }
复制代码

[ 本帖最后由 gawk 于 2008-6-9 16:49 编辑 ]

论坛徽章:
0
4 [报告]
发表于 2008-06-09 22:38 |只看该作者
好长的代码,一会好好研究一下.


原帖由 cobras 于 2008-6-9 14:50 发表

#include
#include

#define DEFAULT_PROTO_NAME_LENGTH  256

struct log_line {
        char name[DEFAULT_PROTO_NAME_LENGTH];
        unsigned char source_ip4[4];
        unsigned char target_ip4[4];
};

...

论坛徽章:
0
5 [报告]
发表于 2008-06-09 22:39 |只看该作者
是的,一定要用C做,因为前面的开发就是C,所以,不能改动了.


原帖由 gawk 于 2008-6-9 16:47 发表
必须用c么?
用用awk试试

#!/usr/bin/gawk -f
{
  print $2,$3;
  line++;
}
END{
printf "Line is %d\n",line;
}
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP