Advertisement
Guest User

Untitled

a guest
Feb 16th, 2017
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.55 KB | None | 0 0
  1. ###############################################################################
  2. # Written By: Al Esposito
  3. # Library for handling different Switches via vendor API's
  4. #
  5. # Currently Supported: Arista eAPI, Cisco NX-API
  6. # Future: Metamako API
  7. ###############################################################################
  8.  
  9. import socket
  10. import requests
  11. import json
  12.  
  13.  
  14. class Switch:
  15.     """
  16.    Parent class construct for all vendors. Takes netdb json blob as input
  17.    """
  18.  
  19.     def __init__(self, json_blob, username=None, password=None):
  20.         self.username = username
  21.         self.password = password
  22.         self.name = json_blob['name']
  23.         self.site = json_blob['site']['dns_prefix']
  24.         self.tags = json_blob['tags']
  25.  
  26.         if not self.username and not self.password:
  27.             try:
  28.                 # Try and pull RADIUS credentials from settings
  29.                 from settings import RADIUS_USER, RADIUS_PASSWORD
  30.  
  31.                 self.username = RADIUS_USER
  32.                 self.password = RADIUS_PASSWORD
  33.             except ImportError:
  34.                 pass
  35.  
  36.     def __repr__(self):
  37.         return self.hostname
  38.  
  39.     __print__ = __repr__
  40.  
  41.     @property
  42.     def is_core(self):
  43.         """
  44.        Determine if device is a core node by checking for 'CORE' in tags
  45.        """
  46.  
  47.         if 'CORE' in self.tags:
  48.             return True
  49.  
  50.         return False
  51.  
  52.     @property
  53.     def hostname(self):
  54.         """
  55.        Create full hostname by merging self.name and self.site
  56.        """
  57.  
  58.         return "%s.%s" % (self.name, self.site)
  59.  
  60.  
  61. class AristaSwitch(Switch):
  62.     """
  63.    Class construct for Arista eAPI
  64.    """
  65.  
  66.     def run_commands(self, commands=[], output='json'):
  67.         """
  68.        Function to run commands against Arista eAPI
  69.        """
  70.  
  71.         # Set timeout to 5 seconds
  72.         socket.setdefaulttimeout(5.0)
  73.  
  74.         url = "https://%s/command-api" % (self.hostname)
  75.         header = {'Content-Type': "application/json-rpc"}
  76.         payload = {
  77.             'jsonrpc': "2.0",
  78.             'method': "runCmds",
  79.             'params': {
  80.                 'cmds': commands,
  81.                 'version': 1
  82.             },
  83.             'id': "run_commands"
  84.         }
  85.         data = json.dumps(payload)
  86.         response = requests.post(url,
  87.                                  data=data,
  88.                                  headers=header,
  89.                                  auth=(self.username, self.password),
  90.                                  verify=False).json()
  91.         return response['result']
  92.  
  93.     @property
  94.     def show_version(self):
  95.         """
  96.        Funcion to run show version against device for other child functions
  97.        """
  98.  
  99.         response = self.run_commands(commands=['show version'])
  100.         return response[0]
  101.  
  102.     @property
  103.     def version(self):
  104.         """
  105.        Function to grab version from show_version
  106.        """
  107.  
  108.         return self.show_version['version']
  109.  
  110.     @property
  111.     def model(self):
  112.         """
  113.        Function to grab model from show_version
  114.        """
  115.  
  116.         return self.show_version['modelName']
  117.  
  118.     @property
  119.     def serial_number(self):
  120.         """
  121.        Function to grab serial number from show_version
  122.        """
  123.  
  124.         return self.show_version['serialNumber']
  125.  
  126.  
  127. class NXOSSwitch(Switch):
  128.     """
  129.    Class construct for NXOS API.
  130.    """
  131.  
  132.     def run_commands(self, commands=[], output='json'):
  133.         """
  134.        Function to run commands against NXOS API. Results are normalized to
  135.        match the output of AristaSwitch.run_commands()
  136.        """
  137.  
  138.         # Set timeout to 5 seconds
  139.         socket.setdefaulttimeout(5.0)
  140.  
  141.         url = "https://%s/ins" % (self.hostname)
  142.         header = {'Content-Type': "application/json-rpc"}
  143.  
  144.         payload = []
  145.  
  146.         if output == "json":
  147.             method = "cli"
  148.         if output == "text":
  149.             method = "cli_ascii"
  150.  
  151.         for command in commands:
  152.             payload.append({
  153.                 'jsonrpc': "2.0",
  154.                 'method': method,
  155.                 'params': {
  156.                     'cmd': command,
  157.                     'version': 1
  158.                 },
  159.                 'id': "run_commands"
  160.             })
  161.         data = json.dumps(payload)
  162.  
  163.         response = requests.post(url,
  164.                                  data=data,
  165.                                  headers=header,
  166.                                  auth=(self.username, self.password),
  167.                                  verify=False).json()
  168.  
  169.         response_normalized = []
  170.  
  171.         if isinstance(response, list):
  172.             for each in response:
  173.                 response_normalized.append(each['result']['body'])
  174.         if isinstance(response, dict):
  175.             response_normalized.append(response['result']['body'])
  176.  
  177.         return response_normalized
  178.  
  179.     @property
  180.     def show_version(self):
  181.         """
  182.        Function to grab show version from device for other child functions
  183.        """
  184.  
  185.         response = self.run_commands(['show version'])
  186.         return response[0]
  187.  
  188.     @property
  189.     def version(self):
  190.         """
  191.        Function to grab version from show_version
  192.        """
  193.  
  194.         return self.show_version['sys_ver_str']
  195.  
  196.     @property
  197.     def model(self):
  198.         """
  199.        Function to grab model from show_version
  200.        """
  201.  
  202.         return self.show_version['chassis_id']
  203.  
  204.     @property
  205.     def serial_number(self):
  206.         """
  207.        Function to grab serial number from show_version
  208.        """
  209.  
  210.         return self.show_version['proc_board_id']
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement