From 6a0d2b927a92e19b73702815002c01a6d8eeb4be Mon Sep 17 00:00:00 2001 From: tope-oyenuga Date: Sun, 11 Dec 2016 00:13:07 +0100 Subject: [PATCH] Add files via upload --- nxapi/samples/conf_interface.py | 75 +++++++++++++++++++++++++++++++++ nxapi/samples/conf_svi.py | 21 ++++----- nxapi/samples/conf_vlan.py | 13 +++--- nxapi/samples/show_interface.py | 46 ++++++++++++++++++++ nxapi/samples/show_version.py | 18 ++++---- 5 files changed, 145 insertions(+), 28 deletions(-) create mode 100644 nxapi/samples/conf_interface.py create mode 100644 nxapi/samples/show_interface.py diff --git a/nxapi/samples/conf_interface.py b/nxapi/samples/conf_interface.py new file mode 100644 index 0000000..b3890cf --- /dev/null +++ b/nxapi/samples/conf_interface.py @@ -0,0 +1,75 @@ +import requests +import json + +print ("enter ip address of switch") +ip = input () + +#get the interface to be configured +print ("what interface would you like to configure, e.g interface 'eth 1/1'") +int_id = input () + +#print ("is this interface: 'routed' or an 'access' port?") +#port_type = () + +#if port_type = "routed" +# port = "no switchport" + +#get ip address of interface +print ("enter ip of the interface, e.g. 'A.B.C.D/SM'") +int_ip = input() + +myheaders = {'content-type': 'application/json-rpc'} +url = ("http://"+ip+"/ins") +username = ("admin") +password = ("Cisco321") + +payload = [ + { + "jsonrpc": "2.0", + "method": "cli", + "params": { + "cmd": "conf t", + "version": 1 + }, + "id": 1 + }, + { + "jsonrpc": "2.0", + "method": "cli", + "params": { + "cmd": "interface "+int_id, + "version": 1 + }, + "id": 2 + }, + { + "jsonrpc": "2.0", + "method": "cli", + "params": { + "cmd": "no switchport", + "version": 1 + }, + "id": 3 + }, + { + "jsonrpc": "2.0", + "method": "cli", + "params": { + "cmd": "ip address "+int_ip, + "version": 1 + }, + "id": 4 + }, + { + "jsonrpc": "2.0", + "method": "cli", + "params": { + "cmd": "no shut", + "version": 1 + }, + "id": 5 + }, +] + + +response = requests.post(url,data=json.dumps(payload), headers=myheaders,auth=(username,password)).json() \ No newline at end of file diff --git a/nxapi/samples/conf_svi.py b/nxapi/samples/conf_svi.py index 3fd87e4..9e7d69c 100644 --- a/nxapi/samples/conf_svi.py +++ b/nxapi/samples/conf_svi.py @@ -1,19 +1,19 @@ import requests import json -print "enter ip address of switch" -ip=raw_input() +print ("enter ip address of switch") +ip = input () -print "enter vlan for which svi has to be configured" -vlanId=raw_input() +print ("enter vlan for which svi has to be configured") +vlanId = input () -print "enter ip of the svi" -ip_vlanId = raw_input() +print ("enter ip of the svi") +ip_vlanId = input() myheaders = {'content-type': 'application/json-rpc'} -url = "http://"+ip+"/ins" -username = "admin" -password = "ciscotme" +url = ("http://"+ip+"/ins") +username = ("admin") +password = ("Cisco321") payload=[ {"jsonrpc": "2.0","method": "cli","params": {"cmd": "conf t","version": 1},"id": 1}, @@ -24,6 +24,3 @@ ] response = requests.post(url,data=json.dumps(payload), headers=myheaders,auth=(username,password)).json() - - - diff --git a/nxapi/samples/conf_vlan.py b/nxapi/samples/conf_vlan.py index 9287b0b..0ddac9b 100644 --- a/nxapi/samples/conf_vlan.py +++ b/nxapi/samples/conf_vlan.py @@ -1,16 +1,16 @@ import requests import json -print "enter ip address" -ip=raw_input() +print ("enter ip address of switch") +ip=input() -print "enter vlan to be configured" -vlanId=raw_input() +print ("enter vlan to be configured") +vlanId=input() myheaders = {'content-type': 'application/json-rpc'} url = "http://"+ip+"/ins" username = "admin" -password = "ciscotme" +password = "Cisco321" payload=[ @@ -20,6 +20,3 @@ ] response = requests.post(url,data=json.dumps(payload), headers=myheaders,auth=(username,password)).json() - - - diff --git a/nxapi/samples/show_interface.py b/nxapi/samples/show_interface.py new file mode 100644 index 0000000..d4faf90 --- /dev/null +++ b/nxapi/samples/show_interface.py @@ -0,0 +1,46 @@ +#Show interfaces from a switch +import json +import requests + +print ("enter ip address of the switch") +ip=input () + +print ('what interface do you want to check? e.g. eth 1/1') +interface = input () + +my_headers = {'content-type': 'application/json-rpc'} +url = "http://"+ip+"/ins" +username = "admin" +password = "Cisco321" + +#Json-rpc payload +payload=[ + { + "jsonrpc": "2.0", + "method": "cli", + "params": { + "cmd": "sh ip int "+interface, + "version": 1 + }, + "id": 1 + } +] + +#Post the payload to the switch in a json-rpc format to get a response and save it +response = requests.post(url, data=json.dumps(payload), headers=my_headers, auth=(username, password)).json() + +#Now Process the response +#First check if the interface is disabled/unconfigured +if response['result']['body']['TABLE_intf']['ROW_intf']['ip-disabled'] == 'TRUE': + print ('This interface is disabled') +#If it isn't disabled, then go ahead to search for its details +else: + Int1 = response['result']['body']['TABLE_intf']['ROW_intf']['intf-name'] + link_state = response['result']['body']['TABLE_intf']['ROW_intf']['link-state'] + intf_ip = response['result']['body']['TABLE_intf']['ROW_intf']['prefix'] + mask = response['result']['body']['TABLE_intf']['ROW_intf']['masklen'] +#chassis_id = response['result']['body']['chassis_id'] +#hostname = response['result']['body']['host_name'] + + print ("The interface is : {0} it has an ip of {2}/{3} and it is {1}".format(Int1, link_state, intf_ip, mask)) +#print ("ip : {0} is a \"{1}\" with hostname: {2} running software version : {3}".format(ip , chassis_id, hostname, kick_start_image)) \ No newline at end of file diff --git a/nxapi/samples/show_version.py b/nxapi/samples/show_version.py index a4f76ab..d5e7d10 100644 --- a/nxapi/samples/show_version.py +++ b/nxapi/samples/show_version.py @@ -2,20 +2,23 @@ import json import requests -print "enter ip address" -ip=raw_input() +print ("enter ip address of the switch") +ip=input () my_headers = {'content-type': 'application/json-rpc'} url = "http://"+ip+"/ins" username = "admin" -password = "ciscotme" +password = "Cisco321" payload=[{"jsonrpc": "2.0", "method": "cli", - "params": {"cmd": "show version", - "version": 1}, - "id": 1} + "params": { + "cmd": "show version", + "version": 1 + }, + "id": 1 + } ] response = requests.post(url, data=json.dumps(payload), headers=my_headers, auth=(username, password)).json() @@ -25,5 +28,4 @@ chassis_id = response['result']['body']['chassis_id'] hostname = response['result']['body']['host_name'] -print "ip : {0} is a \"{1}\" with hostname: {2} running software version : {3}".format(ip , chassis_id, hostname, kick_start_image) - +print ("Switch of this ip : {0} is a \"{1}\" with hostname: {2} running software version : {3}".format(ip , chassis_id, hostname, kick_start_image)) \ No newline at end of file