Skip to content

Instantly share code, notes, and snippets.

@twr14152
Last active November 3, 2018 23:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save twr14152/d72d19e665310920e595 to your computer and use it in GitHub Desktop.
Save twr14152/d72d19e665310920e595 to your computer and use it in GitHub Desktop.
Test Python interaction with nx-api
### Target hosts for this script lives in nexus_host.py
import time
from nexus_hosts import network_devices
import requests
import json
import getpass
UN = raw_input("Username : ")
PW = getpass.getpass("Password : ")
#For loop allows you to specify number of hosts
for IP in network_devices:
print IP
url='http://%s/ins' % IP
switchuser=UN
switchpassword=PW
myheaders={'content-type':'application/json-rpc'}
payload=[{"jsonrpc": "2.0","method": "cli", "params": {"cmd": "config t", "version": 1}, "id": 1},
{"jsonrpc": "2.0", "method": "cli", "params": {"cmd": "interface loopback 12","version": 1},"id": 2},
{"jsonrpc": "2.0", "method": "cli", "params": {"cmd": "description nxapi script test","version": 1},"id": 3},
{"jsonrpc": "2.0", "method": "cli", "params": {"cmd": "end", "version": 1}, "id": 4}]
response = requests.post(url,data=json.dumps(payload), headers=myheaders,auth=(switchuser,switchpassword))
print response.text
### This log file output is identical to what you see on the screen when running the script
f = open('nxapi-logfile0001.txt', 'a')
f.write(response.text)
f.close()
# This script is simply an attempt to learn the interaction between python and nx-api
import sys
import requests
import json
import getpass
UN = raw_input("Username : ")
PW = getpass.getpass("Password : ")
IP = raw_input("IP address of host : ")
cmd = raw_input("What command do you want to see? : ")
my_headers = {'content-type': 'application/json-rpc'}
url = 'http://%s/ins' % (IP)
payload = [{'jsonrpc': '2.0', 'method': 'cli', 'params': [cmd,1], 'id': '1'}]
my_data = json.dumps(payload)
response = requests.post(url, data=my_data, headers=my_headers, auth=(UN, PW))
print response.text
# This was my attempt to use the sandbox that comes with the Nexus9K to make an interactive script.
# As posted in the sandbox the output would not print in a readable fashion. Had to remove .json() at the end of the post command.
# Then add the print response.text. Then it started posting a readable output
#
import requests
import json
import getpass
UN = raw_input("Username: ")
PW = getpass.getpass("Password: ")
IP = raw_input("Host address or name: ")
command = raw_input("Commands: ")
url='http://%s/ins' % IP
switchuser=UN
switchpassword=PW
myheaders={'content-type':'application/json-rpc'}
payload=[
{
"jsonrpc": "2.0",
"method": "cli",
"params": {
"cmd": command,
"version": 1
},
"id": 1
}
]
response = requests.post(url,data=json.dumps(payload), headers=myheaders,auth=(switchuser,switchpassword))
print response.text
@twr14152
Copy link
Author

The v2 script was an output I modified from the Nexus93128s sandbox. I had to remove the .json() and add the print response.text to get the output readable from the cli.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment