cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
527
Views
5
Helpful
6
Replies

Change ip address expression

zshowip
Enthusiast
Enthusiast

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

 

2 Accepted Solutions

Accepted Solutions

Seb Rupik
VIP Advisor VIP Advisor
VIP Advisor

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.

 

View solution in original post

balaji.bandi
VIP Community Legend VIP Community Legend
VIP Community Legend

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)

BB

***** Rate All Helpful Responses *****

How to Ask The Cisco Community for Help

View solution in original post

6 Replies 6

Georg Pauwen
VIP Master VIP Master
VIP Master

Hello,

is this a duplicate post ? Since you accepted a solution to the same question below, is the problem still existing ?

Seb Rupik
VIP Advisor VIP Advisor
VIP Advisor

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.

 

zshowip
Enthusiast
Enthusiast

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

balaji.bandi
VIP Community Legend VIP Community Legend
VIP Community Legend

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)

BB

***** Rate All Helpful Responses *****

How to Ask The Cisco Community for Help

zshowip
Enthusiast
Enthusiast

 

COOL, thank you!!

 

zshowip
Enthusiast
Enthusiast

@balaji.bandi 

"import re" in your comment, how can we get "re" ? what does "re" look like?

 

 

 

Getting Started

Find answers to your questions by entering keywords or phrases in the Search bar above. New here? Use these resources to familiarize yourself with the community:

Recognize Your Peers