Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/312.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.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:包头中显示了错误的ip_Python_Sockets_Networking_Ip_Packet - Fatal编程技术网

Python:包头中显示了错误的ip

Python:包头中显示了错误的ip,python,sockets,networking,ip,packet,Python,Sockets,Networking,Ip,Packet,我正在通过我的传送一台机器的数据包。我测试了tcpdump主机,看看是否一切都正常,确实如此。现在我需要捕获这些数据包。我选择用Python来实现这一点。现在我正在尝试过滤数据包,但是打印的ip与正确的不同。它应该是192.168.0.8: import socket from struct import * print("Started") with socket.socket(socket.AF_PACKET,socket.SOCK_RAW, socket.ntohs(0x0003)) as

我正在通过我的传送一台机器的数据包。我测试了
tcpdump主机
,看看是否一切都正常,确实如此。现在我需要捕获这些数据包。我选择用Python来实现这一点。现在我正在尝试过滤数据包,但是打印的ip与正确的不同。它应该是
192.168.0.8

import socket
from struct import *
print("Started")
with socket.socket(socket.AF_PACKET,socket.SOCK_RAW, socket.ntohs(0x0003)) as s:
        while True:
                packet=s.recvfrom(65565)
                content=packet[0]
                ip_header=unpack('!BBHHHBBH4s4s', content[:20])
                source_ip=socket.inet_ntoa(ip_header[8])
                print(source_ip)

打印的是
8.0.69.0
8.0.69.16
,它们都不符合预期的格式。

这是因为原始数据前面是MAC头

如果更改行:

ip_header=unpack(“!bbhhbbh4s4s”,内容[:20])

ip_header=unpack('!bbhhbbh4s4s',content[14:34])


你可能会得到你的ip地址。我说可能是因为它确实取决于链路层,因为可能存在vlan标记,因此ip头会进一步移动

Thx,我设法让它与你提供的信息一起工作。