简体   繁体   English

为什么此python代码在Linux中有效,但在Windows中不起作用?

[英]Why does this python code work in linux but not windows?

I'm trying to check the location of a captured packet file. 我正在尝试检查捕获的数据包文件的位置。 It works perfectly fine if I run it in ubuntu, but if I switch and run it in windows every time it hits a IPv6 packet it stops. 如果我在ubuntu中运行它,则可以正常工作,但是如果我每次在Windows中切换并运行它时,它遇到的IPv6数据包都会停止运行。 I'm wanting it to just skip it and go on to the next packet like it does in ubuntu but it doesn't. 我希望它像ubuntu中那样跳过它并继续到下一个数据包,但事实并非如此。 It just stops the for loop whenever it hits the v6 IP addy. 只要碰到v6 IP addy,它就会停止for循环。

Any ideas how to fix this? 任何想法如何解决这一问题?

def printPcap(pcap):
    for (ts, buf) in pcap:
        try:
            eth = dpkt.ethernet.Ethernet(buf)
            ip = eth.data
            src = socket.inet_ntoa(ip.src)
            dst = socket.inet_ntoa(ip.dst)
            print '[+] Src: ' + src + ' --> Dst: ' + dst
            print '[+] Src: ' + retGeoStr(src) + ' --> Dst: ' + retGeoStr(dst) + '\n'
        except:
            pass

If I print out the error the except catches it prints: 如果我打印出错误,则打印出来的除外:

Packet IP wrong length for inet_ntoa inet_ntoa的数据包IP长度错误

I'm pretty sure this is because its the IPv6 which then I would expect it to go on to the next packet, but it also prints out this error: 我很确定这是因为它是IPv6,然后我希望它继续到下一个数据包,但它还会打印出此错误:

'str' object has no attribute 'src' 'str'对象没有属性'src'

I think this is what is causing my problem. 我认为这就是造成我问题的原因。

Like I said it will work fine up until the point it hits that v6 address and it works fine on ubuntu. 就像我说的那样,它将一直工作到到达v6地址为止,并且在ubuntu上也可以正常工作。 I'm puzzled. 我很困惑

Packet IP wrong length for inet_ntoa inet_ntoa的数据包IP长度错误

inet_ntoa is for IPv4 only. inet_ntoa仅适用于IPv4。 The reason it works on one system and not on the other is probably because you get IPv6 packets only on one of the systems. 它在一个系统上而不在另一个系统上起作用的原因可能是因为您仅在其中一个系统上获得IPv6数据包。

From the documentation at https://docs.python.org/2/library/socket.html 从位于https://docs.python.org/2/library/socket.html的文档中

socket.inet_ntoa(packed_ip) Convert a 32-bit packed IPv4 address (a string four characters in length) to its standard dotted-quad string representation (for example, '123.45.67.89'). socket.inet_ntoa(packed_ip)将32位压缩的IPv4地址(长度为四个字符的字符串)转换为其标准的点分四进制字符串表示形式(例如'123.45.67.89')。 This is useful when conversing with a program that uses the standard C library and needs objects of type struct in_addr, which is the C type for the 32-bit packed binary data this function takes as an argument. 与使用标准C库并需要struct in_addr类型的对象的程序进行对话时,此功能很有用。该对象是此函数作为参数的32位打包二进制数据的C类型。

If the string passed to this function is not exactly 4 bytes in length, socket.error will be raised. 如果传递给此函数的字符串长度不完全是4个字节,则会引发socket.error。 inet_ntoa() does not support IPv6, and inet_ntop() should be used instead for IPv4/v6 dual stack support. inet_ntoa()不支持IPv6,而应使用inet_ntop()来支持IPv4 / v6双栈。

Note that the socket library do not have the inet_ntop function under windows. 请注意,套接字库在Windows下没有inet_ntop函数。 only Linux. 仅Linux。 You can use the IPy Python library under windows for taking care of IPv6 addresses this way: 您可以通过以下方式在Windows下使用IPy Python库来处理IPv6地址:

ip = eth.data
src = IPy.IP(ip.src.encode('hex'))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM