Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ###############################################################################
- # Written By: Al Esposito
- # Library for handling different Switches via vendor API's
- #
- # Currently Supported: Arista eAPI, Cisco NX-API
- # Future: Metamako API
- ###############################################################################
- import socket
- import requests
- import json
- class Switch:
- """
- Parent class construct for all vendors. Takes netdb json blob as input
- """
- def __init__(self, json_blob, username=None, password=None):
- self.username = username
- self.password = password
- self.name = json_blob['name']
- self.site = json_blob['site']['dns_prefix']
- self.tags = json_blob['tags']
- if not self.username and not self.password:
- try:
- # Try and pull RADIUS credentials from settings
- from settings import RADIUS_USER, RADIUS_PASSWORD
- self.username = RADIUS_USER
- self.password = RADIUS_PASSWORD
- except ImportError:
- pass
- def __repr__(self):
- return self.hostname
- __print__ = __repr__
- @property
- def is_core(self):
- """
- Determine if device is a core node by checking for 'CORE' in tags
- """
- if 'CORE' in self.tags:
- return True
- return False
- @property
- def hostname(self):
- """
- Create full hostname by merging self.name and self.site
- """
- return "%s.%s" % (self.name, self.site)
- class AristaSwitch(Switch):
- """
- Class construct for Arista eAPI
- """
- def run_commands(self, commands=[], output='json'):
- """
- Function to run commands against Arista eAPI
- """
- # Set timeout to 5 seconds
- socket.setdefaulttimeout(5.0)
- url = "https://%s/command-api" % (self.hostname)
- header = {'Content-Type': "application/json-rpc"}
- payload = {
- 'jsonrpc': "2.0",
- 'method': "runCmds",
- 'params': {
- 'cmds': commands,
- 'version': 1
- },
- 'id': "run_commands"
- }
- data = json.dumps(payload)
- response = requests.post(url,
- data=data,
- headers=header,
- auth=(self.username, self.password),
- verify=False).json()
- return response['result']
- @property
- def show_version(self):
- """
- Funcion to run show version against device for other child functions
- """
- response = self.run_commands(commands=['show version'])
- return response[0]
- @property
- def version(self):
- """
- Function to grab version from show_version
- """
- return self.show_version['version']
- @property
- def model(self):
- """
- Function to grab model from show_version
- """
- return self.show_version['modelName']
- @property
- def serial_number(self):
- """
- Function to grab serial number from show_version
- """
- return self.show_version['serialNumber']
- class NXOSSwitch(Switch):
- """
- Class construct for NXOS API.
- """
- def run_commands(self, commands=[], output='json'):
- """
- Function to run commands against NXOS API. Results are normalized to
- match the output of AristaSwitch.run_commands()
- """
- # Set timeout to 5 seconds
- socket.setdefaulttimeout(5.0)
- url = "https://%s/ins" % (self.hostname)
- header = {'Content-Type': "application/json-rpc"}
- payload = []
- if output == "json":
- method = "cli"
- if output == "text":
- method = "cli_ascii"
- for command in commands:
- payload.append({
- 'jsonrpc': "2.0",
- 'method': method,
- 'params': {
- 'cmd': command,
- 'version': 1
- },
- 'id': "run_commands"
- })
- data = json.dumps(payload)
- response = requests.post(url,
- data=data,
- headers=header,
- auth=(self.username, self.password),
- verify=False).json()
- response_normalized = []
- if isinstance(response, list):
- for each in response:
- response_normalized.append(each['result']['body'])
- if isinstance(response, dict):
- response_normalized.append(response['result']['body'])
- return response_normalized
- @property
- def show_version(self):
- """
- Function to grab show version from device for other child functions
- """
- response = self.run_commands(['show version'])
- return response[0]
- @property
- def version(self):
- """
- Function to grab version from show_version
- """
- return self.show_version['sys_ver_str']
- @property
- def model(self):
- """
- Function to grab model from show_version
- """
- return self.show_version['chassis_id']
- @property
- def serial_number(self):
- """
- Function to grab serial number from show_version
- """
- return self.show_version['proc_board_id']
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement