Advertisement
Guest User

Untitled

a guest
Feb 3rd, 2017
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.49 KB | None | 0 0
  1. import dns.resolver
  2. import socket
  3. import ssl
  4. from jsonrpclib import Server
  5.  
  6. try:
  7.         _create_unverified_https_context = ssl._create_unverified_context
  8. except AttributeError:
  9.         # Legacy Python that doesn't verify HTTPS certificates by default
  10.         pass
  11. else:
  12.         # Handle target environment that doesn't support HTTPS verification
  13.         ssl._create_default_https_context = _create_unverified_https_context
  14.  
  15.  
  16. class Switch:
  17.     """ Class construct for a switch. Takes netdb json blob as input """
  18.  
  19.     def __init__(self, json_blob):
  20.         self.name = json_blob['name']
  21.         self.ip_address = json_blob['ip_address']
  22.         self.id = json_blob['id']
  23.         self.vendor = json_blob['vendor']
  24.         self.model = json_blob['name'].split("-")[0]
  25.         self.site = json_blob['site']['dns_prefix']
  26.  
  27.     def __repr__(self):
  28.         return self.hostname
  29.  
  30.     __print__ = __repr__
  31.  
  32.     @property
  33.     def hostname(self):
  34.         """ Parse hostname from self.name and self.site """
  35.  
  36.         return '%s.%s' % (self.name, self.site)
  37.  
  38.     def run_commands(self, username=None, password=None,
  39.                      commands=[], output='json'):
  40.         """ Function to run commands against device and return the output """
  41.  
  42.         socket.setdefaulttimeout(5.0)
  43.         connection = Server("https://%s:%s@%s/command-api" %
  44.                            (username, password, self.hostname))
  45.  
  46.         response = connection.runCmds(1, commands, output)
  47.  
  48.         return response
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement