Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import dns.resolver
- import socket
- import ssl
- from jsonrpclib import Server
- try:
- _create_unverified_https_context = ssl._create_unverified_context
- except AttributeError:
- # Legacy Python that doesn't verify HTTPS certificates by default
- pass
- else:
- # Handle target environment that doesn't support HTTPS verification
- ssl._create_default_https_context = _create_unverified_https_context
- class Switch:
- """ Class construct for a switch. Takes netdb json blob as input """
- def __init__(self, json_blob):
- self.name = json_blob['name']
- self.ip_address = json_blob['ip_address']
- self.id = json_blob['id']
- self.vendor = json_blob['vendor']
- self.model = json_blob['name'].split("-")[0]
- self.site = json_blob['site']['dns_prefix']
- def __repr__(self):
- return self.hostname
- __print__ = __repr__
- @property
- def hostname(self):
- """ Parse hostname from self.name and self.site """
- return '%s.%s' % (self.name, self.site)
- def run_commands(self, username=None, password=None,
- commands=[], output='json'):
- """ Function to run commands against device and return the output """
- socket.setdefaulttimeout(5.0)
- connection = Server("https://%s:%s@%s/command-api" %
- (username, password, self.hostname))
- response = connection.runCmds(1, commands, output)
- return response
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement