diff --git a/README.md b/README.md index 5f7afb1..aa0eaed 100644 --- a/README.md +++ b/README.md @@ -34,4 +34,4 @@ Here are few Python scripts that can interact with network elements using one of | [Basic NETCONF Edit](/NC-edit-config) | A basic ncclient example to `` NETCONF Data | | [NETCONF XPATH Example](/NC-get-config-xpath) | Use the XPATH feature when making a NETCONF Requests | | [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 | - +| [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. | \ No newline at end of file diff --git a/RESTCONF/README.md b/RESTCONF/README.md new file mode 100644 index 0000000..0e99643 --- /dev/null +++ b/RESTCONF/README.md @@ -0,0 +1,10 @@ +# RESTCONF + +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. + +These are examples scripts for RESTCONF to retrieve and configure the switch using different operations such as Get, Delete, Post, Put and Patch. + +## requirements + +-- IOS-XE running >/= 16.8 also enabled for RESTCONF + diff --git a/RESTCONF/delete-Ip-address.py b/RESTCONF/delete-Ip-address.py new file mode 100644 index 0000000..157a524 --- /dev/null +++ b/RESTCONF/delete-Ip-address.py @@ -0,0 +1,61 @@ +#!/usr/bin/python +# +# Copyright (c) 2018 Krishna Kotha +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# 1. Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# 2. Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# +# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND +# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE +# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS +# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY +# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +# SUCH DAMAGE. +# +# This script deletes the Ip address of a interface using DELETE operation via RESTCONF + +# import the requests library +import requests +import sys + +# disable warnings from SSL/TLS certificates +requests.packages.urllib3.disable_warnings() + +# use the IP address or hostname of your Cat9300 +HOST = '172.26.198.63' + +# use your user credentials to access the Cat9300 +USER = 'cisco' +PASS = 'cisco' + + +# create a main() method +def main(): + """Main method that configures the Ip address for a interface via RESTCONF.""" + + # url string to issue GET request + url = "https://172.26.198.63/restconf/data/Cisco-IOS-XE-native:native/interface/TenGigabitEthernet=1%2F0%2F10/ip/address/primary" + + # RESTCONF media types for REST API headers + headers = {'Content-Type': 'application/yang-data+json', + 'Accept': 'application/yang-data+json'} + # this statement performs a GET on the specified url + response = requests.request("DELETE",url, auth=(USER, PASS), + headers=headers, verify=False) + # print the json that is returned + print(response.text) + +if __name__ == '__main__': + sys.exit(main()) diff --git a/RESTCONF/get-interface-config.py b/RESTCONF/get-interface-config.py new file mode 100644 index 0000000..d945a28 --- /dev/null +++ b/RESTCONF/get-interface-config.py @@ -0,0 +1,62 @@ +#!/usr/bin/python +# +# Copyright (c) 2018 Krishna Kotha +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# 1. Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# 2. Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# +# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND +# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE +# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS +# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY +# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +# SUCH DAMAGE. +# +# This script retrieves entire interface configuration from a network element via RESTCONF + +# import the requests library +import requests +import sys + +# disable warnings from SSL/TLS certificates +requests.packages.urllib3.disable_warnings() + +# use the IP address or hostname of your Cat9300 +HOST = '172.26.198.63' + +# use your user credentials to access the Cat9300 +USER = 'cisco' +PASS = 'cisco' + + +# create a main() method +def main(): + """Main method that retrieves the Interface details from Cat9300 via RESTCONF.""" + + # url string to issue GET request + url = "https://{h}/restconf/data/ietf-interfaces:interfaces".format(h=HOST) + + # RESTCONF media types for REST API headers + headers = {'Content-Type': 'application/yang-data+json', + 'Accept': 'application/yang-data+json'} + # this statement performs a GET on the specified url + response = requests.get(url, auth=(USER, PASS), + headers=headers, verify=False) + + # print the json that is returned + print(response.text) + +if __name__ == '__main__': + sys.exit(main()) diff --git a/RESTCONF/patch-Ip-address-config.py b/RESTCONF/patch-Ip-address-config.py new file mode 100644 index 0000000..162565f --- /dev/null +++ b/RESTCONF/patch-Ip-address-config.py @@ -0,0 +1,63 @@ +#!/usr/bin/python +# +# Copyright (c) 2018 Krishna Kotha +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# 1. Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# 2. Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# +# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND +# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE +# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS +# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY +# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +# SUCH DAMAGE. +# +# This script configures Ip address of a interface using PATCH operation via RESTCONF + +# import the requests library +import requests +import sys + +# disable warnings from SSL/TLS certificates +requests.packages.urllib3.disable_warnings() + +# use the IP address or hostname of your Cat9300 +HOST = '172.26.198.63' + +# use your user credentials to access the Cat9300 +USER = 'cisco' +PASS = 'cisco' + + +# create a main() method +def main(): + """Main method that configures the Ip address for a interface via RESTCONF.""" + + # url string to issue GET request + url = "https://{h}/restconf/data/Cisco-IOS-XE-native:native/interface/TenGigabitEthernet=1%2F0%2F10/ip/address/primary".format(h=HOST) + payload = "{\"primary\": {\"address\": \"100.100.100.1\", \"mask\": \"255.255.255.0\"}}" + + # RESTCONF media types for REST API headers + headers = {'Content-Type': 'application/yang-data+json', + 'Accept': 'application/yang-data+json'} + # this statement performs a GET on the specified url + response = requests.request("PATCH",url, auth=(USER, PASS), + data=payload, headers=headers, verify=False) + + # print the json that is returned + print(response.text) + +if __name__ == '__main__': + sys.exit(main()) diff --git a/RESTCONF/post-ipdomain.py b/RESTCONF/post-ipdomain.py new file mode 100644 index 0000000..d25d3dd --- /dev/null +++ b/RESTCONF/post-ipdomain.py @@ -0,0 +1,63 @@ +#!/usr/bin/python +# +# Copyright (c) 2018 Krishna Kotha +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# 1. Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# 2. Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# +# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND +# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE +# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS +# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY +# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +# SUCH DAMAGE. +# +# This script configures IP domain cisco.com using POST operation via RESTCONF + +# import the requests library +import requests +import sys + +# disable warnings from SSL/TLS certificates +requests.packages.urllib3.disable_warnings() + +# use the IP address or hostname of your Cat9300 +HOST = '172.26.198.63' + +# use your user credentials to access the Cat9300 +USER = 'cisco' +PASS = 'cisco' + + +# create a main() method +def main(): + """Main method that configures the Ip address for a interface via RESTCONF.""" + + # url string to issue GET request + url = "https://{h}/restconf/data/Cisco-IOS-XE-native:native/ip/domain".format(h=HOST) + payload = "{\"name\": \"cisco.com\"}" + + # RESTCONF media types for REST API headers + headers = {'Content-Type': 'application/yang-data+json', + 'Accept': 'application/yang-data+json'} + # this statement performs a GET on the specified url + response = requests.request("POST",url, auth=(USER, PASS), + data=payload, headers=headers, verify=False) + + # print the json that is returned + print(response.text) + +if __name__ == '__main__': + sys.exit(main()) diff --git a/RESTCONF/put-hostname-config.py b/RESTCONF/put-hostname-config.py new file mode 100644 index 0000000..d2e5f2f --- /dev/null +++ b/RESTCONF/put-hostname-config.py @@ -0,0 +1,63 @@ +#!/usr/bin/python +# +# Copyright (c) 2018 Krishna Kotha +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# 1. Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# 2. Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# +# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND +# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE +# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS +# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY +# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +# SUCH DAMAGE. +# +# This script configures hostname of a network element using PUT operation via RESTCONF + +# import the requests library +import requests +import sys + +# disable warnings from SSL/TLS certificates +requests.packages.urllib3.disable_warnings() + +# use the IP address or hostname of your Cat9300 +HOST = '172.26.198.63' + +# use your user credentials to access the Cat9300 +USER = 'cisco' +PASS = 'cisco' + + +# create a main() method +def main(): + """Main method that configures the Ip address for a interface via RESTCONF.""" + + # url string to issue GET request + url = "https://{h}/restconf/data/Cisco-IOS-XE-native:native/hostname".format(h=HOST) + payload = "{\"hostname\": \"CATALYST9300\"}" + + # RESTCONF media types for REST API headers + headers = {'Content-Type': 'application/yang-data+json', + 'Accept': 'application/yang-data+json'} + # this statement performs a GET on the specified url + response = requests.request("PUT",url, auth=(USER, PASS), + data=payload, headers=headers, verify=False) + + # print the json that is returned + print(response.text) + +if __name__ == '__main__': + sys.exit(main())