Skip to content

Commit 633c0c1

Browse files
authored
Merge pull request #6 from krishna426426/master
Added scripts for RESTCONF and updated README file
2 parents ae3100c + 5d5b8c8 commit 633c0c1

7 files changed

+323
-1
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,4 @@ Here are few Python scripts that can interact with network elements using one of
3434
| [Basic NETCONF Edit](/NC-edit-config) | A basic ncclient example to `<edit-config>` NETCONF Data |
3535
| [NETCONF XPATH Example](/NC-get-config-xpath) | Use the XPATH feature when making a NETCONF Requests |
3636
| [Model Based AAA](/model-based-aaa) | These example scripts are for Model Based AAA to get, edit and delete the rule-lists for privilege level users and Groups by using ietf-netconf-acm.yang data model |
37-
37+
| [RESTCONF](/RESTCONF) | These example scripts are for RESTCONF to retrieve and configure the switch using different operations such as Get, Delete, Put, Post and Patch. |

RESTCONF/README.md

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# RESTCONF
2+
3+
The RESTCONF is a standard protocol uses YANG data models for managing network devices. It is a protocol based on HTTP to access the configuration data or state data of a networking device. RESTCONF supports operations such as Get, Put, Post, Patch, and Delete. One of the major advantages RESTCONF has over NETCONF is its ability to leverage JSON as a data format. To make the RESTCONF calls, you can use any client application that supports any REST call.
4+
5+
These are examples scripts for RESTCONF to retrieve and configure the switch using different operations such as Get, Delete, Post, Put and Patch.
6+
7+
## requirements
8+
9+
-- IOS-XE running >/= 16.8 also enabled for RESTCONF
10+

RESTCONF/delete-Ip-address.py

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#!/usr/bin/python
2+
#
3+
# Copyright (c) 2018 Krishna Kotha <[email protected]>
4+
# All rights reserved.
5+
#
6+
# Redistribution and use in source and binary forms, with or without
7+
# modification, are permitted provided that the following conditions
8+
# are met:
9+
# 1. Redistributions of source code must retain the above copyright
10+
# notice, this list of conditions and the following disclaimer.
11+
# 2. Redistributions in binary form must reproduce the above copyright
12+
# notice, this list of conditions and the following disclaimer in the
13+
# documentation and/or other materials provided with the distribution.
14+
#
15+
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16+
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17+
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18+
# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19+
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20+
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21+
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22+
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23+
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24+
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25+
# SUCH DAMAGE.
26+
#
27+
# This script deletes the Ip address of a interface using DELETE operation via RESTCONF
28+
29+
# import the requests library
30+
import requests
31+
import sys
32+
33+
# disable warnings from SSL/TLS certificates
34+
requests.packages.urllib3.disable_warnings()
35+
36+
# use the IP address or hostname of your Cat9300
37+
HOST = '172.26.198.63'
38+
39+
# use your user credentials to access the Cat9300
40+
USER = 'cisco'
41+
PASS = 'cisco'
42+
43+
44+
# create a main() method
45+
def main():
46+
"""Main method that configures the Ip address for a interface via RESTCONF."""
47+
48+
# url string to issue GET request
49+
url = "https://172.26.198.63/restconf/data/Cisco-IOS-XE-native:native/interface/TenGigabitEthernet=1%2F0%2F10/ip/address/primary"
50+
51+
# RESTCONF media types for REST API headers
52+
headers = {'Content-Type': 'application/yang-data+json',
53+
'Accept': 'application/yang-data+json'}
54+
# this statement performs a GET on the specified url
55+
response = requests.request("DELETE",url, auth=(USER, PASS),
56+
headers=headers, verify=False)
57+
# print the json that is returned
58+
print(response.text)
59+
60+
if __name__ == '__main__':
61+
sys.exit(main())

