python - 找到未使用的IP /找到一个未在nos范围内使用的数字?

时间:2016-06-29 15:24:38

标签: python

我有一个IP地址列表(或只是重要的数字)

示例列表:

192.168.1.1
192.168.1.2
192.168.1.4
192.168.1.5
192.168.1.7
192.168.1.9

我想要的是一个能够显示某个范围内哪些IP /数字是免费的功能。

例如,如果我的范围是192.168.1.1-10,那么.3,.6,.8和.10将可用,那些可用的nos我想要返回最低编号

到目前为止,我有我的IP列表,我使用正则表达式和拆分来获取最后一个八位字节/数字,但不知道接下来该做什么?

def getXOctect(ip_address,octect):
    octects = re.split('(.*)\.(.*)\.(.*)\.(.*)', ip_address)
    return octects[octect]

list_of_ips = ['192.168.1.1','192.168.1.2','192.168.1.4','192.168.1.5','192.168.1.7','192.168.1.9']
octect_list = []
for item in list_of_ips:
 octect_list.append(getXOctect(item,4))
#now what?

2 个答案:

答案 0 :(得分:1)

你可以试试像

这样的东西
usable = [] 
for i in range(1,255): # or whichever are the available ports
    if i not in octect_list: 
        usable.append(i) 
min(usable) # min value of the unused ports

答案 1 :(得分:0)

所以,如果我理解你,你会想要在你的例子中返回192.168.1.3吗?如果您知道您的数字将会是什么样子(如所有表格192.168.1.x),您可能只需从1开始迭代并检查您的号码是否免费。如果是,则返回,否则递增并重复。