Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何使用Scapy获取接收到的最后一个数据包的信息_Python_Packet_Scapy_Packets - Fatal编程技术网

Python 如何使用Scapy获取接收到的最后一个数据包的信息

Python 如何使用Scapy获取接收到的最后一个数据包的信息,python,packet,scapy,packets,Python,Packet,Scapy,Packets,我正在使用Scapy的srp1()将pcap文件重放到设备上,如下所示: for p in rdpcap(pcapfile): ... rcv = srp1(p, 'eth0') print rcv[IP].len print rcv[TCP].seq ... for p in rdpcap(pcapfile): ... answers, unanswered = srp(p, 'eth0') last_request, last

我正在使用Scapy的
srp1()
将pcap文件重放到设备上,如下所示:

for p in rdpcap(pcapfile):
    ...
    rcv = srp1(p, 'eth0')
    print rcv[IP].len
    print rcv[TCP].seq
    ...
for p in rdpcap(pcapfile):
    ...
    answers, unanswered = srp(p, 'eth0')
    last_request, last_answer = answers[-1]
    print last_answer[IP].len
    print last_answer[TCP].seq
    ...
当设备发送1个数据包时,我可以获取其
IP.len
TCP.seq
,但当设备发送2个数据包时,我只能获取第一个数据包的信息,而我需要第二个数据包的信息

我哪里出错了?

两者都有,并声明
srp1()
srp()
的一个变体,它只返回构成发送数据包答案的第一个数据包

因此,请尝试使用
srp()
而不是
srp1()
,如下所示:

for p in rdpcap(pcapfile):
    ...
    rcv = srp1(p, 'eth0')
    print rcv[IP].len
    print rcv[TCP].seq
    ...
for p in rdpcap(pcapfile):
    ...
    answers, unanswered = srp(p, 'eth0')
    last_request, last_answer = answers[-1]
    print last_answer[IP].len
    print last_answer[TCP].seq
    ...
和都表示
srp1()
srp()
的一个变体,它只返回构成已发送数据包应答的第一个数据包

因此,请尝试使用
srp()
而不是
srp1()
,如下所示:

for p in rdpcap(pcapfile):
    ...
    rcv = srp1(p, 'eth0')
    print rcv[IP].len
    print rcv[TCP].seq
    ...
for p in rdpcap(pcapfile):
    ...
    answers, unanswered = srp(p, 'eth0')
    last_request, last_answer = answers[-1]
    print last_answer[IP].len
    print last_answer[TCP].seq
    ...

我尝试了您的解决方案,但出现了以下错误“AttributeError:'list'对象没有属性'seq'”。我打印了收到的数据包,它看起来是这样的:“[]”您确定提取了最后收到的答案,即,
rcv=srp(p,'eth0')[-1]
而不是
rcv=srp(p,'eth0'))
?在引发错误之前,
rcv的值是多少?rcv=srp(p,'eth0')[-1]打印rcv这是结果:[]“…”表示其他属性last\u answer=answers[-1],其中一些数据包返回“indexer:列表索引超出范围”和last\u answer[IP]。len return“TypeError:元组索引必须是整数,而不是Packet_元类”我尝试了您的解决方案,但出现了此错误“AttributeError:'list'对象没有属性'seq'”我打印了收到的数据包,它看起来是这样的:“[]”您确定提取了最后收到的答案,即,
rcv=srp(p,'eth0')[-1]
而不是
rcv=srp(p,'eth0')
?在引发错误之前,
rcv
的值是多少?rcv=srp(p,'eth0')[-1]打印rcv这是结果:[]“…”表示其他属性last\u answer=answers[-1],其中一些数据包返回“索引器:列表索引超出范围”和last\u answer[IP]。len return”TypeError:元组索引必须是整数,而不是数据包\元类“