2014-12-22 2 views
2

PacketlisField 패킷을 만드는 방법이 궁금합니다.패킷 인수 필드에 scapy 전달 인수

의 인스턴스 인 Packet을 공예로 만들고 plist에 값을 전달하고 싶습니다.

class TestPkt(Packet): 
    fields_desc = [ ByteField("f1",65), 
        ShortField("f2",0x4244) ] 
    def extract_padding(self, p): 
     return "", p 

class TestPLF2(Packet): 
    fields_desc = [ FieldLenField("len1", None, count_of="plist",fmt="H", adjust=lambda pkt,x:x+2), 
        FieldLenField("len2", None, length_of="plist",fmt="I", adjust=lambda pkt,x:(x+1)/2), 
        PacketListField("plist", None, TestPkt, length_from=lambda x:(x.len2*2)/3*3) ] 

>>> pkt=TestPLF2() 
>>> pkt.show() 
###[ TestPLF2 ]### 
    len1= None 
    len2= None 
    \plist\ 

>>> pkt=TestPLF2(['f1=75,f2=76']) 
Traceback (most recent call last): 
    File "<console>", line 1, in <module> 
    File "/usr/local/lib64/python2.6/site-packages/scapy/base_classes.py", line 199, in __call__ 
    i.__init__(*args, **kargs) 
    File "/usr/local/lib64/python2.6/site-packages/scapy/packet.py", line 80, in __init__ 
    self.dissect(_pkt) 
    File "/usr/local/lib64/python2.6/site-packages/scapy/packet.py", line 579, in dissect 
    s = self.do_dissect(s) 
    File "/usr/local/lib64/python2.6/site-packages/scapy/packet.py", line 553, in do_dissect 
    s,fval = f.getfield(self, s) 
    File "/usr/local/lib64/python2.6/site-packages/scapy/fields.py", line 74, in getfield 
    return s[self.sz:], self.m2i(pkt, struct.unpack(self.fmt, s[:self.sz])[0]) 
error: unpack requires a string argument of length 2 
>>> 

답변

0

당신이 원하는 무엇에 대한 구문은 다음과 같습니다

>>> pkt=TestPLF2(plist=[TestPkt(f1=75,f2=76)]) 
>>> pkt.show() 
###[ TestPLF2 ]### 
    len1= None 
    len2= None 
    \plist\ 
    |###[ TestPkt ]### 
    | f1= 75 
    | f2= 76