RESTCONF/get-interface-config.py

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#!/usr/bin/python
2+
#
3+
# Copyright (c) 2018 Krishna Kotha <[email protected]>
4+
# All rights reserved.
5+
#
6+
# Redistribution and use in source and binary forms, with or without
7+
# modification, are permitted provided that the following conditions
8+
# are met:
9+
# 1. Redistributions of source code must retain the above copyright
10+
# notice, this list of conditions and the following disclaimer.
11+
# 2. Redistributions in binary form must reproduce the above copyright
12+
# notice, this list of conditions and the following disclaimer in the
13+
# documentation and/or other materials provided with the distribution.
14+
#
15+
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16+
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17+
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18+
# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19+
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20+
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21+
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22+
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23+
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24+
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25+
# SUCH DAMAGE.
26+
#
27+
# This script retrieves entire interface configuration from a network element via RESTCONF
28+
29+
# import the requests library
30+
import requests
31+
import sys
32+
33+
# disable warnings from SSL/TLS certificates
34+
requests.packages.urllib3.disable_warnings()
35+
36+
# use the IP address or hostname of your Cat9300
37+
HOST = '172.26.198.63'
38+
39+
# use your user credentials to access the Cat9300
40+
USER = 'cisco'
41+
PASS = 'cisco'
42+
43+
44+
# create a main() method
45+
def main():
46+
"""Main method that retrieves the Interface details from Cat9300 via RESTCONF."""
47+
48+
# url string to issue GET request
49+
url = "https://{h}/restconf/data/ietf-interfaces:interfaces".format(h=HOST)
50+
51+
# RESTCONF media types for REST API headers
52+
headers = {'Content-Type': 'application/yang-data+json',
53+
'Accept': 'application/yang-data+json'}
54+
# this statement performs a GET on the specified url
55+
response = requests.get(url, auth=(USER, PASS),
56+
headers=headers, verify=False)
57+
58+
# print the json that is returned
59+
print(response.text)
60+
61+
if __name__ == '__main__':
62+
sys.exit(main())

RESTCONF/patch-Ip-address-config.py

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#!/usr/bin/python
2+
#
3+
# Copyright (c) 2018 Krishna Kotha <[email protected]>
4+
# All rights reserved.
5+
#
6+
# Redistribution and use in source and binary forms, with or without
7+
# modification, are permitted provided that the following conditions
8+
# are met:
9+
# 1. Redistributions of source code must retain the above copyright
10+
# notice, this list of conditions and the following disclaimer.
11+
# 2. Redistributions in binary form must reproduce the above copyright
12+
# notice, this list of conditions and the following disclaimer in the
13+
# documentation and/or other materials provided with the distribution.
14+
#
15+
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16+
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17+
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18+
# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19+
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20+
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21+
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22+
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23+
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24+
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25+
# SUCH DAMAGE.
26+
#
27+
# This script configures Ip address of a interface using PATCH operation via RESTCONF
28+
29+
# import the requests library
30+
import requests
31+
import sys
32+
33+
# disable warnings from SSL/TLS certificates
34+
requests.packages.urllib3.disable_warnings()
35+
36+
# use the IP address or hostname of your Cat9300
37+
HOST = '172.26.198.63'
38+
39+
# use your user credentials to access the Cat9300
40+
USER = 'cisco'
41+
PASS = 'cisco'
42+
43+
44+
# create a main() method
45+
def main():
46+
"""Main method that configures the Ip address for a interface via RESTCONF."""
47+
48+
# url string to issue GET request
49+
url = "https://{h}/restconf/data/Cisco-IOS-XE-native:native/interface/TenGigabitEthernet=1%2F0%2F10/ip/address/primary".format(h=HOST)
50+
payload = "{\"primary\": {\"address\": \"100.100.100.1\", \"mask\": \"255.255.255.0\"}}"
51+
52+
# RESTCONF media types for REST API headers
53+
headers = {'Content-Type': 'application/yang-data+json',
54+
'Accept': 'application/yang-data+json'}
55+
# this statement performs a GET on the specified url
56+
response = requests.request("PATCH",url, auth=(USER, PASS),
57+
data=payload, headers=headers, verify=False)
58+
59+
# print the json that is returned
60+
print(response.text)
61+
62+
if __name__ == '__main__':
63+
sys.exit(main())

