Skip to content

Commit fadf9a6

Browse files
authored
Merge pull request #93 from gregbo/patch-1
* Add CIDR support * Validate the interface IP
2 parents afcf72a + 40b433e commit fadf9a6

File tree

2 files changed

+36
-12
lines changed

2 files changed

+36
-12
lines changed

restconf_update_ipaddress/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ This Python script leverages RESTCONF to:
1010
- updates the IP address on the interface
1111
- displays the final IP information on the interface
1212

13-
This script has been tested with Python 3.5, however may work with other versions.
13+
This script has been tested with Python 3.7, however may work with other versions.
1414

1515
## DevNet Sandbox
1616

@@ -38,7 +38,7 @@ Python
3838
* Create and activate a virtualenv
3939

4040
```bash
41-
virtualenv venv --python=python3.5
41+
virtualenv venv --python=python3.7
4242
source venv/bin/activate
4343
```
4444

restconf_update_ipaddress/updateip.py

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
- updates the IP address on the interface
99
- displays the final IP information on the interface
1010
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.
1212
1313
This script targets the RESTCONF DevNet Sandbox that leverages a CSR1000v as
1414
a target. To execute this script against a different device, update the
@@ -26,6 +26,7 @@
2626
import requests
2727
import sys
2828
import os
29+
import ipaddress
2930
from argparse import ArgumentParser
3031
from collections import OrderedDict
3132
from 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(int(nma)).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,26 @@ 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 = 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)
151173
return(ip)
152174

153175

@@ -170,6 +192,8 @@ def main():
170192
help='management interface', default='GigabitEthernet1')
171193
parser.add_argument('--port', '-P', type=int,
172194
help='sandbox web port', default=443)
195+
parser.add_argument('--cidr', help='use CIDR format for interface IP',
196+
action='store_true')
173197
args = parser.parse_args()
174198

175199
password = os.getenv('DEVNET_RESTCONF_PASSWORD')
@@ -196,18 +220,18 @@ def main():
196220
# Print Starting Interface Details
197221
print("Starting Interface Configuration")
198222
print_interface_details(url_base, selected_interface, args.username,
199-
password)
223+
password, args.cidr)
200224

201225
# As User for IP Address to set
202-
ip = get_ip_info()
226+
ip = get_ip_info(args.cidr)
203227

204228
# Configure interface
205229
configure_ip_address(url_base, selected_interface, ip, args.username, password)
206230

207231
# Print Ending Interface Details
208232
print("Ending Interface Configuration")
209233
print_interface_details(url_base, selected_interface, args.username,
210-
password)
234+
password, args.cidr)
211235

212236
if __name__ == '__main__':
213237
sys.exit(main())

0 commit comments

Comments
 (0)