8
8
- updates the IP address on the interface
9
9
- displays the final IP information on the interface
10
10
11
- This script has been tested with Python 3.5 , however may work with other versions.
11
+ This script has been tested with Python 3.7 , however may work with other versions.
12
12
13
13
This script targets the RESTCONF DevNet Sandbox that leverages a CSR1000v as
14
14
a target. To execute this script against a different device, update the
26
26
import requests
27
27
import sys
28
28
import os
29
+ import ipaddress
29
30
from argparse import ArgumentParser
30
31
from collections import OrderedDict
31
32
from getpass import getpass
@@ -97,7 +98,7 @@ def configure_ip_address(url_base, interface, ip, username, password):
97
98
98
99
99
100
# Retrieve and print the current configuration of an interface
100
- def print_interface_details (url_base , interface , username , password ):
101
+ def print_interface_details (url_base , interface , username , password , cidr ):
101
102
url = url_base + "/interface={i}" .format (i = interface )
102
103
103
104
# this statement performs a GET on the specified url
@@ -116,10 +117,17 @@ def print_interface_details(url_base, interface, username, password):
116
117
# return the json as text
117
118
print ("Name: " , intf [0 ]["name" ])
118
119
try :
120
+ netmask = intf [0 ]["ietf-ip:ipv4" ]["address" ][0 ]["netmask" ]
121
+ if cidr :
122
+ nma = ipaddress .ip_address (netmask )
123
+ netmask = str ("{0:b}" .format (int (nma )).count ('1' ))
119
124
print ("IP Address: " , intf [0 ]["ietf-ip:ipv4" ]["address" ][0 ]["ip" ], "/" ,
120
- intf [ 0 ][ "ietf-ip:ipv4" ][ "address" ][ 0 ][ " netmask" ] )
125
+ netmask )
121
126
except KeyError :
122
127
print ("IP Address: UNCONFIGURED" )
128
+ except Exception as e :
129
+ print (e , file = sys .stderr )
130
+ sys .exit (1 )
123
131
print ()
124
132
125
133
return (intf )
@@ -142,12 +150,26 @@ def interface_selection(interfaces, mgmt_if):
142
150
return (sel )
143
151
144
152
145
- # Asks the user to provide an IP address and Mask. Data is NOT validated.
146
- def get_ip_info ():
153
+ # Asks the user to provide an IP address and Mask.
154
+ def get_ip_info (cidr ):
147
155
# Ask User for IP and Mask
148
156
ip = {}
149
- ip ["address" ] = input ("What IP address do you want to set? " )
150
- ip ["mask" ] = input ("What Subnet Mask do you want to set? " )
157
+ try :
158
+ if cidr :
159
+ ipa_t = input ("What IP address/prefixlen do you want to set? " )
160
+ ipi = ipaddress .ip_interface (ipa_t )
161
+ ip ["address" ] = ipi .ip .compressed
162
+ ip ["mask" ] = ipi .netmask .compressed
163
+ else :
164
+ ipa_t = input ("What IP address do you want to set? " )
165
+ ipi = ipaddress .ip_interface (ipa_t )
166
+ ip ["address" ] = ipi .ip .compressed
167
+ ipm_t = input ("What Subnet Mask do you want to set? " )
168
+ ipm = ipaddress .ip_address (ipm_t )
169
+ ip ["mask" ] = ipm .compressed
170
+ except Exception as e :
171
+ print (e , file = sys .stderr )
172
+ sys .exit (1 )
151
173
return (ip )
152
174
153
175
@@ -170,6 +192,8 @@ def main():
170
192
help = 'management interface' , default = 'GigabitEthernet1' )
171
193
parser .add_argument ('--port' , '-P' , type = int ,
172
194
help = 'sandbox web port' , default = 443 )
195
+ parser .add_argument ('--cidr' , help = 'use CIDR format for interface IP' ,
196
+ action = 'store_true' )
173
197
args = parser .parse_args ()
174
198
175
199
password = os .getenv ('DEVNET_RESTCONF_PASSWORD' )
@@ -196,18 +220,18 @@ def main():
196
220
# Print Starting Interface Details
197
221
print ("Starting Interface Configuration" )
198
222
print_interface_details (url_base , selected_interface , args .username ,
199
- password )
223
+ password , args . cidr )
200
224
201
225
# As User for IP Address to set
202
- ip = get_ip_info ()
226
+ ip = get_ip_info (args . cidr )
203
227
204
228
# Configure interface
205
229
configure_ip_address (url_base , selected_interface , ip , args .username , password )
206
230
207
231
# Print Ending Interface Details
208
232
print ("Ending Interface Configuration" )
209
233
print_interface_details (url_base , selected_interface , args .username ,
210
- password )
234
+ password , args . cidr )
211
235
212
236
if __name__ == '__main__' :
213
237
sys .exit (main ())
0 commit comments