def execute(endpoint, commands, **kwargs): """Send exec commands :param endpoint: remote host or URI to connect to :param commands: command or commands to send :param creds: (optional) :class:`Creds <Creds>` object with authentication credentials :param protocol: (optional) Protocol name, e.g. 'ssh' or 'eapi' Usage: >>> arcomm.configure('eapi://veos', ['show version']) <ResponseStore [ok]> """ authorize = kwargs.pop('authorize', None) # allow an existing session to be used if not isinstance(endpoint, BaseSession): sess = Session(endpoint, **kwargs) sess.connect() else: sess = endpoint if authorize: if hasattr(authorize, '__iter__'): username, password = authorize[0], authorize[1] else: username, password = ('', authorize) sess.authorize(password, username) response = sess.send(commands, **kwargs) return response
def execute(endpoint, commands, creds=("admin", ""), protocol=None, authorize=None, **kwargs): """Send exec commands :param authorize: enter enable mode :param endpoint: remote host or URI to connect to :param commands: command or commands to send :param creds: (optional) :class:`Creds <Creds>` object with authentication credentials :param protocol: (optional) Protocol name, e.g. 'ssh' or 'eapi' Usage: >>> arcomm.configure('eapi://veos', ['show version']) <ResponseStore [ok]> """ # allow an existing session to be used if not isinstance(endpoint, Session): sess = Session(endpoint, creds=creds, protocol=protocol, **kwargs) sess.connect() else: sess = endpoint if authorize is not None: sess.authorize(authorize, None) response = sess.send(commands, **kwargs) return response
def _worker(host, commands, **kwargs): """Common worker func. Logs into remote host, executes commands and puts the results in the queue. Called from `Pool` and `Background`""" responses = None try: with Session(host, **kwargs) as sess: authorize = kwargs.pop('authorize', None) if authorize: sess.authorize(authorize) responses = sess.execute(commands, **kwargs) except (ConnectFailed, AuthenticationFailed, AuthorizationFailed) as exc: exc_type, exc_value, exc_traceback = sys.exc_info() repr_ = "\n".join(traceback.format_exception(exc_type, exc_value, exc_traceback)) # if we get kicked out of the session... we have to make our own # response object... :( responses = ResponseStore(host, **kwargs) responses.append(Response('_authentication', repr_, errored=True)) return responses
def connect(endpoint, creds=None, protocol=None, **kwargs): """Construct a :class:`Session <Session>` and make the connection :param endpoint: remote host or URI to connect to :param creds: (optional) :class:`Creds <Creds>` object with authentication credentials :param protocol: (optional) Protocol name, e.g. 'ssh' or 'eapi' Usage: >>> import arcomm >>> conn = arcomm.connect('ssh://veos', creds=BasicCreds('admin', '')) """ if isinstance(creds, (tuple, list)): creds = BasicCreds(*creds) sess = Session(endpoint, creds=creds, protocol=protocol, **kwargs) sess.connect() return sess