예제 #1
0
파일: api.py 프로젝트: vatsan62/arcomm
def connect(uri, creds=None, **kwargs):
    if creds:
        kwargs['creds'] = creds

    sess = Session()
    sess.connect(uri, **kwargs)
    return sess
예제 #2
0
파일: async.py 프로젝트: zongzhigang/arcomm
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
예제 #3
0
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
예제 #4
0
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
예제 #5
0
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
예제 #6
0
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
예제 #7
0
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
예제 #8
0
파일: api.py 프로젝트: zongzhigang/arcomm
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
예제 #9
0
파일: api.py 프로젝트: vatsan62/arcomm
def execute(uri, commands, **kwargs):

    # allow an existing session to be used
    if not isinstance(uri, BaseSession):
        sess = Session()
        sess.connect(uri,  **kwargs)
    else:
        sess = uri

    authorize = kwargs.pop('authorize', None)
    if authorize:
        if hasattr(authorize, '__iter__'):
            username, password = authorize[0], authorize[1]
        else:
            username, password = ('', authorize)
        sess.authorize(password, username)

    response = sess.execute(commands,  **kwargs)

    return response