文本分析的If语句困境

2024-10-01 22:38:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在解析一个大文本文件以获得正确的TCP连接,我需要做的一件事是检查服务器和主机IP地址(有30个不同的主机)是否已向对方发送了一个SYN数据包。你知道吗

主机列表:

HostList1 =   ['150.100.12.130','150.100.12.131','150.100.12.132','150.100.12.133','150.100.12.134','150.100.12.135','150.100.12.136','150.100.12.137','150.100.12.138','150.100.12.139']  

文本中的示例行(有数千行):

2.000724 IP (tos 0x0, ttl  63, id 0, offset 0, flags [none], proto: TCP (6), length: 40, bad cksum 0 (->4280)!) 150.100.12.134.49153 > 150.100.0.2.57300: S, cksum 0x0000 (incorrect (-> 0xd6bb), 0:0(0) win 65535
2.000724 IP (tos 0x0, ttl  64, id 17, offset 0, flags [none], proto: TCP (6), length: 40, bad cksum 0 (->416f)!) 150.100.0.2.57300 > 150.100.12.134.49153: S, cksum 0x0000 (incorrect (-> 0xd6aa), 0:0(0) ack 1 win 65535

我的当前代码,服务器地址是150.100.0.2:

with open("serverLanTraceWithErrors.txt" , "r") as ParseFile1:
   for line in ParseFile1:
     if 'TCP' in line:               #Check if TCP proto is in the line
      Split1 = line.split(",")       #Split each line by a comma for future splits                                       
      Split2 = Split1[7].split(">")  #Split1[7] contains the two IP addresses, host and server and the direction of the packet
      Split3 = Split1[1].split(" ")  #Split3[3] will show the # hops 

      for item in HostList1:
          if 'S' in line:            #check if SYN flag is in the line

            if item in Split2[1] and '150.100.0.2' in Split2[2] and '63' in Split3[3]:
               print("SYN packet from host computer: %s to server completed with right # of hops %s" %(item, Split3[3]))

            elif item in Split2[2] and  '150.100.0.2' in Split2[1] and '64' in Split3[3] and 'ack' in Split1[9]:
               print("SYN packet from server to host computer: %s completed with right # of hops %s" %(item, Split3[3])) 

            elif: item hasn't been found sending or receving SYN flag...
               print("SYN pack from wherever to wherever not completed...")

理想情况下,应该有两种情况,在服务器和主机之间找到SYN标志。主机到服务器,服务器回到主机。我的问题是,我找不到方法使最后一个elif语句工作,我需要一种方法来说明,如果两个最上面的语句中只有一个出现过,那么它出现在哪个IP地址上。因为对于主机150.100.12.139,它只向主机发送一个数据包,主机从不回复一个数据包,所以我使用代码来指出这一点。我对python的了解非常有限,所以我在这里呆了一段时间。你知道吗


Tags: andtheinip服务器iflineitem
1条回答
网友
1楼 · 发布于 2024-10-01 22:38:49

您说过“理想情况下,应该有两种情况,在服务器和主机之间找到SYN标志”,因此我猜最后一个elif条件是检查上述任一条件是否有效。我建议不要使用最后的elif语句,而是尝试使用else,如下所示。你知道吗

for item in HostList1:
          if 'S' in line:            #check if SYN flag is in the line

            if item in Split2[1] and '150.100.0.2' in Split2[2] and '63' in Split3[3]:
               print("SYN packet from host computer: %s to server completed with right # of hops %s" %(item, Split3[3]))

            elif item in Split2[2] and  '150.100.0.2' in Split2[1] and '64' in Split3[3] and 'ack' in Split1[9]:
               print("SYN packet from server to host computer: %s completed with right # of hops %s" %(item, Split3[3])) 

            else:  ## item hasn't been found sending or receving SYN flag...
               print("SYN pack from wherever to wherever not completed...")

如果对你有帮助就告诉我。你知道吗

编辑1: 在研究了这些评论之后,我建议你

            if item in Split2[1] and '150.100.0.2' in Split2[2] and '63' in Split3[3]:
               print("SYN packet from host computer: %s to server completed with right # of hops %s" %(item, Split3[3]))

            elif item in Split2[2] and  '150.100.0.2' in Split2[1] and '64' in Split3[3] and 'ack' in Split1[9]:
               print("SYN packet from server to host computer: %s completed with right # of hops %s" %(item, Split3[3])) 

            elif item not in Split2[1] or item in Split2[2] :  ## item hasn't been found sending or receving SYN flag...
               print("SYN pack from wherever to wherever not completed...")

相关问题 更多 >

    热门问题