RESTCONF/post-ipdomain.py

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#!/usr/bin/python
2+
#
3+
# Copyright (c) 2018 Krishna Kotha <[email protected]>
4+
# All rights reserved.
5+
#
6+
# Redistribution and use in source and binary forms, with or without
7+
# modification, are permitted provided that the following conditions
8+
# are met:
9+
# 1. Redistributions of source code must retain the above copyright
10+
# notice, this list of conditions and the following disclaimer.
11+
# 2. Redistributions in binary form must reproduce the above copyright
12+
# notice, this list of conditions and the following disclaimer in the
13+
# documentation and/or other materials provided with the distribution.
14+
#
15+
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16+
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17+
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18+
# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19+
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20+
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21+
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22+
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23+
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24+
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25+
# SUCH DAMAGE.
26+
#
27+
# This script configures IP domain cisco.com using POST operation via RESTCONF
28+
29+
# import the requests library
30+
import requests
31+
import sys
32+
33+
# disable warnings from SSL/TLS certificates
34+
requests.packages.urllib3.disable_warnings()
35+
36+
# use the IP address or hostname of your Cat9300
37+
HOST = '172.26.198.63'
38+
39+
# use your user credentials to access the Cat9300
40+
USER = 'cisco'
41+
PASS = 'cisco'
42+
43+
44+
# create a main() method
45+
def main():
46+
"""Main method that configures the Ip address for a interface via RESTCONF."""
47+
48+
# url string to issue GET request
49+
url = "https://{h}/restconf/data/Cisco-IOS-XE-native:native/ip/domain".format(h=HOST)
50+
payload = "{\"name\": \"cisco.com\"}"
51+
52+
# RESTCONF media types for REST API headers
53+
headers = {'Content-Type': 'application/yang-data+json',
54+
'Accept': 'application/yang-data+json'}
55+
# this statement performs a GET on the specified url
56+
response = requests.request("POST",url, auth=(USER, PASS),
57+
data=payload, headers=headers, verify=False)
58+
59+
# print the json that is returned
60+
print(response.text)
61+
62+
if __name__ == '__main__':
63+
sys.exit(main())

RESTCONF/put-hostname-config.py

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#!/usr/bin/python
2+
#
3+
# Copyright (c) 2018 Krishna Kotha <[email protected]>
4+
# All rights reserved.
5+
#
6+
# Redistribution and use in source and binary forms, with or without
7+
# modification, are permitted provided that the following conditions
8+
# are met:
9+
# 1. Redistributions of source code must retain the above copyright
10+
# notice, this list of conditions and the following disclaimer.
11+
# 2. Redistributions in binary form must reproduce the above copyright
12+
# notice, this list of conditions and the following disclaimer in the
13+
# documentation and/or other materials provided with the distribution.
14+
#
15+
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16+
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17+
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18+
# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19+
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20+
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21+
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22+
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23+
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24+
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25+
# SUCH DAMAGE.
26+
#
27+
# This script configures hostname of a network element using PUT operation via RESTCONF
28+
29+
# import the requests library
30+
import requests
31+
import sys
32+
33+
# disable warnings from SSL/TLS certificates
34+
requests.packages.urllib3.disable_warnings()
35+
36+
# use the IP address or hostname of your Cat9300
37+
HOST = '172.26.198.63'
38+
39+
# use your user credentials to access the Cat9300
40+
USER = 'cisco'
41+
PASS = 'cisco'
42+
43+
44+
# create a main() method
45+
def main():
46+
"""Main method that configures the Ip address for a interface via RESTCONF."""
47+
48+
# url string to issue GET request
49+
url = "https://{h}/restconf/data/Cisco-IOS-XE-native:native/hostname".format(h=HOST)
50+
payload = "{\"hostname\": \"CATALYST9300\"}"
51+
52+
# RESTCONF media types for REST API headers
53+
headers = {'Content-Type': 'application/yang-data+json',
54+
'Accept': 'application/yang-data+json'}
55+
# this statement performs a GET on the specified url
56+
response = requests.request("PUT",url, auth=(USER, PASS),
57+
data=payload, headers=headers, verify=False)
58+
59+
# print the json that is returned
60+
print(response.text)
61+
62+
if __name__ == '__main__':
63+
sys.exit(main())

0 commit comments

Comments
 (0)