2626import requests
2727import sys
2828import os
29+ import ipaddress
2930from argparse import ArgumentParser
3031from collections import OrderedDict
3132from getpass import getpass
@@ -97,7 +98,7 @@ def configure_ip_address(url_base, interface, ip, username, password):
9798
9899
99100# 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 ):
101102 url = url_base + "/interface={i}" .format (i = interface )
102103
103104 # this statement performs a GET on the specified url
@@ -116,10 +117,17 @@ def print_interface_details(url_base, interface, username, password):
116117 # return the json as text
117118 print ("Name: " , intf [0 ]["name" ])
118119 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 (nma ._ip ).count ('1' ))
119124 print ("IP Address: " , intf [0 ]["ietf-ip:ipv4" ]["address" ][0 ]["ip" ], "/" ,
120- intf [ 0 ][ "ietf-ip:ipv4" ][ "address" ][ 0 ][ " netmask" ] )
125+ netmask )
121126 except KeyError :
122127 print ("IP Address: UNCONFIGURED" )
128+ except Exception as e :
129+ print (e , file = sys .stderr )
130+ sys .exit (1 )
123131 print ()
124132
125133 return (intf )
@@ -142,12 +150,23 @@ def interface_selection(interfaces, mgmt_if):
142150 return (sel )
143151
144152
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 ):
147155 # Ask User for IP and Mask
148156 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 , ipl ) = input ("What IP address/prefixlen do you want to set? " ).split ('/' )
160+ ipa = ipaddress .ip_address (ipa_t )
161+ ip ["address" ] = ipa .compressed
162+ ip ["mask" ] = ipa ._make_netmask (int (ipl ))[0 ].compressed
163+ else :
164+ ipa_t = input ("What IP address do you want to set? " )
165+ ip ["address" ] = ipaddress .ip_address (ipa_t ).compressed
166+ ip ["mask" ] = input ("What Subnet Mask do you want to set? " )
167+ except Exception as e :
168+ print (e , file = sys .stderr )
169+ sys .exit (1 )
151170 return (ip )
152171
153172
@@ -170,6 +189,8 @@ def main():
170189 help = 'management interface' , default = 'GigabitEthernet1' )
171190 parser .add_argument ('--port' , '-P' , type = int ,
172191 help = 'sandbox web port' , default = 443 )
192+ parser .add_argument ('--cidr' , help = 'use CIDR format for interface IP' ,
193+ action = 'store_true' )
173194 args = parser .parse_args ()
174195
175196 password = os .getenv ('DEVNET_RESTCONF_PASSWORD' )
@@ -196,18 +217,18 @@ def main():
196217 # Print Starting Interface Details
197218 print ("Starting Interface Configuration" )
198219 print_interface_details (url_base , selected_interface , args .username ,
199- password )
220+ password , args . cidr )
200221
201222 # As User for IP Address to set
202- ip = get_ip_info ()
223+ ip = get_ip_info (args . cidr )
203224
204225 # Configure interface
205226 configure_ip_address (url_base , selected_interface , ip , args .username , password )
206227
207228 # Print Ending Interface Details
208229 print ("Ending Interface Configuration" )
209230 print_interface_details (url_base , selected_interface , args .username ,
210- password )
231+ password , args . cidr )
211232
212233if __name__ == '__main__' :
213234 sys .exit (main ())
0 commit comments