forked from CiscoDevNet/python_code_samples_network
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdateip.py
186 lines (143 loc) · 6.03 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
#!/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.5, 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
that list the connectivity, management interface, and url_base for RESTCONF.
Requirements:
Python
- requests
"""
import json
import requests
import sys
from argparse import ArgumentParser
from collections import OrderedDict
import urllib3
# Disable SSL Warnings
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
# These variables target the RESTCONF Always-On Sandbox hosted by Cisco DevNet
HOST = 'ios-xe-mgmt.cisco.com'
PORT = '9443'
USER = 'root'
PASS = 'D_Vay!_10&'
# 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
MANAGEMENT_INTERFACE = "GigabitEthernet1"
# Create the base URL for RESTCONF calls
url_base = "https://{h}:{p}/restconf".format(h=HOST, p=PORT)
# 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 = url_base + "/data/ietf-interfaces:interfaces"
# this statement performs a GET on the specified url
response = requests.get(url,
auth=(USER, PASS),
headers=headers,
verify=False
)
# 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(interface, ip):
# RESTCONF URL for specific interface
url = url_base + "/data/ietf-interfaces:interfaces/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
response = requests.put(url,
auth=(USER, PASS),
headers=headers,
verify=False,
json=data
)
print(response.text)
# Retrieve and print the current configuration of an interface
def print_interface_details(interface):
url = url_base + "/data/ietf-interfaces:interfaces/interface={i}".format(i=interface)
# this statement performs a GET on the specified url
response = requests.get(url,
auth=(USER, PASS),
headers=headers,
verify=False
)
intf = response.json()["ietf-interfaces:interface"]
# return the json as text
print("Name: ", intf["name"])
try:
print("IP Address: ", intf["ietf-ip:ipv4"]["address"][0]["ip"], "/",
intf["ietf-ip:ipv4"]["address"][0]["netmask"])
except KeyError:
print("IP Address: UNCONFIGURED")
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):
# 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 == MANAGEMENT_INTERFACE or not sel in [intf["name"] for intf in interfaces]:
print("INVALID: Select an available interface.")
print(" " + MANAGEMENT_INTERFACE + " 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. Data is NOT validated.
def get_ip_info():
# Ask User for IP and Mask
ip = {}
ip["address"] = input("What IP address do you want to set? ")
ip["mask"] = input("What Subnet Mask do you want to set? ")
return(ip)
def main():
global do_input
"""
Simple main method calling our function.
"""
# Get a List of Interfaces
interfaces = get_configured_interfaces()
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)
print(selected_interface)
# Print Starting Interface Details
print("Starting Interface Configuration")
print_interface_details(selected_interface)
# As User for IP Address to set
ip = get_ip_info()
# Configure interface
configure_ip_address(selected_interface, ip)
# Print Ending Interface Details
print("Ending Interface Configuration")
print_interface_details(selected_interface)
if __name__ == '__main__':
sys.exit(main())