简体   繁体   English

如何遍历IP范围?

[英]How to loop through an IP range?

What's the easiest way to loop through an IP range in python? 在python中遍历IP范围的最简单方法是什么? I saw a couple of examples but none of them worked for me. 我看到了几个示例,但没有一个对我有用。 I tried the following technique, works perfectly, but it will not exit the loop when it reaches the end of the range. 我尝试了以下技术,效果很好,但是当达到范围末端时,它不会退出循环。 For example, I will specify the following arguments via the command line 例如,我将通过命令行指定以下参数

python loop.py 1.0.0.0 2.0.0.0 

and the loop will not stop execution when the range reaches 2.0.0.0 当范围达到2.0.0.0时,循环不会停止执行

start_ip = string.split(sys.argv[1], ".")
end_ip = string.split(sys.argv[2], ".")

for c1 in range(int(start_ip[0]), 255):
    for c2 in range(int(start_ip[1]), 255):
        for c3 in range(int(start_ip[2]), 255):
            for c4 in range(int(start_ip[3]), 255):
                if "%d.%d.%d.%d" % (c1, c2, c3, c4) == "%d.%d.%d.%d" % (int(end_ip[0]), int(end_ip[1]), int(end_ip[2]), int(end_ip[3])):
                    break
                else:
                    print "%d.%d.%d.%d" % (c1, c2, c3, c4)

It would be great if someone could tell me why my code isn't working correctly, or show me a better way to iterate through an IP range, thanks. 如果有人可以告诉我为什么我的代码无法正常工作,或者向我展示一种遍历IP范围的更好方法,那就太好了,谢谢。 All help is appreciated. 感谢所有帮助。

Your looping logic is wrong. 您的循环逻辑是错误的。 At the if statement where you do the comparison with the upper bound of the IP address, the break statement only breaks the immediate loop above the if statement. 在使用IP地址上限进行比较的if语句中, break语句仅中断if语句上方的立即循环。

Therefore it continues to run for the whole range. 因此,它将继续在整个范围内运行。 You need to break from each of the for loops. 您需要打破每个for循环。 Something like this : 像这样的东西:

done = False

for c1 in range(int(start_ip[0]), 255):
    for c2 in range(int(start_ip[1]), 255):
        for c3 in range(int(start_ip[2]), 255):
            for c4 in range(int(start_ip[3]), 255):
                if "%d.%d.%d.%d" % (c1, c2, c3, c4) == "%d.%d.%d.%d" % (int(end_ip[0]), int(end_ip[1]), int(end_ip[2]), int(end_ip[3])):
                    done = True
                    break
                else:
                    print "%d.%d.%d.%d" % (c1, c2, c3, c4)
            if(done):
                break
        if(done):
            break
    if(done):
        break

As an IP is just 4 bytes, it can be expressed as a single 32-bit integer. 由于IP只有4个字节,因此可以将其表示为单个32位整数。 That way, if we can transform from and to this integer, we only need one loop: 这样,如果我们可以从该整数转换为该整数,则只需要一个循环:

# How much is each byte 'worth' in the ip?
units = [1 << (8 * i) for i in range(3, -1, -1)]


def ip_to_int(ip):
    return sum(int(byte) * unit for (byte, unit) in zip(ip.split('.'), units))


def int_to_ip(i):
    return '.'.join(str((i / bit) & 0xff) for bit in units)

#----------------------
start_ip = "1.0.0.0"
end_ip = "2.0.0.0"

ips = (int_to_ip(i) for i in xrange(ip_to_int(start_ip), ip_to_int(end_ip)))
for ip in ips:
    print ip

This makes the looping really easy, and also gives you easy ways to see if one IP is bigger than another or equal to it (just compare the ints). 这使循环变得非常容易,并且还为您提供了一种简单的方法来查看一个IP是否大于另一个IP或等于它(只是比较整数)。

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

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