|
| 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()) |
0 commit comments