2

我的项目是关于在客户端以混杂模式捕获数据包并在服务器端处理它(区分 tcp、udp、icmp),这是使用 ubuntu 13.04 中的 C 套接字代码完成的。

截至目前,输出存储在一个 txt 文件中,但我想将这些数据包字段保存在 LibreOffice Calc 的单元格中,以便我可以编辑单元格,只选择那些需要的数据包。

我不知道它是否可以做到?如果可能的话,任何人都可以帮助我吗?还有怎么做?

我的输出 txt 文件看起来是这样的(这里只显示 udp 数据包)

 This Report is from the Task agent whose IP is 127.0.0.1


 ***********************UDP Packet*************************

Ethernet Header
|-Destination Address : 01-00-5E-00-00-02 
|-Source Address      : 00-00-0C-07-AC-3B 
|-Protocol            : 8 

IP Header
|-IP Version        : 4
|-IP Header Length  : 5 DWORDS or 20 Bytes
|-Type Of Service   : 192
|-IP Total Length   : 48  Bytes(Size of Packet)
|-Identification    : 0
|-TTL      : 1
|-Protocol : 17
|-Checksum : 61927
|-Source IP        : 172.16.59.3
|-Destination IP   : 224.0.0.2

UDP Header
|-Source Port      : 1985
|-Destination Port : 1985
|-UDP Length       : 28
|-UDP Checksum     : 42701

IP Header
01 00 5E 00 00 02 00 00 0C 07 AC 3B 08 00 45 C0         ..^........;..E.
00 30 00 00                                             .0..
UDP Header
00 00 01 11 F1 E7 AC 10                                 ........
Data Payload
00 00 10 03 0A 6E 3B 00 63 69 73 63 6F 00 00 00         .....n;.cisco...
AC 10 3B 01 00 00 00 00 00 00 00 00 00 00 00 00         ..;.............
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00         ................
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00         ................
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00         ................
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00         ................
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00         ................
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00         ................
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00         ................
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00         ................
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00         ................
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00         ................
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00         ................
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00         ................
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00         ................
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00         ................
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00         ................
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00         ................
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00         ................
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00         ................
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00         ................
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00         ................
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00         ................
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00         ................
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00         ................
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00         ................
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00         ................
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00         ................
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00         ................
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00         ................
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00         ................
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00         ................
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00         ................
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00         ................
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00         ................
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00         ................
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00         ................
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00         ................
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00         ................
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00         ................
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00         ................
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00         ................
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00         ................
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00         ................
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00         ................
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00         ................
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00         ................
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00         ................
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00         ................
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00         ................
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00         ................
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00         ................
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00         ................
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00         ................
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00         ................
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00         ................
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00         ................
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00         ................
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00         ................
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00         ................
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00         ................
00 00 00 00 00 00                                       ......

###########################################################

我需要将所有这些字段保存在单元格(行)中。我该怎么做??


edited part

这就是我在服务器端处理数据包的方式

FILE *logfile;
int infile;
struct sockaddr_in source,dest;
int tcp=0,udp=0,icmp=0,others=0,igmp=0,total=0,i,j;

int main()
{
int saddr_size,data_size;  
struct sockaddr saddr;   
unsigned char *buffer3 = (unsigned char *) malloc(1024);
char *fname = "/home/shishira/Desktop/packet_capture/task_agent_processed.txt";
infile=open("info_agent_report.txt",O_RDONLY);
if(infile==-1)
 {
    perror("cannot open info_agent_report file\n");
    return(1);
 }  

logfile=fopen("task_agent_processed.txt","w");
if(logfile==NULL)
 {
printf("Unable to create task_agent_processed file.");
 }

printf("\n Starting..\n");
saddr_size = sizeof saddr;     

do
{   

 data_size=read(infile,buffer3,1024);

 ProcessPacket(buffer3 , data_size);  

 }
while(data_size>0);


fclose(logfile);
close(infile);   
printf("\n");
printf(" Finished\n\n");
printf("-------------------\n\n");
return 0;
}

void ProcessPacket(unsigned char* buffer, int size)
{
 //Get the IP Header part of this packet , excluding the ethernet header
 struct iphdr *iph = (struct iphdr*)(buffer + sizeof(struct ethhdr));
 ++total;
 switch (iph->protocol) //Check the Protocol and do accordingly...
  {
  case 1:  //ICMP Protocol
    ++icmp;
    print_icmp_packet( buffer , size);
    break;

case 2:  //IGMP Protocol
    ++igmp;
    break;

case 6:  //TCP Protocol
    ++tcp;
    print_tcp_packet(buffer , size);
    break;

case 17: //UDP Protocol
    ++udp;
    print_udp_packet(buffer , size);
    break;

default: //Some Other Protocol like ARP etc.
    ++others;
    break;
}

printf("            TCP : %d   UDP : %d   ICMP : %d   Others : %d   Total : %d\r", tcp ,  

udp , icmp  , others , total);   
}


