枚举 192.55.22.0 到 192.56.22.0 域范围内的所有 IP 地址

发布于 2024-11-14 21:16:05 字数 150 浏览 7 评论 0原文

如何通过python整理从192.55.22.0到192.56.22.0的所有域名范围
我想在 IP 地址范围上执行一组网络任务。一旦范围变大,我就无法枚举该范围内的所有主机。我希望能够使用网络掩码迭代网络的所有主机 如果我将任何范围从一个范围传递到另一个范围以在范围之间显示

How can I sort out all the domain range from 192.55.22.0 to 192.56.22.0 through python
I want to perform a set of network tasks on an IP address range. As soon as the range is getting bigger, I fail to enumerate all hosts in the range. I want to be able to iterate through all hosts of a network with the net mask
If I pass any range from one to another to show between the range

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

万人眼中万个我 2024-11-21 21:16:05

你的例子有点令人困惑。 “192.55.22.0”和“192.56.22.0”之间是什么?您写了“使用网络掩码迭代网络的所有主机”,但该范围不是一个完整的网络。

在一般情况下,要从某个 ip 迭代到另一个 ip,您可以使用:

def ip_atoi(ip):
    return struct.unpack('!I', socket.inet_aton(ip))[0]
def ip_itoa(ip):
    return socket.inet_ntoa(struct.pack('!I', ip))

current = ip_atoi('192.55.22.0')
end = ip_atoi('192.56.22.0')

for ip in xrange(current, end):
    do_something_on(ip_itoa(ip))

如果在如此多的主机上快速执行网络操作时遇到问题,请查看 multiprocessing 中的 Pool模块。它可以通过同时处理几个主机来加速您的操作。

Your example is a bit confusing. What is between "192.55.22.0" and "192.56.22.0"? You wrote "iterate through all hosts of a network with the net mask", but that range is not one complete network.

In a general case, to iterate from some ip to another, you can use:

def ip_atoi(ip):
    return struct.unpack('!I', socket.inet_aton(ip))[0]
def ip_itoa(ip):
    return socket.inet_ntoa(struct.pack('!I', ip))

current = ip_atoi('192.55.22.0')
end = ip_atoi('192.56.22.0')

for ip in xrange(current, end):
    do_something_on(ip_itoa(ip))

If you have problems in performing the network operations quickly on so many hosts, have a look at Pool in the multiprocessing module. It might speed up your operations by processing a couple of hosts at once.

两仪 2024-11-21 21:16:05

使用纯 Python(和 IPv6 支持):

import socket,struct

# In Python 3+, you can simply use range
# In Python 2, you can substitute xrange if you're only dealing with IPv4
def _range(a, b):
    while a<b:
        yield a
        a+=1

def addrRange(start,end,fam=socket.AF_INET):
    famFmt = '!I' if fam == socket.AF_INET else '!Q'
    addr2int = lambda a: struct.unpack(famFmt, socket.inet_pton(fam, a))[0]

    for ai in _range(addr2int(start), addr2int(end)+1):
        yield socket.inet_ntop(fam, struct.pack(famFmt, ai))

for a in addrRange('192.55.22.0', '192.56.22.0'):
    print(a) # Or do something with the address

With pure Python (and IPv6 support):

import socket,struct

# In Python 3+, you can simply use range
# In Python 2, you can substitute xrange if you're only dealing with IPv4
def _range(a, b):
    while a<b:
        yield a
        a+=1

def addrRange(start,end,fam=socket.AF_INET):
    famFmt = '!I' if fam == socket.AF_INET else '!Q'
    addr2int = lambda a: struct.unpack(famFmt, socket.inet_pton(fam, a))[0]

    for ai in _range(addr2int(start), addr2int(end)+1):
        yield socket.inet_ntop(fam, struct.pack(famFmt, ai))

for a in addrRange('192.55.22.0', '192.56.22.0'):
    print(a) # Or do something with the address
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文