1
1
#!/usr/bin/env python
2
2
"""
3
- This Python script leverages RESTCONF to:
3
+ This Python script leverages RESTCONF to:
4
4
- retrieve a list of interfaces on a device
5
- - ask the user for the interface to configure
6
- - displays the interface IP information
7
- - asks user for new IP information
8
- - updates the IP address on the interface
9
- - displays the final IP information on the interface
10
-
11
- This script has been tested with Python 3.5, however may work with other versions.
12
-
13
- This script targets the RESTCONF DevNet Sandbox that leverages a CSR1000v as
14
- a target. To execute this script against a different device, update the variables
15
- that list the connectivity, management interface, and url_base for RESTCONF.
16
-
17
- Requirements:
18
- Python
5
+ - ask the user for the interface to configure
6
+ - displays the interface IP information
7
+ - asks user for new IP information
8
+ - updates the IP address on the interface
9
+ - displays the final IP information on the interface
10
+
11
+ This script has been tested with Python 3.5, however may work with other versions.
12
+
13
+ This script targets the RESTCONF DevNet Sandbox that leverages a CSR1000v as
14
+ a target. To execute this script against a different device, update the variables
15
+ that list the connectivity, management interface, and url_base for RESTCONF.
16
+
17
+ Requirements:
18
+ Python
19
19
- requests
20
-
21
-
20
+
21
+
22
22
"""
23
23
24
24
import json
25
- import requests
25
+ import requests
26
26
import sys
27
27
from collections import OrderedDict
28
+ import urllib3
29
+
30
+ # Disable SSL Warnings
31
+ urllib3 .disable_warnings (urllib3 .exceptions .InsecureRequestWarning )
32
+
28
33
29
34
# These variables target the RESTCONF Always-On Sandbox hosted by Cisco DevNet
30
35
HOST = 'ios-xe-mgmt.cisco.com'
31
36
PORT = '9443'
32
37
USER = 'root'
33
- PASS = 'C!sc0123 '
38
+ PASS = 'D_Vay!_10& '
34
39
35
- # Identifies the interface on the device used for management access
40
+ # Identifies the interface on the device used for management access
36
41
# Used to ensure the script isn't used to update the IP leveraged to manage device
37
42
MANAGEMENT_INTERFACE = "GigabitEthernet1"
38
43
39
44
# Create the base URL for RESTCONF calls
40
- url_base = "http ://{h}:{p}/api " .format (h = HOST , p = PORT )
45
+ url_base = "https ://{h}:{p}/restconf " .format (h = HOST , p = PORT )
41
46
42
- # Identify yang+json as the data formats
43
- headers = {'Content-Type' : 'application/vnd. yang. data+json' ,
44
- 'Accept' : 'application/vnd. yang. data+json' }
47
+ # Identify yang+json as the data formats
48
+ headers = {'Content-Type' : 'application/yang- data+json' ,
49
+ 'Accept' : 'application/yang- data+json' }
45
50
46
51
47
- # Function to retrieve the list of interfaces on a device
52
+ # Function to retrieve the list of interfaces on a device
48
53
def get_configured_interfaces ():
49
- url = url_base + "/running/ interfaces?deep "
54
+ url = url_base + "/data/ietf- interfaces:interfaces "
50
55
51
56
# this statement performs a GET on the specified url
52
- response = requests .get (url ,
57
+ response = requests .get (url ,
53
58
auth = (USER , PASS ),
54
- headers = headers ,
59
+ headers = headers ,
55
60
verify = False
56
61
)
57
62
58
63
# return the json as text
59
64
return response .json ()["ietf-interfaces:interfaces" ]["interface" ]
60
65
61
66
62
- # Used to configure the IP address on an interface
63
- def configure_ip_address (interface , ip ):
64
- # RESTCONF URL for specific interface
65
- url = url_base + "/running/ interfaces/interface/ {i}" .format (i = interface )
66
-
67
+ # Used to configure the IP address on an interface
68
+ def configure_ip_address (interface , ip ):
69
+ # RESTCONF URL for specific interface
70
+ url = url_base + "/data/ietf- interfaces:interfaces /interface= {i}" .format (i = interface )
71
+
67
72
# Create the data payload to reconfigure IP address
68
73
# Need to use OrderedDicts to maintain the order of elements
69
74
data = OrderedDict ([('ietf-interfaces:interface' ,
70
75
OrderedDict ([
71
76
('name' , interface ),
72
- ('type' , 'ianaift :ethernetCsmacd' ),
77
+ ('type' , 'iana-if-type :ethernetCsmacd' ),
73
78
('ietf-ip:ipv4' ,
74
79
OrderedDict ([
75
80
('address' , [OrderedDict ([
@@ -80,43 +85,46 @@ def configure_ip_address(interface, ip):
80
85
])
81
86
),
82
87
])
83
- )])
88
+ )])
84
89
85
90
# Use PUT request to update data
86
- response = requests .put (url ,
87
- auth = (USER , PASS ),
88
- headers = headers ,
89
- verify = False ,
91
+ response = requests .put (url ,
92
+ auth = (USER , PASS ),
93
+ headers = headers ,
94
+ verify = False ,
90
95
json = data
91
96
)
92
97
print (response .text )
93
98
94
99
95
- # Retrieve and print the current configuration of an interface
100
+ # Retrieve and print the current configuration of an interface
96
101
def print_interface_details (interface ):
97
- url = url_base + "/running/ interfaces/interface/ {i}?deep " .format (i = interface )
102
+ url = url_base + "/data/ietf- interfaces:interfaces /interface= {i}" .format (i = interface )
98
103
99
104
# this statement performs a GET on the specified url
100
- response = requests .get (url ,
105
+ response = requests .get (url ,
101
106
auth = (USER , PASS ),
102
- headers = headers ,
107
+ headers = headers ,
103
108
verify = False
104
109
)
105
110
106
111
intf = response .json ()["ietf-interfaces:interface" ]
107
- # return the json as text
112
+ # return the json as text
108
113
print ("Name: " , intf ["name" ])
109
- print ("IP Address: " , intf ["ietf-ip:ipv4" ]["address" ][0 ]["ip" ], "/" ,
110
- intf ["ietf-ip:ipv4" ]["address" ][0 ]["netmask" ])
114
+ try :
115
+ print ("IP Address: " , intf ["ietf-ip:ipv4" ]["address" ][0 ]["ip" ], "/" ,
116
+ intf ["ietf-ip:ipv4" ]["address" ][0 ]["netmask" ])
117
+ except KeyError :
118
+ print ("IP Address: UNCONFIGURED" )
111
119
print ()
112
120
113
121
return (intf )
114
122
115
123
116
- # Ask the user to select an interface to configure. Ensures input is valid and
124
+ # Ask the user to select an interface to configure. Ensures input is valid and
117
125
# NOT the management interface
118
126
def interface_selection (interfaces ):
119
- # Ask User which interface to configure
127
+ # Ask User which interface to configure
120
128
sel = input ("Which Interface do you want to configure? " )
121
129
122
130
# Validate interface input
@@ -126,35 +134,35 @@ def interface_selection(interfaces):
126
134
print (" " + MANAGEMENT_INTERFACE + " is used for management." )
127
135
print (" Choose another Interface" )
128
136
sel = input ("Which Interface do you want to configure? " )
129
-
137
+
130
138
return (sel )
131
139
132
-
133
- # Asks the user to provide an IP address and Mask. Data is NOT validated.
134
- def get_ip_info ():
140
+
141
+ # Asks the user to provide an IP address and Mask. Data is NOT validated.
142
+ def get_ip_info ():
135
143
# Ask User for IP and Mask
136
144
ip = {}
137
145
ip ["address" ] = input ("What IP address do you want to set? " )
138
146
ip ["mask" ] = input ("What Subnet Mask do you want to set? " )
139
147
return (ip )
140
148
141
-
149
+
142
150
def main ():
143
151
"""
144
152
Simple main method calling our function.
145
- """
146
- # Get a List of Interfaces
153
+ """
154
+ # Get a List of Interfaces
147
155
interfaces = get_configured_interfaces ()
148
156
149
157
print ("The router has the following interfaces: \n " )
150
- for interface in interfaces :
158
+ for interface in interfaces :
151
159
print (" * {name:25}" .format (name = interface ["name" ]))
152
160
153
161
print ("" )
154
162
155
- # Ask User which interface to configure
163
+ # Ask User which interface to configure
156
164
selected_interface = interface_selection (interfaces )
157
- print (selected_interface )
165
+ print (selected_interface )
158
166
159
167
# Print Starting Interface Details
160
168
print ("Starting Interface Configuration" )
@@ -163,13 +171,13 @@ def main():
163
171
# As User for IP Address to set
164
172
ip = get_ip_info ()
165
173
166
- # Configure interface
174
+ # Configure interface
167
175
configure_ip_address (selected_interface , ip )
168
-
176
+
169
177
# Print Ending Interface Details
170
178
print ("Ending Interface Configuration" )
171
179
print_interface_details (selected_interface )
172
-
180
+
173
181
174
182
if __name__ == '__main__' :
175
- sys .exit (main ())
183
+ sys .exit (main ())
0 commit comments