Answer the question
In order to leave comments, you need to log in
Python. Ugly While construction. Can it be simplified?
There is a function:
def wildcard_iprange(ipaddr, sw):
if sw:
beginning = [ int(i) if not '-' in i else int(i.split('-')[0]) for i in ipaddr.split('.') ]
e = [ int(i) if not '-' in i else int(i.split('-')[1]) for i in ipaddr.split('.') ]
b = beginning[:]
else:
beginning = [ int(i) if i != '*' else 0 for i in ipaddr.split('.') ]
e = [ int(i) if i != '*' else 255 for i in ipaddr.split('.') ]
b = beginning[:]
while b[0] <= e[0]:
while b[1] <= e[1]:
while b[2] <= e[2]:
while b[3] <= e[3]:
yield '%s.%s.%s.%s' % (b[0], b[1], b[2], b[3])
b[3] = b[3] + 1
b[2] = b[2] + 1
b[3] = beginning[3]
b[1] = b[1] + 1
b[2] = beginning[2]
b[0] = b[0] + 1
b[1] = beginning[1]
Answer the question
In order to leave comments, you need to log in
Your noodles are notable ..
I understand correctly that you just need to display all ip by mask?
import itertools
s = '197.1-10.197.*'
sections = s.split('.')
addresses = []
for i in sections:
if '-' in i:
min,max = i.split('-')
a = [x for x in range(int(min),int(max))]
elif '*' in i:
a = [x for x in range(0,255)]
else:
a = [int(i)]
addresses.append(a)
addresses = itertools.product(*addresses)
for i in addresses:
print i
The head does not work after a working day, but in my opinion you can try something like this:
for num in range(0, 4):
while b[num] <= e[num]:
# yield
b[num] += 1
if b[num] <= e[num]:
b[num + 1] = beggining[num + 1]
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question