08-16-2022 02:52 PM
Hi We have devices ip address like this 10.1.1.2-4. Anyone can help to show python commands to let ip address like 10.1.1.2, 10.1.1.3, 10.1.1.4 ? or below, Thank you
10.1.1.2,
10.1.1.3,
10.1.1.4
Solved! Go to Solution.
08-17-2022 03:42 AM
Hi there,
See below for a first pass at an implementation, only limitation is it that it checks just that last octet for expansion:
IPs = ['192.168.1.1', '192.168.2.1-10']
expanded_ip_list = []
for ip in IPs:
if '-' in ip.split('.')[3]:
prefix = '.'.join(ip.split('.')[:3])
high_low = ip.split('.')[3].split('-')
r = range(int(high_low[0]), int(high_low[1])+1, 1)
for x in r:
expanded_ip_list.append('{0}.{1}'.format(prefix,x))
else:
expanded_ip_list.append(ip)
print(expanded_ip_list)
gives:
['192.168.1.1', '192.168.2.1', '192.168.2.2', '192.168.2.3', '192.168.2.4', '192.168.2.5', '192.168.2.6', '192.168.2.7', '192.168.2.8', '192.168.2.9', '192.168.2.10']
cheers,
Seb.
08-17-2022 01:42 PM
Adding to other option as below :
import re
ip='10.1.1.2-4'
x=re.split('-',ip)
y=re.split('\.',x[0])
first=int(y[3])
last=int(x[1])
list_ip=[]
for i in range(first,last+1):
temp=y[0]+'.'+ y[1]+'.'+y[2]+'.'+ str(i)
list_ip.append(temp)
print(list_ip)
08-16-2022 11:17 PM
Hello,
is this a duplicate post ? Since you accepted a solution to the same question below, is the problem still existing ?
08-17-2022 03:42 AM
Hi there,
See below for a first pass at an implementation, only limitation is it that it checks just that last octet for expansion:
IPs = ['192.168.1.1', '192.168.2.1-10']
expanded_ip_list = []
for ip in IPs:
if '-' in ip.split('.')[3]:
prefix = '.'.join(ip.split('.')[:3])
high_low = ip.split('.')[3].split('-')
r = range(int(high_low[0]), int(high_low[1])+1, 1)
for x in r:
expanded_ip_list.append('{0}.{1}'.format(prefix,x))
else:
expanded_ip_list.append(ip)
print(expanded_ip_list)
gives:
['192.168.1.1', '192.168.2.1', '192.168.2.2', '192.168.2.3', '192.168.2.4', '192.168.2.5', '192.168.2.6', '192.168.2.7', '192.168.2.8', '192.168.2.9', '192.168.2.10']
cheers,
Seb.
08-17-2022 06:12 AM
Thanks all for your reply
@Georg Pauwen its partially resolved. Based on my understand to that reply, my question was a little too bigger, so that I posted unresolved part in this thread.
@Seb Rupik It looks work. let me try it
08-17-2022 01:42 PM
Adding to other option as below :
import re
ip='10.1.1.2-4'
x=re.split('-',ip)
y=re.split('\.',x[0])
first=int(y[3])
last=int(x[1])
list_ip=[]
for i in range(first,last+1):
temp=y[0]+'.'+ y[1]+'.'+y[2]+'.'+ str(i)
list_ip.append(temp)
print(list_ip)
08-18-2022 01:55 PM - edited 08-18-2022 02:44 PM
COOL, thank you!!
08-22-2022 07:00 AM - edited 08-22-2022 07:01 AM
Discover and save your favorite ideas. Come back to expert answers, step-by-step guides, recent topics, and more.
New here? Get started with these tips. How to use Community New member guide