-
Notifications
You must be signed in to change notification settings - Fork 453
/
Copy pathupdateip.py
237 lines (195 loc) · 8.22 KB
/
updateip.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#!/usr/bin/env python
"""
This Python script leverages RESTCONF to:
- retrieve a list of interfaces on a device
- ask the user for the interface to configure
- displays the interface IP information
- asks user for new IP information
- updates the IP address on the interface
- displays the final IP information on the interface
This script has been tested with Python 3.7, however may work with other versions.
This script targets the RESTCONF DevNet Sandbox that leverages a CSR1000v as
a target. To execute this script against a different device, update the
variables and command-line arguments that list the connectivity, management
interface, and url_base for RESTCONF.
Requirements:
Python
- requests
"""
import json
import requests
import sys
import os
import ipaddress
from argparse import ArgumentParser
from collections import OrderedDict
from getpass import getpass
import urllib3
# Disable SSL Warnings
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
# Identify yang+json as the data formats
headers = {'Content-Type': 'application/yang-data+json',
'Accept': 'application/yang-data+json'}
# Function to retrieve the list of interfaces on a device
def get_configured_interfaces(url_base, username, password):
# this statement performs a GET on the specified url
try:
response = requests.get(url_base,
auth=(username, password),
headers=headers,
verify=False
)
response.raise_for_status()
except Exception as e:
print(e, file=sys.stderr)
sys.exit(1)
# return the json as text
return response.json()["ietf-interfaces:interfaces"]["interface"]
# Used to configure the IP address on an interface
def configure_ip_address(url_base, interface, ip, username, password):
# RESTCONF URL for specific interface
url = url_base + "/interface={i}".format(i=interface)
# Create the data payload to reconfigure IP address
# Need to use OrderedDicts to maintain the order of elements
data = OrderedDict([('ietf-interfaces:interface',
OrderedDict([
('name', interface),
('type', 'iana-if-type:ethernetCsmacd'),
('ietf-ip:ipv4',
OrderedDict([
('address', [OrderedDict([
('ip', ip["address"]),
('netmask', ip["mask"])
])]
)
])
),
])
)])
# Use PUT request to update data
try:
response = requests.put(url,
auth=(username, password),
headers=headers,
verify=False,
json=data
)
response.raise_for_status()
except Exception as e:
print(e, file=sys.stderr)
sys.exit(1)
print(response.text)
# Retrieve and print the current configuration of an interface
def print_interface_details(url_base, interface, username, password, cidr):
url = url_base + "/interface={i}".format(i=interface)
# this statement performs a GET on the specified url
try:
response = requests.get(url,
auth=(username, password),
headers=headers,
verify=False
)
response.raise_for_status()
except Exception as e:
print(e, file=sys.stderr)
sys.exit(1)
intf = response.json()["ietf-interfaces:interface"]
# return the json as text
print("Name: ", intf[0]["name"])
try:
netmask = intf[0]["ietf-ip:ipv4"]["address"][0]["netmask"]
if cidr:
nma = ipaddress.ip_address(netmask)
netmask = str("{0:b}".format(int(nma)).count('1'))
print("IP Address: ", intf[0]["ietf-ip:ipv4"]["address"][0]["ip"], "/",
netmask)
except KeyError:
print("IP Address: UNCONFIGURED")
except Exception as e:
print(e, file=sys.stderr)
sys.exit(1)
print()
return(intf)
# Ask the user to select an interface to configure. Ensures input is valid and
# NOT the management interface
def interface_selection(interfaces, mgmt_if):
# Ask User which interface to configure
sel = input("Which Interface do you want to configure? ")
# Validate interface input
# Must be an interface on the device AND NOT be the Management Interface
while sel == mgmt_if or not sel in [intf["name"] for intf in interfaces]:
print("INVALID: Select an available interface.")
print(" " + mgmt_if + " is used for management.")
print(" Choose another Interface")
sel = input("Which Interface do you want to configure? ")
return(sel)
# Asks the user to provide an IP address and Mask.
def get_ip_info(cidr):
# Ask User for IP and Mask
ip = {}
try:
if cidr:
ipa_t = input("What IP address/prefixlen do you want to set? ")
ipi = ipaddress.ip_interface(ipa_t)
ip["address"] = ipi.ip.compressed
ip["mask"] = ipi.netmask.compressed
else:
ipa_t = input("What IP address do you want to set? ")
ipi = ipaddress.ip_interface(ipa_t)
ip["address"] = ipi.ip.compressed
ipm_t = input("What Subnet Mask do you want to set? ")
ipm = ipaddress.ip_address(ipm_t)
ip["mask"] = ipm.compressed
except Exception as e:
print(e, file=sys.stderr)
sys.exit(1)
return(ip)
def main():
"""
Simple main method calling our function.
"""
parser = ArgumentParser(
prog=sys.argv[0], description='RESTCONF interface management tool')
parser.add_argument('--hostname', '-a', type=str,
help='sandbox hostname or IP address',
default='ios-xe-mgmt.cisco.com')
parser.add_argument('--username', '-u', type=str,
help='sandbox username', default='developer')
# Identifies the interface on the device used for management access
# Used to ensure the script isn't used to update the IP leveraged to manage
# device
parser.add_argument('--management_if', '-m', type=str,
help='management interface', default='GigabitEthernet1')
parser.add_argument('--port', '-P', type=int,
help='sandbox web port', default=443)
parser.add_argument('--cidr', help='use CIDR format for interface IP',
action='store_true')
args = parser.parse_args()
password = os.getenv('DEVNET_RESTCONF_PASSWORD')
if password is None:
password = getpass()
# Create the base URL for RESTCONF calls
url_base = "https://{h}:{p}/restconf/data/ietf-interfaces:interfaces".format(h=args.hostname, p=args.port)
# Get a List of Interfaces
interfaces = get_configured_interfaces(url_base, args.username, password)
print("The router has the following interfaces: \n")
for interface in interfaces:
print(" * {name:25}".format(name=interface["name"]))
print("")
# Ask User which interface to configure
selected_interface = interface_selection(interfaces, args.management_if)
print(selected_interface)
# Print Starting Interface Details
print("Starting Interface Configuration")
print_interface_details(url_base, selected_interface, args.username,
password, args.cidr)
# As User for IP Address to set
ip = get_ip_info(args.cidr)
# Configure interface
configure_ip_address(url_base, selected_interface, ip, args.username, password)
# Print Ending Interface Details
print("Ending Interface Configuration")
print_interface_details(url_base, selected_interface, args.username,
password, args.cidr)
if __name__ == '__main__':
sys.exit(main())