相关文章推荐
痛苦的热水瓶  ·  树莓派 ...·  1 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I am using Scapy to craft and sniff packets. I am trying to get last payload index of the sniffing packet. For example:

p=IPv6()/UDP()/DHCPv6_Solicit()

DHPCv6_Solicit() will be the last payload and the index for it is 2

p[2] #print DHCPv6_Solicit() object

I tried to get the last index by using -1 but the Scapy gave me this error

Traceback (most recent call last):
   File "/usr/lib/python3.5/code.py", line 91, in runcode
   exec(code, self.locals)
   File "<console>", line 1, in <module>
   File "/usr/local/lib/python3.5/dist-packages/scapy/packet.py", line 966, in __getitem__
   raise IndexError("Layer [%s] not found" % lname)
IndexError: Layer [-1] not found

I tried to get length of the array by using

len(p2)

but Scapy showed the length of the packet in bytes So how can I find the last payload index of the Scapy packet?

You could also use packet.layers() and use list indexing the way you were expecting:

>>> p.layers()
[<class 'scapy.layers.inet6.IPv6'>, <class 'scapy.layers.inet.UDP'>, <class 'scapy.layers.dhcp6.DHCP6_Solicit'>]
>>> p.layers()[-1]
<class 'scapy.layers.dhcp6.DHCP6_Solicit'>
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.