void print_udp_packet(unsigned char *Buffer , int Size)
{     
 unsigned short iphdrlen;

 struct iphdr *iph = (struct iphdr *)(Buffer +  sizeof(struct ethhdr));
 iphdrlen = iph->ihl*4;

 struct udphdr *udph = (struct udphdr*)(Buffer + iphdrlen  + sizeof(struct ethhdr));

 int header_size =  sizeof(struct ethhdr) + iphdrlen + sizeof udph;

 fprintf(logfile , "\n\n***********************UDP Packet*************************\n");

 print_ip_header(Buffer,Size);          

 fprintf(logfile , "\nUDP Header\n");
 fprintf(logfile , "   |-Source Port      : %d\n" , ntohs(udph->source));
 fprintf(logfile , "   |-Destination Port : %d\n" , ntohs(udph->dest));
 fprintf(logfile , "   |-UDP Length       : %d\n" , ntohs(udph->len));
 fprintf(logfile , "   |-UDP Checksum     : %d\n" , ntohs(udph->check));

 fprintf(logfile , "\n");
 fprintf(logfile , "IP Header\n");
 PrintData(Buffer , iphdrlen);

 fprintf(logfile , "UDP Header\n");
 PrintData(Buffer+iphdrlen , sizeof udph);

 fprintf(logfile , "Data Payload\n");   

 //Move the pointer ahead and reduce the size of string
 PrintData(Buffer + header_size , Size - header_size);

 fprintf(logfile , "\n###########################################################");
}
}
}

我刚刚在这里包含了 udp 数据包。在 fprintf 语句中,我用于打印文件处理程序为“logfile”的文件中的所有数据包。我得到的输出是这样的

4

1 回答 1

3

例如,如果您需要将整个报表放在一个 Excel 单元格中,那么您可以将“放在开头,”放在该输出的末尾。然后您可以将其保存为 *.csv 文件并使用 Excel、LibreOffice.Calc 等打开它。因此在这种情况下(“在开头和结尾)您将在第一个单元格中获得所有这些输出:

在此处输入图像描述

如果您需要在单元格之间以不同的方式分布数据 - 使用逗号和“”:
“第一行,第一个单元格”,“第一行,第二个单元格”,...
“第二行,第一个单元格”,“第二行,第二个单元格“,
……

例如:

user@host $ cat new1.csv 
"first row, first cell", "first row, second cell"
"second row, first cell", "second row, second cell"

然后可以在 LibreOffice.Calc 中打开它,结果如下:
在此处输入图像描述

更新:为了将输出数据从问题传播到不同的单元格:
我将几个,"放入问题的原始报告中:

"This Report is from the Task agent whose IP is 127.0.0.1


 ***********************UDP Packet*************************"
"
Ethernet Header
|-Destination Address : 01-00-5E-00-00-02 
|-Source Address      : 00-00-0C-07-AC-3B 
|-Protocol            : 8 
","
IP Header
|-IP Version        : 4
|-IP Header Length  : 5 DWORDS or 20 Bytes
|-Type Of Service   : 192
|-IP Total Length   : 48  Bytes(Size of Packet)
|-Identification    : 0
|-TTL      : 1
|-Protocol : 17
|-Checksum : 61927
|-Source IP        : 172.16.59.3
|-Destination IP   : 224.0.0.2
","
UDP Header
|-Source Port      : 1985
|-Destination Port : 1985
|-UDP Length       : 28
|-UDP Checksum     : 42701
","
IP Header
01 00 5E 00 00 02 00 00 0C 07 AC 3B 08 00 45 C0         ..^........;..E.
00 30 00 00                                             .0..
UDP Header
00 00 01 11 F1 E7 AC 10                                 ........
Data Payload
00 00 10 03 0A 6E 3B 00 63 69 73 63 6F 00 00 00         .....n;.cisco...
AC 10 3B 01 00 00 00 00 00 00 00 00 00 00 00 00         ..;.............
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00         ................
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00         ................
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00         ................
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00         ................
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00         ................
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00         ................
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00         ................
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00         ................
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00         ................
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00         ................
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00         ................
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00         ................
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00         ................
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00         ................
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00         ................
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00         ................
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00         ................
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00         ................
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00         ................
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00         ................
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00         ................
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00         ................
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00         ................
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00         ................
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00         ................
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00         ................
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00         ................
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00         ................
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00         ................
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00         ................
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00         ................
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00         ................
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00         ................
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00         ................
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00         ................
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00         ................
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00         ................
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00         ................
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00         ................
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00         ................
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00         ................
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00         ................
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00         ................
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00         ................
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00         ................
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00         ................
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00         ................
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00         ................
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00         ................
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00         ................
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00         ................
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00         ................
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00         ................
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00         ................
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00         ................
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00         ................
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00         ................
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00         ................
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00         ................
00 00 00 00 00 00                                       ......

###########################################################"


它导致以下通过 excel 单元格传播:
在此处输入图像描述

于 2013-11-27T06:23:22.567 回答